欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

The Boss on Mars(容斥原理)

程序员文章站 2024-02-11 13:40:28
...
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.逆元有关知识:
The Boss on Mars(容斥原理)
The Boss on Mars(容斥原理)
The Boss on Mars(容斥原理)
The Boss on Mars(容斥原理)
#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;
}


相关标签: 数论 容斥原理