您的位置:首页 > 其它

ZOJ 3547 - The Boss on Mars(容斥)

2015-08-10 17:01 260 查看

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有相同质因子的数,这样减肯定会减重,所以要用容斥

[code]#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: