前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 4059 The Boss on Mars(容斥原理)

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

作者头像
ShenduCC
发布2018-04-26 17:16:16
5660
发布2018-04-26 17:16:16
举报
文章被收录于专栏:算法修养算法修养

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

代码语言:javascript
复制
2
4
5

Sample Output

代码语言:javascript
复制
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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • The Boss on Mars
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档