POJ 2773 Happy 2006

Source: Internet
Author: User
Tags gcd

Happy 2006http://poj.org/problem?id=2773
Time limit:3000ms Memory limit:65536k

Description

The positive integers is said to being relatively prime to all other if the great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are-relatively prime to 2006.

Now your job is easy:for the given integer m, find the k-th element which was relatively prime to m when these elements ar e sorted in ascending order.

Input

The input contains multiple test cases. For each test case, it contains integers m (1 <= m <= 1000000), K (1 <= k <= 100000000).

Output

Output the k-th element in a single line.

Sample Input

2006 12006 22006 3

Sample Output

135

Source

POJ monthly--2006.03.26,staticTest Instructions: number of K for n coprimeproperty: If a, b coprime, then a+b,b coprime; If a b does not coprime, then a+b,b does not coprimeThis can be rolled out, with the number of n coprime having periodicity:that is [1,n] and n coprime number, each +n can get [n+1,2n] and n coprime number, can continue to promoteso we just need to ask for the number of coprime with N in the first cycle.then ans=n* the number of cycles + k%φ (n) and n coprimeso all the numbers in [1,n] and n coprime are required.There is a small deal: if K happens to be a multiple of the number of cycles, then the remainder is 0,So, special treatment, the number of cycles-1, and then in this week's period to find the first φ (n)Law One:enumeration from the N period number +1 to determine if GCD () is equal to 1 until the number of =1 reaches K%φ (n)Number of Cycles =k/φ (n),because of multiple sets of data, it is possible to sift out all the Euler function values in the data range, and then O (1) to usecan also be a one countI only wrote the first one, memory:5380k,time:1579ms.
#include <cstdio>#defineN 1000001using namespacestd;intn,k,cnt,prime[n],phi[n],t,m;BOOLV[n],ok;voidGet_euler () {phi[1]=1;  for(intI=2; i<=n;i++)    {        if(!V[i]) {V[i]=true; prime[++cnt]=i; Phi[i]=i-1; }         for(intj=1; j<=cnt;j++)        {            if(i*prime[j]>n) Break; V[i*prime[j]]=true; if(i%prime[j]==0) {Phi[i*prime[j]]=phi[i]*Prime[j];  Break; } phi[i*prime[j]]=phi[i]* (prime[j]-1); }    }}intgcdintAintb) {    return!b? A:GCD (b,a%b);}intMain () {Get_euler ();  while(SCANF ("%d%d", &n,&k)! =EOF) {        if(n==1) {printf ("%d\n", K); Continue; } OK=false; M=k/Phi[n]; T=k-phi[n]*m; if(!t) m--, t=Phi[n];  for(inti=n*m+1;; i++)        {            if(GCD (n,i) = =1) t--; if(!t) {printf ("%d\n", i);  Break; }        }    }}
View Code

Law II:

The coprime can keep each number with N,

So for each set of data, do an Ed sieve.

Then the enumeration starts from 1, enumerating to the number I of K%φ (n) and N coprime,

ans=i+ Number of Cycles *n

Memory:1156k,time:16ms

It is faster than the law, because can O (1) to determine whether with n coprime, law one to gcd () judgment

#include <cstdio>#include<cmath>#include<cstring>using namespacestd;BOOLcheck[1000001];intEulerintN//an Egyptian sieve template{    intm=int(Sqrt (n+0.5)); intans=n,k=N; memset (check,0,sizeof(check));  for(intI=2; i<=m;i++)     if(n%i==0) {ans=ans/i* (I-1);  for(intj=1; i*j<=k;j++) Check[i*j]=true;  while(n%i==0) n/=i; }    if(n>1) {ans=ans/n* (n1);  for(intj=1; n*j<=k;j++) Check[n*j]=true; }    returnans;}intMain () {intM,k,ans,cnt,t,i;  while(SCANF ("%d%d", &m,&k)! =EOF) {ans=Euler (m); CNT=0; if(k%ans==0) t=k/ans-1; Elset=k/ans; K=k-ans*T;  for(i=1; i<=m;i++)        {            if(!check[i]) cnt++; if(cnt==k) Break; } printf ("%d\n", i+m*t); }}
View Code

POJ 2773 Happy 2006

Related Article

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.