码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 920G - List Of Integers

时间:2018-02-05 23:30:29      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:color   容斥   namespace   ack   ini   二分   define   tor   ios   

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

标签:color   容斥   namespace   ack   ini   二分   define   tor   ios   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!