0
点赞
收藏
分享

微信扫一扫

Sum of gcd of Tuples (Hard)

​​E - Sum of gcd of Tuples (Hard)​​

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

但是会发生重复的情况,例如​​gcd=2​​​和​​gcd=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;
}

CAD加油!欢迎跟我一起讨论学习算法



举报

相关推荐

0 条评论