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;}