[数学]hdu5407 CRB and Candies


CRB and Candies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 209    Accepted Submission(s): 84


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

1002.CRB and Candies

The problem is just to calculate g(N)\ =\ LCM(C(N,0), C(N,1), ..., C(N, N))g(N) = LCM(C(N,0),C(N,1),...,C(N,N)).

Introducing function f(n)\ =\ LCM(1, 2, ..., n)f(n) = LCM(1,2,...,n), the fact g(n)\ =\ f(n+1) / (n+1)g(n) = f(n+1)/(n+1)holds.

We calculate f(n)f(n) in the following way.

f(1)=1f(1)=1.

If n\ =p^{k}n =pk then f(n)\ =\ f(n-1) \times \ pf(n) = f(n1)× p, else f(n)\ =\ f(n-1)f(n) = f(n1).

Time complexity:O(N\cdot logN)O(NlogN)


#include <cstdio>
#include <iostream>
#include <algorithm>
#include<cstring>
using namespace std;

int n,T;
const int MAXN = 1000100;
typedef long long ll;
ll f[MAXN],p[MAXN];
const int mod = 1000000007;
ll pmod(ll a, ll n)
{
    ll ret = 1;
    for (; n; n>>=1, a=a*a%mod) if (n & 1)
        ret = ret * a % mod;
    return ret;
}
bool ok(ll n)
{
    int t = p[n];
    while (n % t==0 && n > 1) n /= t;
    return n==1;
}
ll inv(ll a)
{
    return pmod(a, mod - 2);
}
void init()
{
    for(int i = 1;i<MAXN;i++) p[i] = i;
    for(int i = 2;i<MAXN;i++)
        if (p[i] == i)
        {
            for(int j = i+i; j <MAXN;j+=i)
                p[j] = i;
        }
    f[0] = 1;
    for(int i = 1;i<MAXN;i++)
    {
        if(ok(i))
            f[i] = f[i-1] * p[i] % mod;
        else
            f[i] = f[i-1];
    }
//    for(int i = 1;i<=200;i++)
//        printf("%d %d\n",i,p[i]);
}

int main()
{
    init();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        n++;
        ll ans =  f[n] * inv(n) % mod;
        printf("%I64d\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值