HDU 4059 The Boss on Mars(容斥原理)

来源:互联网 发布:刷信誉源码平台 编辑:程序博客网 时间:2024/04/27 17:04

         容斥原理,先把 n 质分解,利用x^4 的前n项和公式,然后把与 n 不互质的数的和求出来然后减一下就可以了。

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<cmath>#define LL long long#define CLR(a, b) memset(a, b, sizeof(a))using namespace std;const int N = 100000001;const int M = 11111;const int MOD = 1e9 + 7;vector<LL> hav;bool isp[M];int p[M], cnt, sz;LL n;void getp(){    CLR(isp, 0);    int i, j;cnt = 0;    isp[0] = isp[1] = 1;    for(i = 2; i < M; i ++)    {        if(!isp[i])        {            p[cnt ++] = i;            if(i <= 1111)for(j = i * i; j < M; j += i) isp[j] = 1;        }    }}void get_hav(LL h){    LL i;    hav.clear();    for(i = 0; i < cnt && h > 1; i ++)    {        if(h % p[i] == 0)        {            h /= p[i];            int s = hav.size();            if( s == 0 || hav[s - 1] != p[i])hav.push_back(p[i]);            i --;        }    }    if(h != 1) hav.push_back(h);}LL inv(LL x){    LL r, y;    for(r = 1, y = MOD - 2; y; x = x * x % MOD, y >>= 1) (y & 1) && (r = r * x % MOD);    return r;}LL get_sum(LL n){    LL ret;    ret = n * (2 * n + 1) % MOD * (n + 1) % MOD * ((3 * n * (n + 1) - 1) % MOD) % MOD * inv(30) % MOD;    return ret;}LL F_pow(LL a, LL b){    LL ret = 1;    while(b)    {        if(b & 1) ret = ret * a % MOD;        a = a * a % MOD;        b >>= 1;    }    return ret;}LL dfs(LL u, LL c){    LL i, j;    LL ret = 0;    for(i = u; i < sz; i ++)    {        ret = (ret + F_pow(hav[i] * c, 4) * get_sum(n / hav[i] / c) % MOD - dfs(i + 1, c * hav[i])) % MOD;    }    return (ret + MOD) % MOD;}int main(){    //freopen("input.txt", "r", stdin);    LL i, j, t;    getp();    cin >> t;    while(t --)    {        cin >> n;        get_hav(n);        sz = hav.size();        cout << (get_sum(n) - dfs(0, 1) + MOD) % MOD << endl;    }}


原创粉丝点击