HDU - 4059 The Boss on Mars - (容斥定理,1~n的4次方之和)

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让求1~n中与n互素的数的4次方之和,结果模1,000,000,007

这里可以先求出总和再减去不互素的,因为可以通过对n分解质因数,快速找到与n不互素的数,

这里减去不互素额的方法就是利用容斥定理:先减去所有质因子的4次方和,再加上是两个质因子的倍数的4次方和(显然这里质因子之间是互素的,所以最小公倍数即是两者之积),再减去三个质因子的倍数的4次方之和……

这里容斥定理用位运算和dfs均可

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define mod 1000000007

vector<ll>prime;
ll len,n,ans;

ll pow_mod(ll n,ll k)
{
    ll res=1;
    while(k>0)
    {
        if(k&1)
            res=res*n%mod;
        n=n*n%mod;
        k>>=1;
    }
    return res;
}

ll sum(ll x) //求1~n的4次方之和余mod,用到除法求逆元
{
    ll t1 = (x * (x + 1) % mod) * ((2 * x + 1) % mod);
    ll t2 = ((3 * x * x + 3 * x - 1) % mod) * pow_mod(30ll, mod - 2);
    return ((t1 % mod) * (t2 % mod)) % mod;
}

ll solve() //位运算解决
{
    ll ans=0;
    for(ll msk=1;msk<(1ll<<len);msk++)
    {
        ll bits=0,cnt=1;
        for(ll i=0;i<len;i++)
        {
            if(msk&(1<<i))
            {
                bits++;
                cnt*=prime[i];
            }
        }
        if(bits&1)
        {
            ans+=pow_mod(cnt,4)*sum(n/cnt); //pow_mod(cnt,4)*sum(n/cnt)是所有cnt的倍数的四次方和
            if(ans>=mod)
                ans%=mod;
        }else{
            ans-=pow_mod(cnt,4)*sum(n/cnt);
            if(ans>=mod)
                ans%=mod;
        }
    }
    return ans;
}

/*void dfs(ll pos,ll pre,int sgin) //dfs解决
{
    if(pos>=len||pre>n)
        return;
    ll t=1,cur=pre*prime[pos];
    for(int i=0;i<4;i++)
        t=(t*cur)%mod;
    t=(t*sum(n/cur))%mod;
    ans=((ans%mod)+(sgin*t)%mod+mod)%mod;
    if(n%cur==0)
        dfs(pos+1,cur,-sgin);
    dfs(pos+1,pre,sgin);
}*/

int main()
{
    int T;
    ll i;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        prime.clear();
        
        //分解质因数
        ll tn=n;
        for(i=2;i*i<=tn;i++)
        {
            if(tn%i==0)
            {
                prime.push_back(i);
                while(tn%i==0)
                    tn=tn/i;
            }
        }
        if(tn!=1)
            prime.push_back(tn);
            
        len=prime.size();
        ans=sum(n);

        //dfs(0,1,-1);
        ans=((ans-solve())%mod+mod)%mod;

        printf("%lld\n",ans);
    }

    return 0;
}

参考:求1~n中与n互素的数的4次方之和


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值