POJ 2773-happy 2006 (Euler function)

Source: Internet
Author: User
Tags gcd

Happy 2006Time limit:MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 2773Appoint Description:System Crawler (2015-04-06)

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

Test instructions: Given a two number m,k, you are asked to find the number of K and M coprime.

Idea: Because there are gcd (A, B) =gcd (A+T*B,B) for the number of conventions. Therefore, in [1,m-1] with the number of M-Mutual is the same as in [K*m+1, (k+1) *m-1] The number of interconnects is equal to Phi (m), so as long as the number of [1,m-1] and M Mutual, you can find the next interval of the use of the number of the following: Then the answer is equal to the first k% The Value p+m* (K/phi (m)) of Phi (m) and M-biotin.

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map>using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const double pi= ACOs (    -1.0); int prime[1000010];int Euler (int n)//filter prime number + Seek <=n with n-ary numbers {int i,j;    int m=n;//because the following n is going to become, so first save n int c=n;//return is pji[m] memset (prime,0,sizeof (prime));            for (i=2;i*i<=n;i++) {if (n%i==0) {n/=i;            for (j=1;i*j<=m;j++)//Use the filter method to mark the number of all and n prime[i*j]=1;            c=c/i* (i-1);        while (n%i==0) n=n/i;        }} if (n>1) {for (j=1;n*j<=m;j++)//When n>1, n itself is also a vegetarian factor prime[n*j]=1;    c=c/n* (n-1); } return C;}    int main () {int m,k,i;    int cnt;    int sum;    int t;        while (~SCANF ("%d%d", &m,&k)) {Cnt=euler (M);t=k/cnt;        sum=0;        if (k%cnt==0) t--;        K=k-cnt*t;            for (i=1;i<=m;i++) {if (!prime[i]) sum++;        if (sum==k) break;    } printf ("%d\n", m*t+i); } return 0;}


POJ 2773-happy 2006 (Euler function)

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.