HDU 1395 2^x mod n = 1

/*

中文题意:

中文翻译:

题目大意:求出最小的 n 使得2的 I 次方对 n 的值为1.

解题思路:如下:

难点详解:先用费马小定理了解2的 i 次方对偶数取余都不可能是一,还有就是排除 1 。之后要用中国剩余定理让 t 的值不超出 int 范围。不用这个定理我错了n次,都是超时。我猜测可能是 t 的值超出了int 的范围了,之后的数都是随机的,所以一直运行不出来,才会超时的。(不知道我的猜测对不对,欢迎大家指正)

关键点:理解费马小定理(我到现在还是不理解),只是用到了一点点这个东西。还有就是中国剩余定理的深刻理解

解题人:lingnichong

解题时间:2014/07/31    12:02

解题感受:一开始感觉题挺简单的,但后来老是超时,就感觉不正常,后来经会长的提醒,用了剩余定理,才AC了。感觉知道一个定理和理解一个定理是大不一样的。还有就是题看不懂和写出来是没多大关系的。

*/

2^x mod n = 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11762    Accepted Submission(s): 3662

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
#include<stdio.h>
int main()
{
	int n,i,t,m;
	while(~scanf("%d",&n))
	{
		if(n%2==0||n==1)
		printf("2^? mod %d = 1\n",n);
		else
		{
			t=1;
			for(i=1;;i++)
			{
				t=t*2;
				if(t%n==1)
				{
					m=i;
					break;
				}
				t=t%n;//此处用了剩余定理了,不然t会超出int型,会报错
			}
			printf("2^%d mod %d = 1\n",m,n);
		}
	}
	return 0;
}

HDU 1395 2^x mod n = 1

时间: 2024-10-12 00:08:50

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

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(暴力题)

题目链接: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

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(欧拉函数)

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

(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 <cstdli

杭电 HDU ACM 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): 13610    Accepted Submission(s): 4208 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. In

2^x mod n = 1 HDU - 1395(欧拉定理 原根)

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

hdoj 1395 2^x mod n = 1 【暴力】

策略 : 观察可知,1 或者是能被2整除的数都不会求余等于1, 仅仅须要推断一下是不是除1之外的奇数,在依次查找2^x(mod(n)) ? = 1就能够了 难点:假设每次都是在原来的基础上×2 再推断 会超时.这时候,要用一下同余定理就能够了 AC by SWS; 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1395 代码: #include<stdio.h> int main() { int n; while(scanf("%d&

hdu 1395 Minimum Transport Cost

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma