POJ2115 C Looooops

嘟嘟嘟

题面就是说,解方程 a + c * x Ξ b (mod 2k)

然后变个型:a + c * x - 2k * y = b - a。用exgcd求解即可。

刚开始我以为b - a还要正负讨论,但其实不用,因为正负数取摸结果不一样。

然后我调了半天是因为当a = b = c = k = 0的时候才退出,然后我写的是有一个为0就退出……

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("") 
13 #define space putchar(' ')
14 #define Mem(a, x) memset(a, x, sizeof(a))
15 #define rg register
16 typedef long long ll;
17 typedef double db;
18 const int INF = 0x3f3f3f3f;
19 const db eps = 1e-8;
20 //const int maxn = ;
21 inline ll read()
22 {
23     ll ans = 0;
24     char ch = getchar(), last = ' ';
25     while(!isdigit(ch)) {last = ch; ch = getchar();}
26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
27     if(last == '-') ans = -ans;
28     return ans;
29 }
30 inline void write(ll x)
31 {
32     if(x < 0) x = -x, putchar('-');
33     if(x >= 10) write(x / 10);
34     putchar(x % 10 + '0');
35 }
36 
37 ll a, b, c, k;
38 
39 void exgcd(ll a, ll b, ll &x, ll &y, ll &d)
40 {
41   if(!b) d = a, x = 1, y = 0;
42   else {exgcd(b, a % b, y, x, d); y -= a / b * x;}
43 }
44 
45 int main()
46 {
47   while(scanf("%lld%lld%lld%lld", &a, &b, &c, &k))
48     {
49       if(!a && !b && !c && !k) break;
50       k = (ll)1 << k;            //别忘了1得改成long long ,要不然1 << 32会爆
51       ll x, y, d;
52       exgcd(c, k, x, y, d);
53       if((b - a) % d) {puts("FOREVER"); continue;}
54       x = x * (b - a) / d;
55       ll t = k / d;
56       x = (x % t + t) % t;
57       write(x); enter;
58     }
59   return 0;
60 }
View Code
原文地址:https://www.cnblogs.com/mrclr/p/9776975.html