poj 2773 (欧拉函数)

分析: [1,m-1]有phi[m]个数与m互质。则 显然 [n * m + 1, ((n + 1)* m) - 1]也有phi[m]个数与之互质。(n = 0,1,2,...)

RE 的考虑 m = 1 这种情况。

 1 // File Name: 2773.cpp

 2 // Author: Missa_Chen

 3 // Created Time: 2013年05月28日 星期二 13时58分13秒

 4 

 5 #include<iostream>

 6 #include<string>

 7 #include<algorithm>

 8 #include<cstdio>

 9 #include<cstring>

10 #include<cmath>

11 #include<queue>

12 #include<map>

13 #include<stack>

14 #include<set>

15 #include<cstdlib>

16 

17 using namespace std;

18 

19 #define LL long long

20 const int inf = 0x3f3f3f3f;

21 const int maxn = 1e6 + 10;

22 int phi[maxn], pn, prim[maxn];

23 bool p[maxn];

24 void init_euler(int n)

25 {

26     phi[1] = 0;

27     memset(p,0,sizeof(p));

28     p[0] = p[1] = 1;

29     for (int i = 2; i <= n; ++i)

30     {

31         if (!p[i])

32         {

33             phi[i] = i - 1;

34             prim[pn++] = i;

35         }

36         for (int j = 0; j < pn && prim[j] * i <= n; ++j)

37         {

38             p[prim[j] * i] = 1;

39             if (i % prim[j])

40                 phi[i * prim[j]] = phi[i] * (prim[j] - 1);

41             else

42             {

43                 phi[i * prim[j]] = phi[i] * prim[j];

44                 break;

45             }

46         }

47     }

48 }

49 LL gcd(LL a, LL b)

50 {

51     while (b != 0)

52     {

53         LL c = a % b;

54         a = b;

55         b = c;

56     }

57     return a;

58     //return b == 0 ? a : gcd(b, a % b);

59 }

60 int m, k;

61 

62 int main()

63 {

64     init_euler((int)1e6);

65     while (~scanf("%d%d",&m,&k))

66     {

67         if (m == 1)

68         {

69             printf("%d\n",k);

70             continue;

71         }

72         int n = k / phi[m];

73         k -= n * phi[m];

74         if (k == 0) k = phi[m], n--;

75         for (LL i = n * m + 1; ; ++i)

76         {

77             if (gcd(m, i) == 1LL) k--;

78             if (k == 0)

79             {

80                 printf("%lld\n",i);

81                 break;

82             }

83         }

84     }

85     return 0;

86 }

 

你可能感兴趣的:(poj)