HDU 5407 CRB and Candies 数论
2017-07-23 14:47
169 查看
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
解法:
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
解法:
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 1e6+7; const LL mod = 1e9+7; //g(n) = lCM(C(n,0),C(n,1),...,C(n,n)) //f(n) = LCM(1,2,...,n) g(n)=f(n+1)/(n+1) //f(1)=1, if(n=p^k) f(n)=f(n-1)*p else f(n)=f(n-1) bool prime[maxn]; int pri[maxn], cnt; LL p[maxn]; LL f[maxn], g[maxn]; void predeal() { for(int i=2; i*i<=maxn; i++){ if(!prime[i]){ for(int j=i*2; j<maxn; j+=i){ prime[j]=1; } } } cnt=0; for(int i=2; i<maxn; i++){ if(!prime[i]){ pri[cnt++]=i; } } } LL qsm(LL a, LL n){ LL ret = 1; while(n){ if(n&1) ret=ret*a%mod; a = a*a%mod; n>>=1; } return ret; } int main() { predeal(); memset(p, 0, sizeof(p)); for(int i=0; i<cnt; i++){ LL k=pri[i]; while(k<maxn){ p[k] = pri[i]; k*=pri[i]; } } f[1]=1; for(int i=2; i<maxn; i++){ if(p[i]){ f[i]=f[i-1]*p[i]%mod; } else f[i]=f[i-1]%mod; g[i-1]=f[i]*qsm(i,mod-2)%mod; } int T; scanf("%d", &T); while(T--){ int n; scanf("%d", &n); printf("%lld\n", g ); } return 0; }
相关文章推荐
- HDU 5407 CRB and Candies(数论+yy)
- 数论(CRB and Candies,HDU 5407)
- hdu 5407 CRB and Candies(数论)
- hdu 5407 CRB and Candies(数论,LCM,快速幂取模,求逆元)
- LCM性质 + 组合数 - HDU 5407 CRB and Candies
- hdu 5407 CRB and Candies(素数筛选法,除法取模(乘法逆元))
- HDU 5407 CRB and Candies(LCM +最大素因子求逆元)
- hdu 5407 CRB and Candies(乘法逆元+快速幂)
- HDU 5407 CRB and Candies(素数筛 + 逆元 + 组合数公式)——2015 Multi-University Training Contest 10
- hdu 5407 CRB and Candies(组合数+最小公倍数+素数表+逆元)2015 Multi-University Training Contest 10
- HDU 5407 CRB and Candies(数学 素数表+预处理+快速幂+乘法逆元)——多校练习10
- HDU 5407 CRB and Candies (2015多校第10场第一题)素数打表,除法取模(乘法逆元)
- HDU 5407(CRB and Candies-OEIS)
- HDU 5407 CRB and Candies (Kummer定理)
- hdu 5407 CRB and Candies
- Hdu 5407 CRB and Candies (找规律)
- 【数论】 HDOJ 5407 CRB and Candies
- hdu 5407 CRB and Candies 2015多校联合训练赛#10 找规律 素数筛法
- 数论 HDOJ 5407 CRB and Candies
- HDU 5407(2015多校10)-CRB and Candies(组合数最小公倍数+乘法逆元)