The Boss on Mars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1934 Accepted Submission(s): 580
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.
題解及代碼:
這道題目的綜合性還是很強的。首先說一下題目,就是求小於n並且與n互素的數的四次方的和。
說一下思路吧:首先我們求出1---n-1的所有的數的四次方的和,之後將n進行素因子分解,求出n的所有因子,然後減去包含這些因子的數的四次方就可以了。
大體上的思路有了,來處理一下細節:1.首先我們要求出四次方和的公式 2.素數打表 3.求逆元(因爲四次方和公式有一個分母,取餘時要乘上逆元)
4.素因子分解 5.容斥原理
搞定這5步,我們這道題就能做了,所以說綜合性非常強。
具體見代碼吧:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const long long mod=1000000007,q=233333335;//p爲逆元,用費馬小定理求出
bool prime[10010];
int p[1400];
int k=0;
//四次方和計算公式
long long cal(long long n)
{
if(n==0) return 0;
return (n*(n+1)%mod*(2*n+1)%mod)%mod*((3*n*n+3*n-1)%mod*q%mod)%mod;
}
//容斥原理
void dfs(int base,int num_p,long long n,long long m,long long nt,long long mu,long long &sum,long long tab_p[])
{
if(nt==m)
{
long long b=n/mu;
if(m%2==0)
{
sum=(sum-mu*mu%mod*mu%mod*mu%mod*cal(b)%mod+mod)%mod;
}
else
{
sum=(sum+mu*mu%mod*mu%mod*mu%mod*cal(b)%mod)%mod;
}
return;
}
for(long long i=base; i<num_p; i++)
{
dfs(i+1,num_p,n,m,nt+1,mu*tab_p[i],sum,tab_p);
}
}
//素數打表
void isprime()
{
long long i,j;
memset(prime,true,sizeof(prime));
prime[0]=prime[1]=false;
for(i=2; i<10010; i++)
{
if(prime[i])
{
p[k++]=i;
for(j=i*i; j<10010; j+=i)
prime[j]=false;
}
}
}
int main()
{
isprime();
long long n,ans,tab_p[1400];
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%I64d",&n);
n=n-1;
ans=cal(n);
long long m=n,t=n+1;
int num_p=0;
for(int i=0; i<k&&p[i]*p[i]<=t; i++) //素因子分解
if(t%p[i]==0)
{
tab_p[num_p++]=p[i];
while(t%p[i]==0)
{
t/=p[i];
}
}
if(t>1) tab_p[num_p++]=t;
/*//輸出測試
for(int i=0;i<num_p;i++)
{
printf("%d ",tab_p[i]);
}
puts("");
//測試結束
*/
long long sum=0;
for(int i=0; i<num_p; i++) //將不互素的部分減去
{
n=m/tab_p[i];
sum=(sum+tab_p[i]*tab_p[i]%mod*tab_p[i]%mod*tab_p[i]%mod*cal(n))%mod;
}
for(long long i=2; i<=num_p; i++) //容斥部分求解
dfs(0,num_p,m,i,0LL,1LL,sum,tab_p);
printf("%I64d\n",(ans-sum+mod)%mod);
}
return 0;
}