SGU 106 The equation

分情况讨论

扩展欧几里得

代码是按别人的思路改的。。

  1 /*Author :usedrose  */

  2 /*Created Time :2015/7/25 14:51:42*/

  3 /*File Name :2.cpp*/

  4 #include <cstdio>

  5 #include <iostream>

  6 #include <algorithm>

  7 #include <sstream>

  8 #include <cstdlib>

  9 #include <cstring>

 10 #include <climits>

 11 #include <vector>

 12 #include <string>

 13 #include <ctime>

 14 #include <cmath>

 15 #include <deque>

 16 #include <queue>

 17 #include <stack>

 18 #include <set>

 19 #include <map>

 20 #define INF 0x3f3f3f3f

 21 #define eps 1e-8

 22 #define pi acos(-1.0)

 23 #define MAXN 1110

 24 #define OK cout << "ok" << endl;

 25 #define o(a) cout << #a << " = " << a << endl

 26 #define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl

 27 using namespace std;

 28 typedef long long LL;

 29 

 30 void exgcd(LL a, LL b, LL &d, LL &x, LL &y)

 31 {

 32     if (!b) d = a, x = 1, y = 0;

 33     else {

 34         exgcd(b, a%b, d, y, x);

 35         y -= x*(a/b);

 36     }

 37 }

 38 

 39 int main()

 40 {

 41     //freopen("data.in","r",stdin);

 42     //freopen("data.out","w",stdout);

 43     cin.tie(0);

 44     ios::sync_with_stdio(false);

 45     LL a, b, c, d, x, y, x1, x2, y1, y2;

 46     while (cin >> a >> b >> c) {

 47         cin >> x1 >> x2 >> y1 >> y2;

 48         if (a == 0 || b == 0) {

 49             if (a == 0 && b) {

 50                 if (c%b || c/b > y2 || c/b < y1) cout << "0" << endl;

 51                 else cout << x2 - x1 + 1 << endl;

 52             }

 53             else if (b == 0 && a) {

 54                 if (c%a || c/a > x1 || c/b < x1) cout << "0" << endl;

 55                 else cout << y2 - y1 + 1 << endl;

 56             } 

 57             else {

 58                 if (c) cout << "0" << endl;

 59                    else cout << (x2 - x1 + 1)*(y2 - y1 + 1) << endl;    

 60             }

 61         }

 62         else {

 63             if (c < 0) c = -c;

 64             else a = -a, b = -b;

 65             if (a < 0) {

 66                 a = -a;

 67                 x1 = -x1;

 68                 x2 = -x2;

 69                 swap(x1, x2);

 70             }

 71             if (b < 0) {

 72                 b = -b;

 73                 y1 = -y1;

 74                 y2 = -y2;

 75                 swap(y1, y2);

 76             }

 77             if (a < b) exgcd(b, a, d, y, x);

 78             else exgcd(a, b, d, x, y);

 79             if (c%d) {

 80                 cout << "0" << endl;

 81                 continue;

 82             }

 83             a /= d, b /= d, c /= d;

 84             //小优化

 85             while(x < x1) x += b, y -= a;

 86             while(x > x2) x -= b, y += a;

 87             LL ans = 0;

 88             bool in = false;

 89             x *= c, y *= c;

 90             while (x <= x2) {

 91                 if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {

 92                     in = true;

 93                     break;

 94                 }    

 95                 x += b, y -= a;

 96             }

 97             while (!in && x >= x1) {

 98                 if (x >= x1 && x <= x2 && y >= y1 && y <= y2) { 

 99                     in = true;

100                     break;

101                 }    

102                 x -= b, y += a;

103             } 

104             if (in) {

105                 ans += min((x2-x)/b, (y-y1)/a);

106                 ans += min((x-x1)/b, (y2-y)/a);

107             }

108             cout << ans + in << endl;

109         }

110     }

111     return 0;

112 }
View Code

 

你可能感兴趣的:(IO)