您的位置:首页 > 其它

ZOJ 3547 The Boss on Mars 2011大连现场赛I题 数论

2011-10-02 18:26 344 查看
The Boss on Mars

Time Limit: 2 Seconds
Memory Limit: 65536 KB

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互质的数的性质

比如12=2*2*3

那么与12不互质的数就有2,3,4,6,8,9,10,12

其实就是2的所有倍数,以及3的所有倍数

所以可以先求一个1到12的所有数的四次方和。这个有公式:

n*(n+1)*(6*n*n*n+9*n*n+n-1)/30(先开始就是抄错了。。)

注意这里30最好求一下逆元大概是2000多万

求的所有的四次方和之后当然要减去那些不互质的数的四次方

也就是说分别剪去了2和3的倍数的四次方,注意这里2和3的公倍数被多减去了,所以要加回来

所以容斥原理也要用

我的代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>

using namespace std;

typedef long long ll;

ll mod=1000000007;
ll prime[10005];
bool flag[10005];
ll re,ni;
ll M=mod;
vector<ll>link;

ll power(ll p,ll n,ll m)
{
ll sq=1;
while(n>0)
{
if(n&1)
sq=(sq%m)*(p%m)%m;
p=(p%m)*(p%m)%m;
n=n/2;
}
return sq%m;
}

void init()
{
int i,j,num=0;
for(i=2;i<=10000;i++)
{
if(!flag[i])
{
prime[num++]=i;
for(j=i*i;j<=10000;j=j+i)
flag[j]=true;
}
}
re=power(30,mod-2,mod);
ni=re;
}

//n*(n+1)*(6*n*n*n+9*n*n+n-1)/30

ll sum(ll n)//四次方求和
{
ll a,b,c,d,e,f,g,h,i,j;
a=n%mod;
b=(n+1)%mod;
c=6*n%mod;
d=c*n%mod;
d=d*n%mod;
e=9*n%mod;
f=e*n%mod;
g=(a*b)%mod;
h=(d+f-1+n)%mod;
i=g*h%mod;
j=i*re%mod;
return j;
}

ll dfs(ll x,ll n)//容斥原理
{
ll i,res=0,tmp;
for(i=x;i<link.size();i++)
{
tmp=link[i];
res=res+sum(n/tmp)*(tmp%mod*tmp%mod*tmp%mod*tmp%mod)%mod;
res=((res-dfs(i+1,n/tmp)*(tmp%mod*tmp%mod*tmp%mod*tmp%mod)%mod)%mod+mod)%mod;
}
return res%mod;
}

bool judge(ll n)
{
ll i;
for(i=0;prime[i]*prime[i]<=n;i++)
if(n%prime[i]==0)
return false;
return true;
}

int main()
{
ll n,i,t,N,ans,T;
init();
scanf("%lld",&T);
for(t=1;t<=T;t++)
{
scanf("%lld",&n);
if(judge(n))
{
printf("%lld\n",sum(n-1));
continue;
}
N=n,link.clear();
for(i=0;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==0)
{
n=n/prime[i];
link.push_back(prime[i]);
while(n%prime[i]==0)
n=n/prime[i];
}
if(n==1)
break;
}
if(n>1)
link.push_back(n);
n=N;
ans=((sum(n)-dfs(0,n))%mod+mod)%mod;
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: