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 ;
}
HDU 4675 GCD of Sequence (2013多校7 1010题 数学题)的更多相关文章
- HDU 4705 Y (2013多校10,1010题,简单树形DP)
Y Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...
- HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- HDU 4691 Front compression (2013多校9 1006题 后缀数组)
Front compression Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Othe ...
- HDU 4679 Terrorist’s destroy (2013多校8 1004题 树形DP)
Terrorist’s destroy Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4671 Backup Plan (2013多校7 1006题 构造)
Backup Plan Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- HDU 4667 Building Fence(2013多校7 1002题 计算几何,凸包,圆和三角形)
Building Fence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)To ...
- HDU 4675 GCD of Sequence(容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4675 题意:给出n,m,K,一个长度为n的数列A(1<=A[i]<=m).对于d(1< ...
- HDU - 4675 GCD of Sequence (莫比乌斯反演+组合数学)
题意:给出序列[a1..aN],整数M和k,求对1-M中的每个整数d,构建新的序列[b1...bN],使其满足: 1. \(1 \le bi \le M\) 2. \(gcd(b 1, b 2, -, ...
随机推荐
- 【转载】C#异常Retry通用类
//Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...
- Zabbix3.0 API调用
Zabbix API 是什么? API简单来说是服务对外开放的一个接口,用户通过该接口传递请求,完成操作.API的背后是一组方法的集合,这些方法实现了服务对应的不同功能,调用API实际上就是换了一种方 ...
- Nginx1.8.1 编译扩展https
nginx无缝编译扩展https 本贴只限用于通过编译安装的nginx,如果用的是yum源安装请卸载后参见 http://www.cnblogs.com/rslai/p/7851220.html 安装 ...
- 【转】doxygen+graphviz生成工程中的类继承树及函数调用图
转自----hequn8128 在阅读代码量比较多的项目时,类的继承树和函数调用图能够直观地向我们显示类之间或者函数之间的各种关系,方便我们了解程序的整体框架,很多时候可以起到事半功倍的作用.这里尝试 ...
- python中的any和all函数
any和all函数是判断一组数据真假性的综合结果.以下摘选自Stackoverflow. ------------------ 分割线开始 ----------------- any any will ...
- 用WP SMTP插件实现邮件发送功能
WordPress本身是采用mail()函数发邮件的,但是这样发出的邮件很容易被放入垃圾箱,很多主机商(特别是Windows主机)为了避免用户滥发邮件直接禁用了mail()函数,还有些云计算平台(比如 ...
- python开发学习-day07(面向对象之多态、类的方法、反射、新式类and旧式类、socket编程)
s12-20160227-day07 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- visible, disable, css绑定
这个例子非常简单,主要演示如何通过属性控制html元素的显示与否(visible),可用性(disable)以及根据属性添加相应的CSS样式. 先简单的看一段代码: <p> <inp ...
- python元类:type和metaclass
python元类:type和metaclass python中一切皆对象,所以类本身也是对象.类有创建对象的能力,那谁来创建类的呢?答案是type. 1.用tpye函数创建一个类 class A(ob ...
- 久邦数码(3G门户)面试
久邦数码(3G门户)面试 1.数组和链表的区别(为什么数组带有索引) 2.数据库(手写选出一个公司年龄最大的100个员工) 3.一百个数查找一个数 利用二分查找一个数在最差的情况下至少比较多少次 4. ...