Sum of gcd of Tuples (Hard)

E - Sum of gcd of Tuples (Hard)

暴力解肯定是不行的,那么要想到的是,可以给它分一个类。根据gcd的值,找到对应的有多少数对。

但是会发生重复的情况,例如gcd=2gcd=4的时候,会有重复,所以需要用到容斥的思想。

// Created by CAD on 2020/4/19.
#include <bits/stdc++.h>

#define ll long long
using namespace std;

const int mod=1e9+7;
const int maxn=1e5+5;
ll qpow(ll x,ll n){
    ll ans=1;
    while(n>0){
        if(n&1) ans=ans*x%mod;
        n>>=1,x=x*x%mod;
    }
    return ans;
}
ll sum[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n,k;cin>>n>>k;
    ll ans=0;
    for(int i=k;i>=1;--i){
        sum[i]=qpow(k/i,n);
        for(int j=2*i;j<=k;j+=i) sum[i]-=sum[j];
        ans=(ans+1ll*i*sum[i]%mod)%mod;
    }
    cout<<ans<<"\n";
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/CADCADCAD/p/12730890.html