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


思路: 求1~n内与n互素的四次方之和,可以先利用公式求1~n的四次方和:1^4+2^4+3^4+......+n^4=n(n+1)(2n+1)(3n^2+3n-1)/30;然后在减去与n不互素的四次方之和(用容斥),这里要注意的是,因为n非常大,所以,直接除以30会导致答案错误,这时候就要用到逆元中的推论,设x为b/a模n的除数,若gcd(a,n)=1,则x=b/a (mod n)的解为x=b*a^(phi(n)-1);因为30^(phi(l)-1)%l=233333335,所以计算的时候可以把除以30转化为乘以233333335;计算的时候与x不互素的个数d=n/s;这时候又是一个1~d的四次方和公式;

代码如下:

#include<stdio.h>
#include<math.h>
long long p[10000],L=1000000007,m=233333335;
long long pp(int x)
{
    long long s=1;;
    for(int i=1;i<=4;i++)
        s=s*x%L;
        return s;
}
long long QH(long long n)
{
    return n*(n+1)%L*(2*n+1)%L*(3*(n*n)%L+3*n-1)%L*m%L;
}
int main()
{
    int t;
    long long n,w,x,S,cnt,y;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%lld",&n);
        S=QH(n);
        cnt=0;
        x=n;
        for(int j=2;j*j<=x;j++)
        {
            if(x%j==0)
            {
                p[cnt++]=j;
                while(x%j==0)
                    x=x/j;
            }
        }
        if(x>1)
            p[cnt++]=x;
        for(int j=1;j<(1<<cnt);j++)
        {
            long long s=1,sum=0;
            for(int k=0;k<cnt;k++)
            {
                if(j&(1<<k))
                {
                    sum++;
                    s=s*p[k];
                }
            }
            s=pp(s)*QH(n/s)%L;
            if(sum%2)
                S=(S-s)%L;
            else
                S=(S+s)%L;

        }
        printf("%lld\n",(S+L)%L);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值