http://acm.hdu.edu.cn/showproblem.php?pid=1395
题目给定一个数 n(n>=1),要求最小的x使得 2^x%n==1;
容易知道 如果n==1 或者 n为偶数 ,就没有最小值;
否则 一定会有一个最小值x;
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
if(n==1||n%2==0)
{
printf("2^? mod %d = 1\n",n);
continue;
}
int cnt=1,temp=2;
while(temp%n!=1) //因为一定会存在一个x,那么直接暴力查找
{
temp=(temp*2)%n;//这里必须一边乘一遍对n取模,如果不取模,则会超时,很大的两个数相乘所耗费的时间肯定多余 两个很小的数相乘所耗费的时间
cnt++;
}
printf("2^%d mod %d = 1\n",cnt,n);
}
return 0;
}