POJ 2115 C Looooops 模线性方程(扩展欧几里得)

来源:互联网 发布:智能手环推荐 知乎 编辑:程序博客网 时间:2024/05/01 22:57

题解:转换一下。和青蛙那题差不多。

#include<iostream>#include<cmath>using namespace std;__int64 Egcd ( __int64 a, __int64 b, __int64 &x, __int64 &y ){__int64 tmp, ret;if ( b == 0 ){x = 1, b = 0;return a;}ret = Egcd ( b, a%b, x, y );tmp = x, x = y, y = tmp - a / b * y;return ret;}int main(){__int64 A, B, C, K, x, y;while ( 1 ){scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&K);if ( A+B+C+K == 0 ) break;__int64 M = (__int64)pow(2.0,K+0.0);__int64 a = C, b = M, c = B - A;__int64 d = Egcd ( a, b, x, y );if ( c % d != 0 ){printf("FOREVER\n");continue;}x = (c*x/d)%(b/d);if ( x < 0 )x += b/d;printf("%I64d\n",x);}return 0;}


原创粉丝点击