| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
哪位路过的帮忙看看,why!!!WA#include <iostream>
#include <cmath>
using namespace std;
__int64 x, y;
__int64 x11, y11;
__int64 bit;
__int64 circle;
__int64 ans;
__int64 E_Euclid(__int64 a, __int64 c)
{
__int64 q, r;
q = a/c;
r = a%c;
if (r == 0)
return c;
__int64 t;
t = x11;
x11 = x;
x = t - q*x;
t = y11;
y11 = y;
y = t - q*y;
a = c;
c = r;
return E_Euclid(a, c);
}
int main()
{
__int64 a, b, c, k;
while (1) {
scanf("%I64d %I64d %I64d %I64d", &a, &b, &c, &k);
if (a==0 && b==0 && c==0 && k==0)
break;
if (c==0 && a!=b) {
printf("FOREVER\n");
continue;
}
if (c==0 && a==b) {
printf("0\n");
continue;
}
bit = 1;
bit = (__int64)(1<<k);
x11 = y = 1;
y11 = x = 0;
int change = 0, add = 0;
if (a<c) {
swap(a,c);
change = 1;
if (c == 0) {
add = 1;
c += a;
}
}
__int64 gcd = E_Euclid(a, c);
int cc=0;
if (b==0) {
b += bit;
cc = 1;
}
//cout<<a<<"*"<<x<<" + "<<c<<"*"<<y<<" = "<<gcd<<endl;
x *= b/gcd;
y *= b/gcd;
//cout<<a<<"*"<<x<<" + "<<c<<"*"<<y<<" = "<<b<<endl;
if ((a*x+c*y) != b)
printf("FOREVER\n");
else {
if (!change) {
ans = (y + a*(x-1)/c);
//cout<<a<<" + "<<c<<"*"<<ans<<" = "<<b<<endl;
if (ans < 0) {
if ((a+c*ans) == b) {
ans = (b+bit-a)/c;
printf("%I64d\n", ans);
}
else
printf("FOREVER\n");
}
else if ((a+c*ans)==b)
printf("%I64d\n", ans);
else
printf("FOREVER\n");
}
else {
ans = (x + c*(y-1)/a);
if (ans < 0) {
if ((c+a*ans) == b) {
ans = (b+bit-c)/a;
if (add)
printf("%I64d\n", ans+1);
else
printf("%I64d\n", ans);
}
else
printf("FOREVER\n");
}
else if (c+a*ans==b) {
if (add)
printf("%I64d\n", ans+1);
else
printf("%I64d\n", ans);
}
else
printf("FOREVER\n");
}
}
}
return 0;
}
/*
#include <iostream>
using namespace std;
int main()
{
int n;
while (cin>>n)
cout<<(1<<n)<<endl;
}*/
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator