hdu 5407 CRB and Candies(数论,LCM,快速幂取模,求逆元)
2016-06-13 19:38
597 查看
CRB and Candies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 947 Accepted Submission(s): 442
Problem Description
CRB has N different
candies. He is going to eat K candies.
He wonders how many combinations he can select.
Can you answer his question for all K(0
≤ K ≤ N)?
CRB is too hungry to check all of your answers one by one, so he only asks least common multiple(LCM) of all answers.
Input
There are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case there is one line containing a single integer N.
1 ≤ T ≤
300
1 ≤ N ≤ 106
Output
For each test case, output a single integer – LCM modulo 1000000007(109+7).
Sample Input
5 1 2 3 4 5
Sample Output
1 2 3 12 10
题意:求LCM(C(0,n),C(1,n)...,C(n,n))
思路:我也不知道怎么来的公式反正直接用就是了。证明过程来自 http://arxiv.org/pdf/0906.2295v2.pdf 因为是英文的能看懂就看吧
结论就是:
令an=LCM(C(n,0),C(n,1),C(n,2),...,C(n,n))
bn=LCM(1,2,3,...,n)
an=bn+1n+1
if (n=pk)bn=p∗bn−1elsebn=bn−1
所以用素数筛选求出所有n可由单个质因子累乘的数即可。然后用欧拉定理或者乘法逆元求n+1的逆元
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define N 1000100 #define LL long long LL mod=1000000007; LL notprime ,prime ; LL dp ; void init() { for(LL i=2; i*i<N; i++) { if(!notprime[i]) { for(LL j=i; j<N; j*=i) prime[j]=i; for(LL j=i*i; j<N; j+=i) notprime[j]=1; } } dp[1]=1; for(int i=2; i<=N; i++) if(prime[i]) dp[i]=dp[i-1]*prime[i]%mod; else if(!notprime[i]) dp[i]=dp[i-1]*i%mod; else dp[i]=dp[i-1]; } LL pow_mod(LL a,LL n) { LL ans=1; while(n) { if(n&1) ans=ans*a%mod; a=a*a%mod; n>>=1; } return ans; } LL inv(LL n) { return pow_mod(n,mod-2); } int main() { int T; LL n; init(); scanf("%d",&T); while(T--) { scanf("%lld",&n); LL ans=dp[n+1]*inv(n+1)%mod; printf("%lld\n",ans); } return 0; }
相关文章推荐
- 第十六周项目1:说出程序的功能,并上机验证(1)
- Activiti 统一身份管理
- hdu 2082 找单词 母函数
- Activiti 统一身份管理
- VR定制 AR定制 就找北京动软VR开发团队(VR案例 AR案例)
- hdu3461(并查集+快速幂)
- Android入门--CheckBox 的isChecked 属性
- linux下面select返回115错误
- LightOJ 1201 A Perfect Murder 最大独立集
- 扒一扒那些奇葩的甲方吧
- Java并发编程-阻塞队列(BlockingQueue)的实现原理
- Android数据库(SQLite)框架——LitePal实战入门
- c++使用MYSQL教程
- 关于listview 的更新问题
- Android 自定义View之随机生成图片验证码
- C++ STL中迭代器介绍
- maven pom.xml详解
- Sprint2-3.0
- 6/13 Sprint2 看板和燃尽图
- 面试总结之指针