The Boss on Mars(容斥原理)

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
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的数字中与n没有素因子的数字的四次方和,并对 1,000,000,007.取余
思路:
1.得到n的四次方的通项公式n*(n+1)*(6*n*n*n+9*n*n+n-1)/30
2.由于通项公式中除30还要对1,000,000,007.取余,所以利用了逆元的求法
3.容斥原理寻找和n有同素因子的数字个数,然后再减去。
4.逆元有关知识:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const ll N=1e6+5;
const ll mod=1e9+7;
ll n,ans;
ll getmult(ll mult)
{
    return mult*mult%mod*mult%mod*mult%mod;
}
ll extgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0) {
        x=1,y=0;
    return a;
    }
    ll d= extgcd(b,a%b,x,y);
    ll t=x;
    x=y;
    y=t-a/b*y;
    return d;
}
ll inverse(ll t)
{
    ll x,y;
    ll d=extgcd(t,mod,x,y);
    return (x%mod+mod)%mod;
}
ll calu(ll n)
{
    return n*(n+1)%mod*(6*n*n%mod*n%mod+9*n*n%mod+n-1)%mod*inverse(30)%mod;
}
ll rongchi(ll n)
{
    ll m=n;
    vector<ll> v;
    for(ll i=2;i*i<=m;i++){
        if(m%i==0){
            v.push_back(i);
        while(m%i==0)
             m=m/i;
        }
    }
    if(m>1) v.push_back(m);
    ll len=v.size();
    for(ll i=1;i<(1<<len);i++)
    {
        ll mult=1,flag=0;
        for(ll j=0;j<len;j++)
        {
            if(i&(1<<j))
            {
                flag++;
                mult=mult*v[j];
            }
        }
        if(flag%2)
        {
            ans=ans-getmult(mult)*calu(n/mult);
            ans=(ans%mod+mod)%mod;
        }
        else
        {
            ans=ans+getmult(mult)*calu(n/mult);
            ans=(ans%mod+mod)%mod;
        }
      }
      v.clear();
      return ans;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ans=calu(n);
        cout<<rongchi(n)<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值