HDU 5407 CRB and Candies

传送门
http://oeis.org/A002944 http://oeis.org/A003418
an=LCM(C(n,0),C(n,1),C(n,2),...,C(n,n))
bn=LCM(1,2,3,...,n)
an=bn+1n+1
if (n=pk)bn=pbn1elsebn=bn1
结论的证明:http://arxiv.org/pdf/0906.2295v2.pdf

#include <bits/stdc++.h>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 1000100;
ll p[N];
ll a[N];
bool ok(ll n)
{
    int t = p[n];
    while (n % t==0 && n > 1) n /= t;
    return n==1;
}
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;
}
ll inv(ll a)
{
    return pmod(a, mod - 2);
}
int main()
{
    for (int i=1;i<N;i++) p[i]=i;
    for (int i=2;i<N;i++) if (p[i]==i) {
        for (int j=i+i;j<N;j+=i) p[j] = i;
    }
    a[0] = 1;
    for (int i=1;i<N;i++) {
        if (ok(i))
            a[i] = a[i-1] * p[i] % mod;
        else
            a[i] = a[i-1];
    }
    int n; int re; cin>>re;
    while (re--) {
        scanf("%d", &n);
        n++;
        ll ans = a[n] * inv(n) % mod;
        printf("%I64d\n", ans);
    }
}

你可能感兴趣的:(公式,数学,C语言)