POJ 2115 C Looooops

大意不再赘述。

思路:简单的一元线性同余方程,有点KD,移位运算符强制转换行不通,必须用1LL<<K才可以。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;

typedef __int64 LL;

LL A, B, C;
int K;

void ex_gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
	if(!b)
	{
		d = a; x = 1; y = 0;
	}
	else
	{
		ex_gcd(b, a%b, d, y, x);
		y -= x*(a/b);
	}
}

void solve()
{
	LL d, x, y;
	ex_gcd(C, 1LL<<K, d, x, y);
	if((B-A) % d)  { printf("FOREVER\n"); return ;}
	LL b1 = (1LL<<K) / d;
    x *= (B-A) / d;
	LL ans = (x % b1 + b1) % b1;
	printf("%I64d\n", ans);
}

int main()
{
	while(scanf("%I64d%I64d%I64d%d", &A, &B, &C, &K) && (A+B+C+K))
	{
		solve();
	}
	return 0;
}


你可能感兴趣的:(POJ 2115 C Looooops)