Title Link: http://poj.org/problem?id=2773
Test instructions
The number of K and M coprime is obtained;
Analysis:
It is obvious that as the number increases and the number of M coprime is greater, so we can answer two points,
In the middle, we need to use the principle of tolerance to find the number of coprime in [1,mid] and M.
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <vector>using namespace std; const int MAXN = 100;typedef long long ll;int m,k,cnt;int p[maxn];void fen (int m)//to M-factor decomposition {cnt=0; for (int i=2;i*i<=m;i++) {if (m%i==0) {p[cnt++]=i; while (m%i==0) m/=i; }} if (m>1) p[cnt++]=m;} ll CalU (ll N)//tolerance count {ll sum=0; for (ll i=1;i< (1<<cnt); i++) {ll mult=1,f=0; for (int j=0;j<cnt;j++) {if (i& (1<<j)) {f++; MULT*=P[J]; }} if (f&1) Sum+=n/mult; else Sum-=n/mult; } return n-sum;} int main () {while (~scanf ("%d%d", &m,&k)) {LL l=0,r= ((LL) 1<<62); LL mid,ans=0; Fen (m); while (l<=r) {//two points answer mid= (l+r)/2; LL sum = CalU (mid); if (sum>=k) {r=mid-1; if (sum==k) Ans=mid; } else l=mid+1; } printf ("%i64d\n", ans); } return 0;}
POJ 2773 Happy 2006 (two-point answer + allowance)