• 【LOJ #2085】「NOI2016」循环之美(莫比乌斯反演+杜教筛)


    传送门

    考虑对于一个kk进制循环小数xyfrac x y
    如果循环节为ll
    那么这个数乘上klk^l后小数部分不变

    那么就是xyxy=xklyxklyfrac x y-lfloorfrac x y floor=frac {xk^l} y-lfloorfrac {xk^l} y floor
    xxyy=xklxklyyx-lfloorfrac x y floor y=xk^l-lfloorfrac {xk^l} y floor y
    xxkl mod  yxequiv xk^l mod y
    kl1mod  yk^lequiv 1mod y
    考虑这个中ll有解的条件即gcd(k,y)=1gcd(k,y)=1
    gcd(x,y)=1gcd(x,y)=1
    所以就可以的到
    ans=S(n,m,k)=i=1nj=1m[gcd(i,j)=1][gcd(j,k)=1]ans=S(n,m,k)=sum_{i=1}^nsum_{j=1}^m[gcd(i,j)=1][gcd(j,k)=1]
    =i=1nj=1m[gcd(i,j)=1]dj,dkμ(d)=sum_{i=1}^nsum_{j=1}^m[gcd(i,j)=1]sum_{d|j,d|k}mu(d)
    枚举dd后可以得到
    =dkμ(d)i=1nj=1md[gcd(i,jd)=1]=sum_{d|k}mu(d)sum_{i=1}^{n}sum_{j=1}^{frac md}[gcd(i,jd)=1]
    =dkμ(d)i=1nj=1md[gcd(i,j)=1][gcd(i,d)=1]=sum_{d|k}mu(d)sum_{i=1}^{n}sum_{j=1}^{frac md}[gcd(i,j)=1][gcd(i,d)=1]
    =dkμ(d)S(md,n,d)=sum_{d|k}mu(d)S(frac md ,n,d)
    然后就可以愉快的递归了
    k=1k=1时可以简单莫反后用杜教筛算μmu前缀和整除分块
    考虑n,mn,m最多只有nsqrt n个取值
    所以复杂度为O(nσ0(k)+n23)O(sqrt nsigma_0(k)+n^{frac 2 3})

    #include<bits/stdc++.h>
    using namespace std;
    #define cs const
    #define re register
    #define pb push_back
    #define pii pair<int,int>
    #define ll long long
    #define fi first
    #define se second
    #define bg begin
    cs int RLEN=1<<20|1;
    inline char gc(){
        static char ibuf[RLEN],*ib,*ob;
        (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
        return (ib==ob)?EOF:*ib++;
    }
    inline int read(){
        char ch=gc();
        int res=0;bool f=1;
        while(!isdigit(ch))f^=ch=='-',ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
    template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
    //cs int mod=1e9+7;
    //inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
    //inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
    //inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
    //inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
    //inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
    //inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
    //inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
    //inline int Inv(int x){return ksm(x,mod-2);}
    cs int N=1000005;
    int mu[N],pr[N],tot;
    bitset<N>vis;
    inline void init(cs int len=N-5){
    	mu[1]=1;
    	for(int i=2;i<=len;i++){
    		if(!vis[i])pr[++tot]=i,mu[i]=-1;
    		for(int j=1,p;j<=tot&&i*pr[j]<=len;j++){
    			p=i*pr[j],vis[p]=1;
    			if(i%pr[j]==0)break;
    			mu[p]=-mu[i];
    		}
    	}
    	for(int i=1;i<=len;i++)mu[i]+=mu[i-1];
    }
    map<int,int> S;
    inline int summu(int n){
    	if(n<=N-5)return mu[n];
    	if(S.count(n))return S[n];
    	int ret=1;
    	for(int i=2,j;i<=n;i=j+1){
    		j=n/(n/i);
    		ret-=(j-i+1)*summu(n/i);
    	}
    	return S[n]=ret;
    }
    struct node{
    	int n,m,k;
    	node(int a=0,int b=0,int c=0):n(a),m(b),k(c){}
    	friend inline bool operator <(cs node &a,cs node &b){
    		return a.n==b.n?(a.m==b.m?(a.k<b.k):a.m<b.m):a.n<b.n;
    	}
    };
    map<node,ll> s;
    int n,m,k,fc[2005],cnt;
    inline ll calc(int n,int m){
    	ll ret=0;if(n>m)swap(n,m);
    	for(int i=1,j;i<=n;i=j+1){
    		j=min(n/(n/i),m/(m/i));
    		ret+=1ll*(summu(j)-summu(i-1))*(n/i)*(m/i);
    	}
    	return ret;
    }
    inline ll calc(int n,int m,int k){
    	if(!n)return 0;
    	if(!m)return 0;
    	node p=node(n,m,k);
    	if(s.count(p))return s[p];
    	if(k==1)return s[p]=calc(n,m);
    	ll ret=0;
    	for(int i=1,d;i<=cnt&&fc[i]<=k;i++)
    	if(k%(d=fc[i])==0)ret+=(mu[d]-mu[d-1])*calc(m/d,n,d);
    	return s[p]=ret;
    }
    int main(){
    	#ifdef Stargazer
    	freopen("lx.in","r",stdin);
    	#endif
    	init();
    	n=read(),m=read(),k=read();
    	for(int i=1;i<=k;i++)if(mu[i]!=mu[i-1]&&k%i==0)fc[++cnt]=i;
    	cout<<calc(n,m,k)<<'
    ';
    }
    
  • 相关阅读:
    IIS常见500错误解决方案
    发送邮件代码
    IIS站点/虚拟目录中访问共享目录(UNC)
    简简单单,一目了然C#与Matlab
    [转载]C#——DataGridView分页功能的实现
    博客之旅
    ASP.Net, Php , Java/Java EE?好困惑
    【转载】DataGridView中虚拟模式(Virtual Mode)用法
    selenium4.0降级为3版本
    web自动化中影响页面定位的场景有哪些?
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328322.html
Copyright © 2020-2023  润新知