zoukankan      html  css  js  c++  java
  • 920G.List of Integers(莫比乌斯函数+二分)

    题意:

    (sqrt)级的时间复杂度内求第k个大于x且与p互质的数。

    题解:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e6+100;
    int tot,vis[maxn],mu[maxn],sum[maxn],pr[maxn];
    void init (int n) {
    	mu[1]=1;
    	for (int i=2;i<=n;i++) {
    		if (!vis[i]) mu[i]=-1,pr[++tot]=i;
    		for (int j=1;j<=tot&&pr[j]*i<=n;j++) {
    			vis[pr[j]*i]=1;
    			if (i%pr[j]==0) {
    				mu[pr[j]*i]=0;
    				break;
    			}
    			mu[pr[j]*i]=-mu[i];
    		}
    	}
    	for (int i=1;i<=n;i++) sum[i]=sum[i-1]+mu[i];
    }
    int check (int x,int p) {
    	//求小于等于x的和p互质的数 
    	int ans=0;
    	for (int i=1;i<=sqrt(p);i++) {
    		if (p%i) continue;
    		ans+=mu[i]*(x/i);
    		if (i==p/i) continue;
    		ans+=mu[p/i]*(x/(p/i)); 
    	}
    	return ans;
    }
    int main () {
    	int t,x,p,k;
    	cin>>t;
    	init(1e6);
    	while (t--) {
    		cin>>x>>p>>k;
    		int l=x+k-1,r=1e7;
    		int ans=-1;
    		while (l<=r) {
    			int mid=(l+r)>>1;
    			if (check(mid,p)-check(x,p)>=k)
    				r=mid-1,ans=mid;
    			else
    				l=mid+1;
    		}
    		printf("%d
    ",ans);
    	}
    }
    
  • 相关阅读:
    子网掩码的作用与IP网段的划分
    DHCP服务器
    Anaconda安装、更新第三方包
    time模块的使用
    TensorFlow安装
    机器学习-线性回归
    机器学习
    Pyhton-类(2)
    python-类(1)
    Python-函数
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/14585533.html
Copyright © 2011-2022 走看看