51Nod-1190 最小公倍数之和V2(莫比乌斯反演)


51Nod-1190 最小公倍数之和V2






#include<bits/stdc++.h>
#define me(a,x) memset(a,x,sizeof(a))
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
#define sc scanf
#define itn int
#define STR clock_t startTime = clock();
#define END clock_t endTime = clock();cout << double(endTime - startTime) / CLOCKS_PER_SEC *1000<< "ms" << endl;
using namespace std;
const int N=2e6+5;
const long long mod=1e9+7;
const int inv2=mod+1>>1;
const long long mod2=998244353;
const int oo=0x7fffffff;
const int sup=0x80000000;
typedef long long ll;
typedef unsigned long long ull;
template <typename it>void db(it *begin,it *end){while(begin!=end)cout<<(*begin++)<<" ";puts("");}
template <typename it>
string to_str(it n){string s="";while(n)s+=n%10+'0',n/=10;reverse(s.begin(),s.end());return s;}
template <typename it>int o(it a){cout<<a<<endl;return 0;}
inline ll mul(ll a,ll b,ll c){ll ans=0;for(;b;b>>=1,a=(a+a)%c)if(b&1)ans=(ans+a)%c;return ans;}
inline ll ksm(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=mul(a,a,c))if(b&1)ans=mul(ans,a,c);return ans;}
inline void exgcd(ll a,ll b,ll &x,ll &y){if(!b)x=1,y=0;else exgcd(b,a%b,y,x),y-=x*(a/b);}
int prime[N],tot=0;
bool vis[N];
int f[N];
void pre(){
    f[1]=1;
    for(int i=2;i<N;i++){
        if(!vis[i])prime[++tot]=i,f[i]=1-i;
        for(int j=1;j<=tot&&i*prime[j]<N;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0){
                f[i*prime[j]]=f[i];
                break;
            }else f[i*prime[j]]=f[i]*f[prime[j]];
        }
    }
}
vector<int>dis;
int cnt;
int p[N],len[N];
void dfs(int res,int pos){
    if(pos>=cnt){
        dis.push_back(res);
        return;
    }
    int x=1;
    dfs(res,pos+1);
    for(int i=0;i<len[pos];i++){
        x*=p[pos];
        dfs(res*x,pos+1);
    }
}
ll cal(int m,int n){
    int sz=dis.size();
    ll ans=0;
    for(int x=0;x<sz;x++){
        int T=dis[x];
        if(T<N){
            ans+=1LL*(m/T)*(m/T+1)/2%mod*f[T];
            ans=(ans+mod)%mod;
        }
                else{
                    int k=T;
                    int res=1;
                    for(int i=1;i<=tot&&1LL*prime[i]*prime[i]<=k;i++){
                        if(k%prime[i]==0){
                            res*=1-prime[i];
                            while(k%prime[i]==0)k/=prime[i];
                        }
                    }
                    if(k>1)res*=1-k;
                    ans+=1LL*(m/T)*(m/T+1)/2%mod*res;
                    ans=(ans+mod)%mod;
                }

    }
    return ans*n%mod;
}
ll qp(ll a,ll b){
    ll ans=1;
    for(;b;b>>=1,a=a*a)if(b&1)ans=ans*a;
    return ans;
}
int main(){
    //STR IN
    pre();
    int t;cin>>t;
    while(t--){
        int m,n;
        sc("%d%d",&m,&n);
        ll ans=1,k=n;
        cnt=0;
        dis.clear();
        for(int i=1;i<=tot&&1LL*prime[i]*prime[i]<=n;i++){
            if(n%prime[i]==0){
                p[cnt++]=prime[i];
                len[cnt-1]=0;
                int k=0;
                while(n%prime[i]==0)n/=prime[i],k++,len[cnt-1]++;
                ll res=1;
                ll x=qp(prime[i],k);
                x=(x+1)*(x-1)/(prime[i]+1)%mod;
                x=x*prime[i]%mod;
                res+=x;
                if(res>=mod)res%=mod;
                ans=ans*res;
                if(ans>=mod)ans%=mod;
            }
        }
        if(n>1)ans=ans*((1LL*n*n-n+1)%mod)%mod,p[cnt++]=n,len[cnt-1]=1;
        dfs(1,0);
        ans=(ans+1)*k%mod*inv2%mod;
        ans-=cal(m,k);
        ans+=1LL*m*k/__gcd(1LL*m,k);
        printf("%lld\n",(ans+mod)%mod);
    }
   // END
}

全部评论

相关推荐

Noel_:中石油是这样的 哥们侥幸混进免笔试名单 一看给我吓尿了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务