hdu 5407CRB and Candies

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


1。乘法逆元

求(a/b)%c时 化成 (a%c)/(b%c)是错误的,所以需要用到乘法逆元。(a%c)*(b^-1%c)。

b^-1的求法:

费马小定理(Fermat Theory): 假如p是质数,且Gcd(a,p)=1,那么 a^(p-1)(mod p)≡1。

由此可得a*a^(p-2)=1 (mod p) 即a^(p-2)就是a的乘法逆元。通过快速幂可求。

2。LCM(C(N,0),C(N,1),...,C(N,N))=LCM(1,2,...,n)/(n+1)

知乎上看到有人证明,并没有看懂。http://www.zhihu.com/question/34859879

3。求LCM(1,2,...,n)的简便算法

If n =p^k,then f(n) = f(n−1)× p, else f(n) = f(n−1).


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

#define LL long long
const int N = 1000010;
const int Mod = 1000000007;

int n, t;
LL ans, mid;
LL inv[N];
LL anss[N];
bool su2[N];
int su[N], su1[N];
LL f[N];                                                    //f[i]表示1~i的最小公倍数


void table()
{
    inv[1] = 1;
    for(int i = 2; i < N; i++)                              //求i的逆元
    {
        inv[i] = inv[Mod%i]*(Mod-Mod/i) % Mod;
    }
    memset(su2, 1, sizeof(su2));
    memset(su1, 0, sizeof(su1));
    su2[0] = su2[1] = 0;                                    //求N以内的素数
    su2[2] = 1;
    for(int i = 3; i < N; i++) su2[i] = i%2 == 0 ? 0 : 1;
    for(int i = 3; i <= sqrt(N*1.0); i++)
    {
        if(su2[i])
        {
            for(int j = i*i; j <= N; j += 2*i)
            {
                su2[j] = 0;
            }
        }
    }
    int k = 0;
    for(int i = 0; i < N; i++)                              //打N以内的素数表
    {
        if(su2[i] == 1) su[k++] = i;
    }
    for(int i = 0; i < k; i++)                              //寻找满足p^k的数,其中p为素数,k为正整数
    {
        LL mid = su[i];
        while(mid < N)
        {
            su1[mid] = su[i];
            mid *= su[i];
        }
    }
    f[1] = 1;                                               //打N以内的1~i的最小公倍数表
    for(int i = 2; i < N; i++)
    {
        if(su1[i])  f[i] = f[i-1]*su1[i];
        else f[i] = f[i-1];
        f[i] %= Mod;
        anss[i-1] = (f[i]*inv[i])%Mod;                      //答案表
    }
}

int main()
{
    table();
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        printf("%I64d\n", anss[n]);
    }
    return 0;
}



害羞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值