hdu 1395 2^x mod n = 1(暴力题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395

2^x mod n = 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12146    Accepted Submission(s):
3797

Problem Description

Give a number n, find the minimum x(x>0) that
satisfies 2^x mod n = 1.

Input

One positive integer on each line, the value of
n.

Output

If the minimum x exists, print a line with 2^x mod n =
1.

Print 2^? mod n = 1 otherwise.

You should replace x and n with
specific numbers.

Sample Input

2

5

Sample Output

2^? mod 2 = 1

2^4 mod 5 = 1

题目大意:暴力搜索,找到合适的X值,这一题可以采取反过来暴力寻找,这一简单易懂些。

要注意的是输出的值时都要变化的,输出注意一下就好了,毕竟我是wa过的。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 int main ()
 6 {
 7     int n;
 8     while (cin>>n)
 9     {
10         if (n%2&&n>1)
11         {
12             int s=1,x=1;
13             while (x)
14             {
15                 s=s*2%n;
16                 if (s==1)
17                 {
18                     printf ("2^%d mod %d = 1\n",x,n);
19                     break;
20                 }
21                 x++;
22             }
23         }
24         else
25             printf ("2^? mod %d = 1\n",n);
26     }
27     return 0;
28 }
时间: 2024-10-11 12:15:27

hdu 1395 2^x mod n = 1(暴力题)的相关文章

hdu 1395 2^x mod n = 1 暴力过~~最好学下欧拉定理~~~

2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13341    Accepted Submission(s): 4143 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. In

hdu 1395 2^x mod n = 1 (简单数论)

题目大意: 求出一个最小的x 使得 2的x次方对n取模为1 思路分析: 若要 a*b%p=1  要使得b存在 则 gcd (a,p)=1. 那么我们应用到这个题目上来. 当n为偶数 2^x 也是偶数,那么gcd 肯定不是1.故这个是不存在的. 那么n为奇数的时候,也就一定是1了. 所以直接暴力找. #include <iostream> #include <cstdio> using namespace std; int main() { int n; while(scanf(&q

HDU 1395 2^x mod n = 1

/* 中文题意: 中文翻译: 题目大意:求出最小的 n 使得2的 I 次方对 n 的值为1. 解题思路:如下: 难点详解:先用费马小定理了解2的 i 次方对偶数取余都不可能是一,还有就是排除 1 .之后要用中国剩余定理让 t 的值不超出 int 范围.不用这个定理我错了n次,都是超时.我猜测可能是 t 的值超出了int 的范围了,之后的数都是随机的,所以一直运行不出来,才会超时的.(不知道我的猜测对不对,欢迎大家指正) 关键点:理解费马小定理(我到现在还是不理解),只是用到了一点点这个东西.还有

hdu 1395 2^x mod n = 1(欧拉函数)

2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18742    Accepted Submission(s): 5860 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. Inp

HDU1395 2^x mod n = 1 暴力题

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1395 可以用欧拉定理证明其存在性. 欧拉定理是这样的: 如果a和m互质且a<m,设x为欧拉函数的值,则a^x%m=1 因此在本题中,只要所给的数为奇数,即和2互质,则一定存在解,再暴力就可以了 1 #include<cstdio> 2 int main() 3 { 4 int n; 5 while (scanf("%d", &n) != EOF){ 6 if (n %

HDU 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

hdu 2721(字符串处理,位运算 暴力)

Persistent Bits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 116 Problem Description WhatNext Software creates sequence generators that they hope will produce

hdu 4585 Shaolin两种方法(暴力和STL)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The mast

hdu 4886 TIANKENG’s restaurant(2)(hash+暴力)

题目链接:hdu 4886 TIANKENG's restaurant(2) 题目大意:给定一个字符串S,要求在该字符串中找到一个最短并且字符串字典序最小. 解题思路:每次枚举字符串的长度,然后将S中所有该长度的子串映射成一个9进制数,最后再遍历一遍标记数组. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000005; i