zoukankan      html  css  js  c++  java
  • hdu 6053 TrickGCD (莫比乌斯)

    题意:给你一个序列a,求序列b满足 ①1<=bi<=ai ②对于序列b[l,r] gcd(bl,...,br)>=2的序列数是多少

    思路:显然这道需要我们枚举gcd

              设F(d)是序列b满足gcd是d的倍数的个数

              F(d)的结果显而易见为∏a[i]/d

              根据容斥原理ans=F(2)+F(3)+F(5)-F(6)+F(7)-F(10).....

              根据莫比乌斯,我们可以把它写成ans=∑d-u(d)∏a[i]/d

              log(n^2)的时间复杂度太高,我们需要进行分块处理

              设f(i,d)是数组a中a/d=i的个数

              那么ans=∑d=2-u(d)∏i=1if(i,d),再利用快速幂计算

              时间复杂度降为log(n*log(n)^2)

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #define ll long long
    using namespace std;
    
    const ll mod = 1e9+7;
    const ll maxn = 2e5+10;
    bool check[maxn];
    ll prime[maxn],mu[maxn],sum[maxn];
    ll data[maxn],cnt[maxn];
    
    void Moblus()
    {
        memset(check,false,sizeof(check));
        mu[1]=1;
        int tot=0;
        for(int i=2;i<maxn;i++)
        {
            if(check[i]==false)
            {
                prime[tot++]=i;
                mu[i]=-1;
            }
            for(int j=0;j<tot;j++)
            {
                if(i*prime[j]>maxn) break;
                check[i*prime[j]]=true;
                if(i%prime[j]==0)
                {
                    mu[i*prime[j]]=0;
                    break;
                }
                else
                {
                    mu[i*prime[j]]=-mu[i];
                }
            }
        }
    }
    
    ll qmod(ll a,ll b)
    {
        ll ans=1;
        a=a%mod;
        while(b)
        {
            if(b&1)
            {
                ans=(ans*a)%mod;
            }
            b=b/2;
            a=(a*a)%mod;
        }
        return ans;
    }
    
    int main()
    {
        int t,cas=1,n;
        Moblus();
        cin>>t;
        while(t--)
        {
            cin>>n;
            ll minn=maxn+10;
            ll maxx=-1;
            memset(cnt,0,sizeof(cnt));
            memset(sum,0,sizeof(sum));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&data[i]);
                minn=min(data[i],minn);
                maxx=max(data[i],maxx);
                cnt[data[i]]++;
            }
            for(int i=1;i<maxn;i++)
            {
                sum[i]=sum[i-1]+cnt[i];
            }
            ll ans=0;
            for(ll i=2;i<=minn;i++)
            {
                ll tmp=1;
                for(ll j=1;j*i<=maxx;j++)
                {
                    tmp=tmp*qmod(j,sum[i*j+i-1]-sum[i*j-1])%mod;
                }
                ans=(ans-mu[i]*tmp+mod)%mod;
            }
            cout<<"Case #"<<cas++<<": "<<ans<<endl;
        }
    }
  • 相关阅读:
    Android总结之json解析(FastJson Gson 对比)
    Android性能优化之UncaughtExceptionHandler定制自己的错误日志系统
    IOS遍历网页获取网页中<img>标签中的图片url
    IOS各种集合遍历效率对比
    cx_oracle访问处理oracle中文乱码问题
    使用tar解压文件提示gzip: stdin: not in gzip format错误
    Mac安装crfpp
    oracle 常用操作
    docker启动centos7后sudo不能使用
    常见Python爬虫工具总结
  • 原文地址:https://www.cnblogs.com/simplekinght/p/7274725.html
Copyright © 2011-2022 走看看