您的位置:首页 > 产品设计 > UI/UE

HDU 4675 GCD of Sequence (2013多校7 1010题 数学题)

2013-08-13 20:12 411 查看

GCD of Sequence

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 46 Accepted Submission(s): 14


[align=left]Problem Description[/align]
Alice is playing a game with Bob.
Alice shows N integers a1, a2, …, aN, and M, K. She says each integers 1 ≤ ai ≤ M.
And now Alice wants to ask for each d = 1 to M, how many different sequences b1, b2, …, bN. which satisfies :
1. For each i = 1…N, 1 ≤ b[i] ≤ M
2. gcd(b1, b2, …, bN) = 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(x1, x2, …, xn) is the greatest common divisor of x1, x2, …, xn

[align=left]Input[/align]
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: a1, a2, ..., an (1 ≤ ai ≤ M) which is original sequence.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

3 3 3
3 3 3
3 5 3
1 2 3

[align=left]Sample Output[/align]

7 1 0
59 3 0 1 1

Hint

In 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

[align=left]Source[/align]
2013 Multi-University Training Contest 7

[align=left]Recommend[/align]
zhuyuanchen520

比赛时候想到了做法,但是错估了复杂度,一直没写出来,结束后才搞完、

其实就是从M算到1.

假如现在算i. 那么找到i ~ M中i的倍数。

看原序列中有多少个是i的倍数,设为cnt.

因为最终假如gcd是i的话,所有数都必须是i的倍数。

那就相当于在cnt个中,要把cnt-(N-K)个变掉,其余的(N-cnt)个要变成i的倍数。

i的倍数为t = M/i 个。

那么符合的数有C[cnt][N-K]* (t-1)^(cnt-(N-K)) * t^(N-cnt)

这个算出来的是gcd是i的倍数的情况。

减掉gcd是2i,3i....这样的就行了

/* **********************************************
Author      : kuangbin
Created Time: 2013/8/13 16:39:35
File Name   : F:\2013ACM练习\2013多校7\1010.cpp
*********************************************** */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

const int MOD = 1e9+7;
long long pow_m(long long a,long long n)
{
long long ret = 1;
long long tmp = a%MOD;
while(n)
{
if(n&1)
{
ret *= tmp;
ret %= MOD;
}
tmp *= tmp;
tmp %= MOD;
n >>= 1;
}
return ret;
}
long long C[300010];
//求ax = 1( mod m) 的x值,就是逆元(0<a<m)
long long inv(long long a,long long m)
{
if(a == 1)return 1;
return inv(m%a,m)*(m-m/a)%m;
}
long long ans[300010];
int a[300010];
int num[300010];
int b[300010];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int N,M,K;
while(scanf("%d%d%d",&N,&M,&K) == 3)
{
memset(num,0,sizeof(num));
for(int i = 1;i <= N;i++)
{
scanf("%d",&a[i]);
num[a[i]]++;
}
C[N-K] = 1;
for(int i = N-K+1;i <= N;i++)
{
C[i] = C[i-1]*i%MOD*inv(i-(N-K),MOD)%MOD;
}
for(int i = M;i>= 1;i--)
{
int cnt = 0;
long long ss = 0;
for(int j = 1; j*i <= M;j++)
{
cnt += num[i*j];
if(j > 1)ss = (ss + ans[i*j])%MOD;
}
int t = M/i;
if(t == 1)
{
if(cnt == N-K)ans[i] = 1;
else ans[i] = 0;
continue;
}

if(cnt < N-K)
{
ans[i] = 0;
continue;
}
long long tmp = 1;
//在cnt个中选N-K个为相同的
tmp =(tmp*C[cnt])%MOD;
//其余的cnt-(N-K)个有t-1个选择
tmp = ( tmp * pow_m(t-1,cnt-(N-K)) )%MOD;
//其余N-cnt个本来就不相同的,有t个选择
tmp = (tmp * pow_m(t,N-cnt));
ans[i] = (tmp - ss + MOD)%MOD;

}
for(int i = 1;i <= M;i++)
{
printf("%I64d",ans[i]);
if(i < M)printf(" ");
else printf("\n");
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: