POJ 2773 Happy 2006 (two-point answer + allowance)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.