ZOJ 3547 - The Boss on Mars(容斥)

The Boss on Mars

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
思路:首先对N分解质因子,10^8以内最对8个质因子,然后减去跟N有相同质因子的数,这样减肯定会减重,所以要用容斥

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=110;
const int maxm=1010;
const LL MOD=1e9+7;
const int INF=0x3f3f3f3f;
const int inv30=233333335;
int N,cnt;
int a[maxn];
bool is_prime(int x)
{
    if(x==1)return false;
    int k=sqrt(x);
    for(int i=2;i<=k;i++)
        if(x%i==0)return false;
    return true;
}
void process(int n)
{
    cnt=0;
    int k=sqrt(n);
    for(int i=2;i<=k;i++)
    {
        if(n==1)break;
        if(n%i)continue;
        a[cnt++]=i;
        while(n%i==0)n/=i;

    }
    if(is_prime(n)){a[cnt++]=n;}
}
LL getsum(LL x)
{
    LL sum=x;
    sum=(sum*(x+1))%MOD;
    sum=(sum*(2*x+1))%MOD;
    sum=((3*x*x%MOD+3*x)%MOD-1+MOD)%MOD*sum%MOD;
    return sum;
}
LL cal(LL x)
{
    LL num=N/x;
    LL sum=getsum(num);
    sum=(x*x%MOD*x%MOD*x%MOD*sum)%MOD;
    return sum;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        process(N);
        LL ans=getsum(N);
        for(int i=0;i<(1<<cnt);i++)
        {
            int num=0;
            LL p=1;
            for(int j=0;j<cnt;j++)
                if(i&(1<<j))p*=a[j],num++;
            if(num==0)continue;
            if((num&1)==0)(ans+=cal(p))%=MOD;
            else ans-=cal(p),ans=(ans+MOD)%MOD;
        }
        printf("%lld\n",(ans*inv30)%MOD);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值