B - C Looooops POJ - 2115 (扩展欧几里得)

时间:2023-03-10 00:20:35
B - C Looooops POJ - 2115 (扩展欧几里得)

题目链接:https://cn.vjudge.net/contest/276376#problem/B

题目大意:for( int  i= A ; i != B; i+ = c ),然后给你A,B,C,K,前三个是for循环里面的变量,K指的是每一次ABC三个数都对2^k进行取余,然后问你要使得这个循环停止,最少的循环次数,如果是一个死循环,就输出"FOREVER".

具体思路:和我之前写的那一篇博客思路差不多,证明过程直接上图。

B - C Looooops POJ - 2115 (扩展欧几里得)

一开始没有注意到B,等号右边应该是B-A,而不是B,以后注意下(不过结果正负的情况下,只有在后b调就可以了)。

AC代码:

 #include<iostream>
#include<stack>
#include<cmath>
#include<queue>
#include<stdio.h>
#include<algorithm>
using namespace std;
# define ll long long
const int maxn = 1e5+;
ll quickpow(ll ti)
{
ll ans=;
ti--;
ll tmp=;
while(ti)
{
if(ti&)
ans=ans*tmp;
tmp*=tmp;
ti>>=;
}
return ans;
}
ll xo,yo;
ll exgcd(ll a,ll b)
{
if(b==)
{
xo=;
yo=;
return a;
}
ll gcd=exgcd(b,a%b);
ll tmp=yo;
yo=xo-a/b*yo;
xo=tmp;
return gcd;
}
int main()
{
ll a,b,c,k;
while(~scanf("%lld %lld %lld %lld",&a,&b,&c,&k)&&(a+b+c+k))
{
ll tmp=quickpow(k);
ll t=exgcd(c,tmp);
if((b-a)%t)
printf("FOREVER\n");
else
{
ll tmp1=tmp/t;
printf("%lld\n",((b-a)*xo/t%tmp1+tmp1)%tmp1);
}
}
return ;
}