CRB and Candies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 193 Accepted Submission(s): 73
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.
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
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
题意:求g(n)。数学不好,谁告诉我这哪来
求g(n),先通过构造f(n)=lcm(1,2,....n),然后求g(n);
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<stdio.h>
#include<math.h>
#include <string>
#include<string.h>
#include<map>
#include<queue>
#include<set>
#include<utility>
#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define eps 1e-8
#define inf 0x3f3f3f3f
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int
#define mod 1000000007
#define maxn 1000005
#define maxm 100005
int prime[maxn],pn;
int v[maxn];
ll f[maxn],g[maxn];
void init_prime()
{
int i, j;
for(i = 2;i <= sqrt(maxn); ++i)
{
if(!prime[i])
for(j = i * i; j < maxn; j += i)
prime[j] = 1;
}
pn = 0;
for(i = 2;i <= maxn; ++i)
if(!prime[i])
prime[pn++] = i;
}
ll inv(ll a,ll m){// (x/a)%m的逆元
ll p=1,q=0,b=m,c,d;
while(b>0){
c=a/b;
d=a; a=b; b=d%b;
d=p; p=q; q=d-c*q;
}
return p<0?p+m:p;
}
int main()
{
int t;
rd(t);
memset(v,0,sizeof(v));
init_prime();
for(int i=0;i<pn;i++)
{
ll k=prime[i];
while(k<maxn){
v[k]=prime[i];
k*=prime[i];
}
}
f[1]=1;
for(int i=2;i<maxn;i++){
if(v[i]) {
f[i]=f[i-1]*v[i];
f[i]%=mod;
}
else f[i]=f[i-1];
f[i]%=mod;
g[i-1]=f[i]*inv(i,mod);
g[i-1]%=mod;
}
int n;
while(t--){
rd(n);
printf("%I64d\n",g[n]);
}
return 0;
}