POJ 2773 Happy 2006 (Euler function application)

Source: Internet
Author: User

http://poj.org/problem?id=2773


Test instructions: Input n,k, to find the number of N-non-reciprocal k, k may be greater than N.


Ideas: Take n=6 as an example, with 6 of the number of mutual vegetarian has a certain regularity. {1,5},{7,12},{13,18} ...., found in [1,n],[n+1,n*2] ... [M*n+1, (m+1) *n] interval has the same number of characters, and the corresponding position of the number of different n is an integer multiple. So it is only possible to find the number of n-1,n in [the]. This process does not need an enumeration, which can be solved with Euler functions. Because the Euler function has found all the mass factors of n, and N is the number of non-reciprocal elements and N has the same number of mass factor conventions, can be used to mark the integer multiples of these mass factors and N of the number of non-reciprocal elements. Then the number of K and n in linear time is obtained.


#include <stdio.h> #include <iostream> #include <map> #include <set> #include <stack># Include <vector> #include <math.h> #include <string.h> #include <queue> #include <string> #include <stdlib.h> #include <algorithm> #define LL long long#define _ll __int64#define eps 1e-12#define PI aco S ( -1.0) using namespace std;const int maxn = 1000010;int n,k,kk;int flag[maxn];//for Eular (n), while the mass factor of n is obtained, and the number of n-ary is labeled with these qualitative factors. int eular (int n) {int ret = N,m = N;memset (flag,0,sizeof (flag)), for (int i = 2; i*i <= N; i++) {if (n%i = = 0) {= (int j = 1 ; J*i <= m; J + +) Flag[j*i] = 1;ret = Ret/i;while (n%i = = 0) n/= i;}} if (n > 1) {for (int j = 1; j*n <= m; j + +) Flag[j*n] = 1;ret-= ret/n;} return ret;} int main () {while (~SCANF ("%d%d", &n,&k)) {int cnt = Eular (n), t;t = k/cnt;if (k%cnt = = 0) T--;kk = k-t*cnt;int Cou = 0,i;for (i = 1; I <= n; i++) {if (flag[i] = = 0) cou++;if (cou = = KK) break;} printf ("%d\n", I+t*n);} return 0;}




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.