(HDU)1395 -- 2^x mod n = 1

题目链接:http://vjudge.net/problem/HDU-1395

分析可知,n为偶数或者1的时候输出‘?’

自己设置一个循环周期。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <string>
 7 #include <cstdlib>
 8 
 9 using namespace std;
10 
11 int main()
12 {
13     int n,ans,test;
14     while(~scanf("%d",&n))
15     {
16         test=1;
17         if(n%2==0 || n==1) printf("2^? mod %d = 1
",n);
18         else
19         {
20             int flag=1;
21             for(ans=1;ans<=5000;ans++)
22             {
23                 test*=2;
24                 test%=n;
25                 if(test%n==1) break;
26                 if(ans==5000)
27                 {
28                     flag=0;
29                     break;
30                 }
31             }
32             if (flag) printf("2^%d mod %d = 1
",ans,n);
33             else printf("2^? mod %d = 1
",n);
34         }
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/ACDoge/p/6138071.html