码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1395 2^x mod n = 1

时间:2014-07-31 13:17:46      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   java   os   strong   io   for   

/*
中文题意:
中文翻译:

题目大意:求出最小的 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,布布扣,bubuko.com

HDU 1395 2^x mod n = 1

标签:des   style   color   java   os   strong   io   for   

原文地址:http://blog.csdn.net/qq_16767427/article/details/38315035

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!