HDU13952^x mod n = 1

it2026-09-26  8

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

题目大意:求出最小的 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; }

转载于:https://www.cnblogs.com/bhlsheji/p/5224736.html

最新回复(0)