hdu 5407 CRB and Candies(数论,LCM,快速幂取模,求逆元)
来源:互联网 发布:js仿今日头条导航栏 编辑:程序博客网 时间:2024/05/22 13:02
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 allK (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.
He wonders how many combinations he can select.
Can you answer his question for all
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
1 ≤
1 ≤
Output
For each test case, output a single integer – LCM modulo 1000000007(109+7 ).
Sample Input
512345
Sample Output
1231210
思路:我也不知道怎么来的公式反正直接用就是了。证明过程来自 http://arxiv.org/pdf/0906.2295v2.pdf 因为是英文的能看懂就看吧
结论就是:
令
所以用素数筛选求出所有n可由单个质因子累乘的数即可。然后用欧拉定理或者乘法逆元求n+1的逆元
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 1000100#define LL long longLL mod=1000000007;LL notprime[N],prime[N];LL dp[N];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;}
0 0
- hdu 5407 CRB and Candies(数论,LCM,快速幂取模,求逆元)
- hdu 5407 CRB and Candies(数论)
- 数论(CRB and Candies,HDU 5407)
- HDU 5407 CRB and Candies(LCM +最大素因子求逆元)
- HDU 5407 CRB and Candies 数论
- HDU 5407 CRB and Candies(数论+yy)
- CRB and Candies (组合的LCM)
- hdu 5407CRB and Candies
- HDU 5407 CRB and Candies
- hdu 5407 CRB and Candies
- hdu 5407 CRB and Candies
- 【数论】 HDOJ 5407 CRB and Candies
- HDU5407 CRB and Candies 数论
- hdu 5407 CRB and Candies(乘法逆元+快速幂)
- HDU 5407 CRB and Candies (Kummer定理)
- HDU 5407(CRB and Candies-OEIS)
- HDOJ 5407 CRB and Candies(求1到n的LCM 位图储存结构)
- Hdoj.5407 CRB and Candies【数论。。。】 2015/12/05
- linux下面select返回115错误
- hdu3461(并查集+快速幂)
- POJ2409 Let it Bead
- hdu 2082 找单词 母函数
- 依赖注入——人生若只如初见
- hdu 5407 CRB and Candies(数论,LCM,快速幂取模,求逆元)
- Redis基础(1)
- JavaScript 入门篇
- STM7F系列的资料
- 关于容器~
- sqoop访问MySql被拒绝
- SQL Select语句完整执行顺序
- Leetcode刷题系列(四)Binary Tree相关
- 自定义viewpager,他可以嵌套到大viewPage中使用