(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",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\n",ans,n);
33             else printf("2^? mod %d = 1\n",n);
34         }
35     }
36     return 0;
37 }
时间: 2024-07-31 22:27:30

(HDU)1395 -- 2^x mod n = 1的相关文章

(HDU)1005 -- Number Sequence(数列)

问题描述 数列定义如下: f(1)= 1,f(2)= 1,f(n)=(A * f(n-1)+ B * f(n-2))mod 7. 给定A,B和n,你要计算f(n)的值. 输入 输入由多个测试用例组成. 每个测试用例在一行(1 <= A,B <= 1000,1 <= n <= 100,000,000)中包含3个整数A,B和n.三个零表示输入结束,此测试用例不进行处理. 输出 对于每个测试用例,在一行上输出f(n)的值. 样例输入 1 1 3 1 2 10 0 0 0 样例输出 2 5

(HDU)1037 --Keep on Truckin&#39;(待在卡丁车上)

题目链接:http://vjudge.net/problem/HDU-1037 告诉你三个通道的高度和车的高度,按顺序判断能否通过. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 int a,b,c; 9 scanf("%d %d %d",&a,&b,&c);

(HDU)1098 -- Ignatius&#39;s puzzle(Ignatius的困惑)

题目链接:http://vjudge.net/problem/HDU-1098 求解思路: f(x)=5*x^13+13*x^5+k*a*x; 其中题中"f(x)|65"表示对于任意的整数x,f(x)都能被65整除.所以不难推断:f(x+1)|65也成立. f(x+1)=5*(x+1)^13+13*(x+1)^5+k*a*(x+1), 根据二项式定理:(a+b)^n=C(n,0)a^n+C(n,1)a^(n-1)*b+C(n,2)a^(n-2)*b^2+...+C(n,n)b^n 得:

(HDU)1014 --Uniform Generator(统一随机数生成)

这个题目不难,关键是看懂英文:(判断两个数是否互质,而且注意输出的格式) 描述 计算机模拟通常需要随机数.生成伪随机数的一种方式是通过一定形式的函数: seed(x + 1)= [seed(x)+ STEP]%MOD 其中'%'是模运算符. 这样的函数将生成在0和MOD-1之间的伪随机数(种子).这种形式的作用的一个问题就是,它们将总是重复地生成相同的模式. 为了最小化这种影响,仔细选择STEP和MOD值,可以使得在0和MOD-1(包括这两者)之间的所有值的均匀分布. 例如,如果STEP = 3

(hdu)5391 Zball in Tina Town

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 Problem Description Tina Town is a friendly place. People there care about each other. Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day, it becomes 1 t

(HDU)1046 -- 完数

题目链接:https://vjudge.net/problem/HDU-1406 注意是所有的因子之和,重复出现的因子不要累加(如果模拟了除法的话),另外给出的两个整数要比较大小(坑). 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <string>

(HDU)1017 --A Mathematical Curiosity(数学好奇心)

描述 给定两个整数n和m,计数整数对(a,b)的数目,使得0 <a <b <n,并且(a ^ 2 + b ^ 2 + m)/(ab)是一个整数. 这个问题包含多个测试用例! 输入的第一行是整数N,然后是空白行,后跟N个输入块. 每个输入块采用问题说明中指示的格式. 输入块之间有空行. 输出格式由N个输出块组成. 输出块之间有一个空行. 输入 您将在输入中获得多个样例. 每个情况由包含整数n和m的行指定. 输入结束由n = m = 0的情况表示.您可以假设0 <n <= 100

(HDU)1040 --As Easy As A+B(像A+B一样简单)

题目链接:http://vjudge.net/problem/HDU-1040 思路:排序算法的水题.注意输出格式,数字之间有空格. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 int n,num,i,j,temp; 9 int s[1010]; 10 scanf("%d",&

(HDU)1061 --Rightmost Digit( 最右边的数字)

题目链接:http://vjudge.net/problem/HDU-1061 这个题目要求出N个N相乘的个位,直接求结果肯定数据溢出. 其实只要每次得出一个数字保留个位和N相乘就可以了, 因为A*B=C,对于个位而言,A(个位)*B(个位)=C(个位)始终成立. 1<=N<=1,000,000,000,这样写还是TLE了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 us