HDU 4675 GCD of Sequence (2013多校7 1010题 数学题)
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
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
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.
The line contains M integer, the i-th integer is the answer shows above when d is the i-th number.
3 3 3
3 5 3
1 2 3
59 3 0 1 1
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
比赛时候想到了做法,但是错估了复杂度,一直没写出来,结束后才搞完、
其实就是从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+;
long long pow_m(long long a,long long n)
{
long long ret = ;
long long tmp = a%MOD;
while(n)
{
if(n&)
{
ret *= tmp;
ret %= MOD;
}
tmp *= tmp;
tmp %= MOD;
n >>= ;
}
return ret;
}
long long C[];
//求ax = 1( mod m) 的x值,就是逆元(0<a<m)
long long inv(long long a,long long m)
{
if(a == )return ;
return inv(m%a,m)*(m-m/a)%m;
}
long long ans[];
int a[];
int num[];
int b[];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int N,M,K;
while(scanf("%d%d%d",&N,&M,&K) == )
{
memset(num,,sizeof(num));
for(int i = ;i <= N;i++)
{
scanf("%d",&a[i]);
num[a[i]]++;
}
C[N-K] = ;
for(int i = N-K+;i <= N;i++)
{
C[i] = C[i-]*i%MOD*inv(i-(N-K),MOD)%MOD;
}
for(int i = M;i>= ;i--)
{
int cnt = ;
long long ss = ;
for(int j = ; j*i <= M;j++)
{
cnt += num[i*j];
if(j > )ss = (ss + ans[i*j])%MOD;
}
int t = M/i;
if(t == )
{
if(cnt == N-K)ans[i] = ;
else ans[i] = ;
continue;
} if(cnt < N-K)
{
ans[i] = ;
continue;
}
long long tmp = ;
//在cnt个中选N-K个为相同的
tmp =(tmp*C[cnt])%MOD;
//其余的cnt-(N-K)个有t-1个选择
tmp = ( tmp * pow_m(t-,cnt-(N-K)) )%MOD;
//其余N-cnt个本来就不相同的,有t个选择
tmp = (tmp * pow_m(t,N-cnt));
ans[i] = (tmp - ss + MOD)%MOD; }
for(int i = ;i <= M;i++)
{
printf("%I64d",ans[i]);
if(i < M)printf(" ");
else printf("\n");
} }
return ;
}
最新文章
- C#后台调用公网接口(GET, POST)
- HDU 1257 最少拦截系统【LIS】
- 动态选路、RIP协议&;&;OSPF协议详解
- 用Visual Studio Code 开发应用之 安装 Visual Studio Code
- el表达式跟ognl表达式的区别(转)
- POJ Code the Tree 树的pufer编号
- 第三篇:GPU 并行编程的运算架构
- a标签# 和javascript:;的区别
- selenium中的webdriver定位元素失败的常见原因
- require include 一个隐藏的用法:作用域。
- Statistical Models and Social Science
- SSH复用代码最终版
- Java并发框架——AQS阻塞队列管理(三)——CLH锁改造
- python模块的使用
- 使用delimiter //,解决mysql end报错问题
- SpringBoot集成MongoDB
- java框架之Struts2(4)-拦截器&;标签库
- http与rfc
- plsql developer如何自定义快捷键
- what&#39;s the python之内置函数