GCD of Sequence
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 209 Accepted Submission(s): 65
Problem Description
Alice is playing a game with Bob.
Alice shows N integers a 1, a 2, …, a N, and M, K. She says each integers 1 ≤ a i ≤ M.
And now Alice wants to ask for each d = 1 to M, how many different sequences b 1, b 2, …, b N. which satisfies :
1. For each i = 1…N, 1 ≤ b[i] ≤ M
2. gcd(b 1, b 2, …, b N) = d
3. There will be exactly K position i that ai != bi (1 ≤ i ≤ n)
Alice thinks that the answer will be too large. In order not to annoy Bob, she only wants to know the answer modulo 1000000007.Bob can not solve the problem. Now he asks you for HELP!
Notes: gcd(x 1, x 2, …, x n) is the greatest common divisor of x 1, x 2, …, x n
Alice shows N integers a 1, a 2, …, a N, and M, K. She says each integers 1 ≤ a i ≤ M.
And now Alice wants to ask for each d = 1 to M, how many different sequences b 1, b 2, …, b N. which satisfies :
1. For each i = 1…N, 1 ≤ b[i] ≤ M
2. gcd(b 1, b 2, …, b N) = d
3. There will be exactly K position i that ai != bi (1 ≤ i ≤ n)
Alice thinks that the answer will be too large. In order not to annoy Bob, she only wants to know the answer modulo 1000000007.Bob can not solve the problem. Now he asks you for HELP!
Notes: gcd(x 1, x 2, …, x n) is the greatest common divisor of x 1, x 2, …, x n
Input
The input contains several test cases, terminated by EOF.
The first line of each test contains three integers N, M, K. (1 ≤ N, M ≤ 300000, 1 ≤ K ≤ N)
The second line contains N integers: a 1, a 2, ..., a n (1 ≤ a i ≤ M) which is original sequence.
The first line of each test contains three integers N, M, K. (1 ≤ N, M ≤ 300000, 1 ≤ K ≤ N)
The second line contains N integers: a 1, a 2, ..., a n (1 ≤ a i ≤ M) which is original sequence.
Output
For each test contains 1 lines :
The line contains M integer, the i-th integer is the answer shows above when d is the i-th number.
The line contains M integer, the i-th integer is the answer shows above when d is the i-th number.
Sample Input
3 3 3 3 3 3 3 5 3 1 2 3
Sample Output
7 1 0 59 3 0 1 1HintIn the first test case : when d = 1, {b} can be : (1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 2, 2) (2, 1, 1) (2, 1, 2) (2, 2, 1) when d = 2, {b} can be : (2, 2, 2) And because {b} must have exactly K number(s) different from {a}, so {b} can't be (3, 3, 3), so Answer = 0
Source
Recommend
zhuyuanchen520
题意: 有三个数N, M, K, 然后N个特定序列。 从这特定的序列里改变K个数字, 取值范围为 [1,M]。 然后求出各序列的最大公约数为1~m分别多少个。
思路: 组合数学
公式:
只是总的。 但是ans(i) 包括了 ans(2 * i) ans(3 * i) ……
所以要一一减去。
公式推到过程
前提: 每次一定要刚好改变k个数字。
特定序列中有N - sumb个不是i的倍数。 那么要使整个序列的最大公约数是i, 那只能是将 N - sumb都变为是 i的公倍数。
如果 N - sumb > K 就无法实现。 ans(i) = 0;
否则将所有不是 i 的公倍数的变为i的公倍数。 对于每一个数字, 都有 m / i 种可能。 所以 就有 (m / i) ^ (n - sumb);
然后还需要改变 k - (N - sumb) 次。
就是要在i的公倍数中选出k - (N - sumb)。C(sumb, k - (n - sumb)).
然后对于每个数字有 m / i - 本身 种可能。 所以就是 (m / i - 1) ^ (k - (n - sumb)) * C(sumb, k - (n - sumb));
对于除法取模还需要用到费马小定理: a ^ (p - 1) % p = 1; -> a ^ (p - 2) % p = (1 / a) % p;
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//
const int V = 300000 + 50;
const int inf = 0x7fffffff;
const int mod = 1000000000 + 7;
int n, m, k;
int sum[V];
__int64 ans[V], d[V], e[V];
__int64 Quick_Pow(__int64 a, __int64 b) {
__int64 res = 1;
while(b) {
if(b & 1)
res = (res * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return res % mod;
}
__int64 Cal(__int64 n, __int64 k) {
return (d[n] * e[k] % mod) * e[n - k] % mod;
}
int main() {
int i, j;
d[0] = e[0] = 1;
for(i = 1; i < V; ++i) {
d[i] = (d[i - 1] * i) % mod;
e[i] = Quick_Pow(d[i], mod - 2);
}
while(~scanf("%d%d%d", &n, &m, &k)) {
memset(sum, 0, sizeof(sum));
for(i = 0; i < n; ++i) {
int temp;
scanf("%d", &temp);
sum[temp]++;
}
for(i = m; i >= 1; --i) {
int sum_d = 0;
for(j = i; j <= m; j += i)
sum_d += sum[j];
if(n - sum_d > k) {
ans[i] = 0;
continue;
}
ans[i] = (Quick_Pow(m / i, n - sum_d) * Quick_Pow(m / i - 1, k - (n - sum_d)) % mod) * Cal(sum_d, k - (n - sum_d)) % mod;
//减去重复的
for(j = 2 * i; j <= m; j += i)
ans[i] = (ans[i] - ans[j] + mod) % mod;
}
for(i = 1; i < m; ++i)
printf("%I64d ", ans[i]);
printf("%I64d\n", ans[m]);
}
}