HDU 4059 The Boss on Mars(容斥原理 + 四次方求和+求逆元)

题目大意:

给定 T 组数据,每组数据一个数 n ,然后让你求 <n且与 n 互素的数的四次方,详见样例。

Sample Input

2 4 5

Sample Output

82 35

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

const int mod = 1000000007;
const int inv_30 = 233333335;
typedef long long LL;
int cas;
int prime[30];
int top;

int func(LL n)
{
    return n*(n+1)%mod*(2*n+1)%mod*(3*(n*n)%mod+3*n-1)%mod*inv_30%mod;
}
void work()
{
    int n;
    scanf("%d", &n);

    int nn = n;
    top = 0;
    for(int i=2;i*i<=nn;i++) if(nn%i==0)
    {
        prime[top++] = i;
        while(nn%i==0) nn/=i;
    }
    if(nn>1) prime[top++] = nn;
 //先筛素数:
    int res = func(n);
    for(int i=1;i<(1<<top);i++)
    {
        int tmp=1,flag=false;
        for(int j=0;j<top;j++) if(i&(1<<j))
            tmp*=prime[j], flag = !flag,cout<<i<<" "<<j<<" "<<tmp<<"  "<<flag<<"    tmp"<<endl;
 //
        int t = func(n/tmp);
       // cout<<n/tmp<<" "<<endl;
        //cout<<"t   "<<t<<endl;
        tmp = ((LL)tmp*tmp)%mod;
        tmp = ((LL)tmp*tmp)%mod;
        tmp = ((LL)tmp*t)%mod;
       // cout<<"tmp     "<<tmp<<endl;
        if(flag)
            res = (res-tmp)%mod;
        else
            res = (res+tmp)%mod;
           // cout<<"res     "<<res<<endl;
    }

    printf("%d\n", (res+mod)%mod);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
        work();
}

代码参考:

https://blog.csdn.net/xieshimao/article/details/6840731

https://blog.csdn.net/sf____/article/details/20791837

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/81661400
今日推荐