hdu 5407 CRB and Candies(乘法逆元+快速幂)

题意:

lcm(C0n,C1n,...,Cnn)=?

解析:

打表了发现规律是 lcm(1,2,3,n,n+1)/(n+1)

然而lcm怎么求呢?

可以发现随着n的增长好多最小公倍数都是不变的,增长的位置都发生在有新的质因子产生或者原本质因子的次数增大的地方。

所以首先找出1~100000的所有质数,
ans=login ,其中 0<=i<=n 且为素数

然后lcm就求出来了,最后乘以(n+1)的逆元即可。

my code

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll MOD = (int)1e9 + 7;
const int N = (int)1e6 + 5;
const double eps = 1e-10;
int prime[N], tot;
bool isPrime[N];

void getPrime() {
    memset(isPrime, false, sizeof(isPrime));
    tot = 0;
    for(int i = 2; i < N; i++) {
        if(!isPrime[i]) prime[tot++] = i;
        for(int j = 2*i; j < N; j += i) {
            if(j > N) break;
            isPrime[j] = true;
        }
    }
}

ll modpow(ll a, ll k) {
    ll c = 1;
    while(k) {
        if(k & 1) c = (a * c) % MOD;
        a = (a * a) % MOD;
        k >>= 1;
    }
    return c;
}

ll exgcd(ll a,ll b,ll &x,ll &y) {
    if(a == 0 && b == 0) return -1;
    if(b == 0) { x=1; y=0; return a;}
    ll d = exgcd(b, a%b, y, x);
    y -= a/b*x;
    return d;
}

ll inv(ll a, ll n) {
    ll x,y;
    ll d=exgcd(a,n,x,y);
    if(d==1) return (x%n+n)%n;
    else return -1;
}

ll cal(ll n) {
    int size = upper_bound(prime, prime+tot, n) - prime + 1;
    ll ret = 1;
    for(int i = 0; i < size; i++) {
        ll tmp = (ll)(log(n+1) / log(prime[i]) + eps);
        ret = (ret * modpow(prime[i], tmp)) % MOD;
    }
    ret = (ret * inv(n+1, MOD)) % MOD;
    return ret;
}

int main() {
    getPrime();
    int T;
    ll n;
    scanf("%d", &T);
    while(T--) {
        scanf("%lld", &n);
        printf("%lld\n", cal(n));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值