HDU_2^x mod n = 1 (数论)

  奇数时一定有结果,偶数时一定没有。。。然后循环就可以了。

ps:好几天没做题,有点手生了。。。

View Code
 1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4
5 using namespace std;
6
7
8 int main() {
9 // freopen("data.txt", "r", stdin);
10
11 __int64 n, tmp;
12 int i;
13 while(~scanf("%I64d", &n)) {
14 if(!(n&1) || n <= 1) {printf("2^? mod %I64d = 1\n",n); continue;}
15 tmp = 1;
16 for(i = 1; ; i++) {
17 tmp *= 2;
18 if(tmp%n == 1) {
19 break;
20 }
21 tmp %= n;
22 }
23 printf("2^%d mod %I64d = 1\n", i, n);
24 }
25 return 0;
26 }



原文地址:https://www.cnblogs.com/vongang/p/2326270.html