首页 > 其他 > 详细

Codeforces 920G - List Of Integers

时间:2018-02-05 22:48:38      阅读:270      评论:0      收藏:0      [点我收藏+]

920G - List Of Integers

思路:容斥+二分

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

vector<int>p;
ll ans=0;
void dfs(int k,int cnt,ll s,ll r){
    if(k==p.size()){
        if(cnt&1)ans-=r/s;
        else ans+=r/s;
        return ; 
    }
    dfs(k+1,cnt,s,r);
    dfs(k+1,cnt+1,s*p[k],r);
}
void init(ll n){
    p.clear();
    for(ll i=2;i*i<=n;i++){
        if(n%i==0){
            p.pb(i);
            while(n%i==0)n/=i;
        }
    }
    if(n>1)p.pb(n);
}
ll count(ll r){
    ans=0;
    dfs(0,0,1,r);
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    int x,p,k;
    cin>>T;
    while(T--){
        cin>>x>>p>>k;
        init(p);
        ll t=count(x);
        ll l=x,r=1e18,m=(l+r)>>1;
        while(l<r){
            if(count(m)-t>=k)r=m;
            else l=m+1;
            m=(l+r)>>1;
        }
        cout<<m<<endl;
    }
    return 0;
} 

 

Codeforces 920G - List Of Integers

原文:https://www.cnblogs.com/widsom/p/8419339.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!