您的位置:首页 > 其它

ZOJ 3547 The Boss on Mars(容斥)

2015-09-12 00:13 393 查看
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*(n+1)*(6*n*n*n+9*n*n+n-1)/30

有了这个我们肯定要算和n不互斥的和,然后减一下就行了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+7;
const int p=233333335;
int v[10];
//n*(n+1)*(6*n*n*n+9*n*n+n-1)/30
int t,n,m;
LL work(LL x)
{
    LL s1=x*(x+1)%mod;
    LL s2=6LL*x*x%mod*x%mod;
    LL s3=(9LL*x*x%mod+x-1)%mod;
    LL s=(s1*((s2+s3)%mod)%mod)*p%mod;
    return s;
}
LL Cal(LL x)
{
    LL s=x*x%mod*x%mod*x%mod;
    LL l=n/x;
    return s*work(l)%mod;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        m=0;int x=n;
        for(int i=2;i*i<=x;i++)
        {
            if(x%i==0)
            {
                while(x%i==0)
                    x/=i;
                v[m++]=i;
            }
        }
        if(x>1)
            v[m++]=x;
        LL ans=0;
        for(int i=1;i<(1<<m);i++)
        {
            LL c=0,l=1;
            for(int j=0;j<m;j++)
            {
                if(i&(1<<j))
                {
                    c++;
                    l*=v[j];
                }
            }
            if(c&1)
                ans=(ans+Cal(l))%mod;
            else
                ans=(ans-Cal(l)+mod)%mod;
        }
        LL all=work(n);
        ans=(all+mod-ans)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: