HDU 4059 The Boss on Mars(容斥原理)

The Boss on Mars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2494    Accepted Submission(s): 775


Problem Description
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
这道题目是求小于n和n互质的数的四次方的和
利用容斥原理,可以用总和减去不和n互质的和
也可以直接求互质的数的和
这里还涉及到了sum{1^4,2^4,3^4....n^4}求和公式
n*(n+1)*(2*n+1)*((3*n*n+3*n-1)/30
求和公式里有/ 那么在用同余定理的时候就要用逆元
b/c(mod m)=b(mod m)* c^(m-2)(mod m)
证明略
 
   
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
typedef long long int LL;
#define MAX 1000000
LL prime[MAX+5];
LL sprime[MAX+5];
bool check[MAX+5];
const LL mod=1e9+7;
LL INF;
LL cnt;
LL quick(LL x,LL a)
{
    LL sum=1;
    for(x;x>0;x>>=1)
    {
        if(x&1) 
			sum=(sum*a)%mod;
         a=(a*a)%mod;
    }
    return sum;
}
LL sum1(LL n)
{
    INF=quick(mod-2,30);
    return n*(n+1)%mod*(2*n+1)%mod*((3*n*n%mod+3*n-1)%mod)%mod*INF%mod;
}
LL sum2(LL n)
{
    return (((n*n)%mod*n)%mod*n)%mod;
}
void eular()
{
    memset(check,false,sizeof(check));
    int tot=0;
    for(LL i=2;i<MAX+5;i++)
    {
        if(!check[i])
            prime[tot++]=i;
        for(int j=0;j<tot;j++)
        {
            if(i*prime[j]>MAX+5) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
}
void Divide(LL n)
{
    cnt=0;
    LL t=(LL)sqrt(n*1.0);
    for(LL i=0;prime[i]<=t;i++)
    {
        if(n%prime[i]==0)
        {
            sprime[cnt++]=prime[i];
            while(n%prime[i]==0)
                n/=prime[i];
        }
    }
    if(n>1)
        sprime[cnt++]=n;
}
int main()
{
    eular();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        LL n;
        scanf("%lld",&n);
        Divide(n);
  
        LL ans=0;
        LL res=sum1(n);
        for(int i=1;i<((LL)(1<<(cnt)));i++)
        {
            int num=0;
            LL tmp=1;
            for(int j=0;j<cnt;j++)
            {
                if(i&(1<<j))
                {
                    num++;
                    tmp*=sprime[j];
                }
            }
            if(num&1)
                ans=(ans+(sum1(n/tmp)*sum2(tmp))%mod)%mod;
            else
                ans=((ans-(sum1(n/tmp)*sum2(tmp))%mod)%mod+mod)%mod;
        }
        printf("%lld\n",(res-ans+mod)%mod);
    }
    return 0;
}



转载于:https://www.cnblogs.com/dacc123/p/8228663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值