capacity 2773

(Binary + inclusion-exclusion principle)
that Italy: first demand k(1k100000000) And a m(1m1000000) Relatively prime number is how much?

Ideas: half k Each time to find less than k And and m Relatively prime numbers how many, put more k Smaller, less put k amplification. Review under way here inclusion and exclusion of two way: iterative method and recursion. (Certainly must pay attention to what Potentially explosive int!)

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long

using namespace std;
const double eps = 1e-8;

bool vis[100100];
LL fac[100100];

/*LL solve(LL x, LL cnt){
    LL ret = 0;
    for(LL i=1; i<(1<<cnt); i++){
        LL bits = 0, mul = 1;
        for(LL j=0; j<cnt; j++)if(i&(1<<j)){
            bits ++; mul *= fac[j];
        }
        if(bits&1) ret += x/mul;
        else ret -= x/mul;
    }
    return x - ret;
}*/

LL solve(LL m, LL x, LL cnt){
    LL ret = 0;
    for(int i=x; i<cnt; i++){
        ret += m/fac[i] - solve(m/fac[i], i+1, cnt);
    }
    return ret;
}

int main()
{
    //freopen("test.txt","r",stdin);
    LL m, k;
    while(scanf("%lld%lld",&m,&k) == 2){
        LL cnt = 0, tm = m;
        for(LL i=2; i*i<=tm; i++) if(tm%i == 0){
            fac[cnt++] = i;
            while(tm%i == 0) tm /= i;
        }
        if(tm > 1) fac[cnt++] = tm;
        LL l = 1, r = 1000000000000;
        while(l < r){
            LL mid = (r-l)/2 + l;
            //if(solve(mid, cnt) >= k) r = mid;
            if(mid - solve(mid, 0, cnt) >= k) r = mid;
            else l = mid + 1;
        }
        printf("%lld\n",l);
    }
    return 0;
}
Published 40 original articles · won praise 44 · views 90000 +

Guess you like

Origin blog.csdn.net/Site1997/article/details/78786916