现在的位置: 首页 > 综合 > 正文

poj2115一次模余定理

2013年03月18日 ⁄ 综合 ⁄ 共 590字 ⁄ 字号 评论关闭

http://poj.org/problem?id=2115

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

using namespace std;
typedef __int64 INT ;
INT ex_gcd( INT a , INT  b , INT &x ,INT &y )
{
	if ( b == 0 )
	{
		x = 1 ; 
		y = 0 ;
		return a ;
	}
	else
	{
		INT tm = ex_gcd( b , a % b , x , y ) ;
		INT t = x ;
		x = y ;
		y = t - ( a / b ) * y ;
		return tm ; 
	}
}
int main()
{
 	INT i , n , a1 ,r1 , a2 ,r2  ,ans , a , b , c , d , k , x0 , y0 ;
	while( scanf( "%I64d%I64d%I64d%I64d" , &a , &b , &c , &k ) , ( a + b + c + k ) )
	{
			INT temp = c ;
			c = b - a ;
			a = temp ;
			b = ( INT )1 << k ;
			d = ex_gcd( a , b , x0 , y0 ) ;
			if( c % d != 0 )
			{
				printf( "FOREVER\n" ) ;
			}
			else
			{
				INT ans = x0 * c / d ;
				INT temp = b / d ;
				ans = ( ans % temp + temp ) % temp ;
				printf( "%I64d\n" , ans ) ;
			}
	} 
	return 0 ;
}

抱歉!评论已关闭.