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

Problem Description
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

 
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: a1, a2, ..., an (1 ≤ ai ≤ 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.
 
Sample Input
3 3 3
3 3 3
3 5 3
1 2 3
 
Sample Output
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

 
Source
 
Recommend
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+;
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 ;
}

最新文章

  1. C#后台调用公网接口(GET, POST)
  2. HDU 1257 最少拦截系统【LIS】
  3. 动态选路、RIP协议&amp;&amp;OSPF协议详解
  4. 用Visual Studio Code 开发应用之 安装 Visual Studio Code
  5. el表达式跟ognl表达式的区别(转)
  6. POJ Code the Tree 树的pufer编号
  7. 第三篇:GPU 并行编程的运算架构
  8. a标签# 和javascript:;的区别
  9. selenium中的webdriver定位元素失败的常见原因
  10. require include 一个隐藏的用法:作用域。
  11. Statistical Models and Social Science
  12. SSH复用代码最终版
  13. Java并发框架——AQS阻塞队列管理(三)——CLH锁改造
  14. python模块的使用
  15. 使用delimiter //,解决mysql end报错问题
  16. SpringBoot集成MongoDB
  17. java框架之Struts2(4)-拦截器&amp;标签库
  18. http与rfc
  19. plsql developer如何自定义快捷键
  20. what&#39;s the python之内置函数

热门文章

  1. [ python ] 集合的使用
  2. SVM资料
  3. C# Winform频繁刷新导致界面闪烁解决方法
  4. EL(表达式)语言的几种运算符
  5. window7 开启自带 ftp
  6. HDU 17111 Number Sequence(KMP裸题)
  7. python爬取漫画
  8. @PathVariable @RequestParam @RequestBody 的区别
  9. LeetCode818. Race Car
  10. 【转】Android打印机--没有设备驱动sdk,自己实现USB打印功能