http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=489

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>

using namespace std;

int main()
{
	int n ;
	while( cin >> n )
	{
		if( n % 2 == 0 || n < 2 )
		{
			cout << "2^? mod "<< n <<" = 1" << endl;
			continue ;
		}
		else
		{
			int temp = 0 ;
			int ans = 1 ;
			while( 1 )
			{
				ans *= 2 ;
				ans %= n ;
				temp++ ;
				if( ans % n == 1 )
					break ;
			}
			cout << "2^" << temp << " mod "<< n <<" = 1" << endl;
		}
	} 
	return 0 ;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>

using namespace std;

int main()
{
	int n ;
	while( cin >> n )
	{
		if( n % 2 == 0 || n < 2 )
		{
			cout << "2^? mod "<< n <<" = 1" << endl;
			continue ;
		}
			bool flag = false ;
	
			int sum = 1;
			for( int i = 1 ; ; ++i )
			{
				
				sum = 2 * sum ;
				if( sum % n == 1 )
				{
					cout << "2^" << i << " mod "<< n <<" = 1" << endl;
					flag = true ;
					break ;
				}
				sum %= n ;
			}
			
			if( !flag )
				cout << "2^? mod "<< n <<" = 1" << endl;;
	} 
	return 0 ;
}


Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐