The Boss on Mars(四次方和加容斥定理)

Problem Description

On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger boss.

Due to no moons around Mars, the employees can only get the salaries per-year. There are n employees in ACM, and it’s time for them to get salaries from their boss. All employees are numbered from 1 to n. With the unknown reasons, if the employee’s work number is k, he can get k^4 Mars dollars this year. So the employees working for the ACM are very rich.

Because the number of employees is so large that the boss of ACM must distribute too much money, he wants to fire the people whose work number is co-prime with n next year. Now the boss wants to know how much he will save after the dismissal.

Input

The first line contains an integer T indicating the number of test cases. (1 ≤ T ≤ 1000) Each test case, there is only one integer n, indicating the number of employees in ACM. (1 ≤ n ≤ 10^8)

Output

For each test case, output an integer indicating the money the boss can save. Because the answer is so large, please module the answer with 1,000,000,007.

Sample Input

 

2 4 5

Sample Output

 

82 354

Hint

Case1: sum=1+3*3*3*3=82 Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354

Author

ZHANG, Chao

Source

2011 Asia Dalian Regional Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  4056 4053 4057 4052 4054 

题意:求[1, n]中与n互质的数的四次方和。

思路:四次方和不好算,不可能预处理每个数的4次方,空间和时间都不允许。

1^4 + 2^4 + ... + n^4 这样的式子还是有公式的。其结果为 n(n+1)(2n+1)(3n^2+3n+1)/30。

推理:

(n+1)^5-n^5=5n^4+10n^3+10n^2+5n+1

n^5-(n-1)^5=5(n-1)^4+10(n-1)^3+10(n-1)^2+5(n-1)+1

(n-1)^5-(n-2)^5=5(n-2)^4+10(n-2)^3+10(n-2)^2+5(n-2)+1
 

...

...

...

2^5-1^5=5*1^4+10*1^3+10*1^2+5*1+1

需要用到的公式有

1^2+2^2+...+n^2=n*(n+1)*(2*n+1)/6;

1^3+2^3+...+n^3=n^2*(n+1)^2/4;

然后全部相加起来,经行化简 就可以就可以得到f(n)

所以,我们可以通过容斥原理把1~n与n不互质的数求出来,求出它们四次方和,再用总的减去即可。

如何求跟不互质的数,跟以前一样,把n的素因子枚举出来,容斥原理做,

 假定n存在2这样的素因子,那么n与2,4,6,8。。。这样的数都不互质。我们可以求出2,4,6,8,。。。的四次方和。

 简单观察,可以发现,2^4+4^4+6^4+8^4+... = 2^4 * ( 1^4 + 2^4 + 3^4 + 4^4... ),仍然可以使用前n次方和的公式计算。

因为要取模,有除法因此我们需要求逆元,这里用费马小定理求逆元

注意:算一下,就要取模,不然答案会错

   还有n可能等于1/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll fac[1005];
int t;
ll n,cnt,inv;
void fenjie(ll x)
{cnt =0;
    for(ll i=2;i*i<=x;i++)
    if(x%i==0)
    {fac[cnt++]=i;
        while(x%i==0)
        x/=i;
    }
    if(x>1)
    fac[cnt++]=x;

}
ll cal(ll x)
{
    ll ans=x;
    ans=(ans*(x+1))%mod;
    ans=(ans*(2*x+1))%mod;
    ans=(ans*((3*x*x+3*x-1)%mod))%mod;
    ans=(ans*inv)%mod;
return ans;
}

ll qw(ll a,ll b,ll m)
{
    ll ans=1;

    while(b)
    {
        if(b&1)
        {
            ans=ans*a%m;

        }
        b>>=1;
        a=a*a%m;
    }
    return ans;
}
ll count(ll n)
{
    ll sum=0;
    for(ll i=1;i<(1<<cnt);i++)
    {
        ll f=0,val=1;
        for(ll j=0;j<cnt;j++)

        if(i&(1<<j))
        {
            f++;
            val*=fac[j];
        }
        ll t=cal(n/val)%mod;
         val=(val*val)%mod;
         val=(val*val)%mod;
         val=(val*t)%mod;
         if(f&1)
         sum=(sum+val)%mod;
         else
         sum=(sum-val)%mod;
             }
             return (cal(n)-sum+mod)%mod;
}
int main()
{
    scanf("%d",&t);
    inv=qw(30,mod-2,mod);
    for(int i=1;i<=t;i++)
    {
        scanf("%lld",&n);
        if(n==1)
        {
            printf("0\n");
            continue;
        }
        fenjie(n);
        printf("%lld\n",count(n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/81660826
今日推荐