/*
题目描述:给出程序段for(int i = A ; i!= B ; i+= C) statement ,给出A,B,C,k,已知该程序段的运算中出现
的数都是mod k之后的,问该程序段将被执行多少次结束,不会结束的话输出FOREVER
方法:由题意列出方程有A + Cx = B (mod k),变形Cx = B - A(mod k),要求该模线性方程的最小正整数解
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-8;
LL ex_gcd(LL a , LL b , LL &x , LL &y )
{
if(b==0){
x = 1; y = 0;
return a;
}
LL d = ex_gcd(b , a% b , y , x);
y = y - a/b * x;
return d;
}
LL linearmod(LL a , LL b , LL c) //求解ax=b (mod c)的最小正整数解
{
LL x , y;
LL d = ex_gcd(a , c , x , y);
if(b%d)
return -1;
x = b / d * x ;
LL m = c / d;
return (x % m + m)% m;
}
int main()
{
LL a , b , c ;
int k;
while(scanf("%lld%lld%lld%d",&a , &b , &c , &k)==4 ){
if(!a && !b && !c && !k) break;
LL real_mod = 1LL << k;
LL ans = linearmod(c ,b - a , real_mod);
if(ans == -1)
printf("FOREVER\n");
else
printf("%lld\n" , ans );
}
return 0;
}
POJ2115 C Looooops 一元模线性方程
最新推荐文章于 2024-08-17 03:36:59 发布