SGU 106 The equation
|
106. The equation time limit per test: 0.5 sec. memory limit per test: 4096 KB 对于等式ax+by=c,a,b,c皆为整数,c如果是gcd(a, b)的倍数,则方程有解,否则方程无解。(定理1)
There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer roots of this equation are satisfy to the following conditions : x1<=x<=x2, y1<=y<=y2. Integer root of this equation is a pair of integer numbers (x,y).
Input Input contains integer numbers a,b,c,x1,x2,y1,y2 delimited by spaces and line breaks. All numbers are not greater than 108 by absolute value.
Output Write answer to the output.
Sample Input 1 1 -3 0 4 0 4 Sample Output 4 |
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <map> 3 #include <queue> 4 #include <vector> 5 #include <string> 6 #include <cmath> 7 #include <cstdio> 8 #include <cstring> 9 #include <cstdlib> 10 #include <iostream> 11 #include <algorithm> 12 using namespace std; 13 #define maxn 105 14 #define ll long long 15 #define INF 0x7fffffff 16 #define eps 1e-8 17 int n,m,ma=-INF; 18 int gcd(int n, int m){ return m ? gcd(m, n%m) : n; } 19 int exd(ll a, ll b, ll &x, ll &y){ 20 if (b == 0){x = 1; y = 0; return a;} 21 ll r=exd(b, a%b, x, y); 22 ll t = x; 23 x = y; y = t - a / b*y; 24 return r; 25 } 26 int main(){ 27 ll x, y, x1, y1, x2, y2, a, b, c,k,k1,k2; 28 while (~scanf("%I64d%I64d%I64d", &a, &b, &c)){ 29 scanf("%I64d%I64d%I64d%I64d", &x1, &x2, &y1, &y2); 30 c = -c; 31 m = exd(a, b, x, y); 32 if (a == 0 && b == 0){ 33 if (c == 0)printf("%I64\n", (x2 - x1+1)*(y2 - y1+1)); 34 else printf("0\n"); 35 continue; 36 } 37 if (a == 0){ 38 if (c%b == 0 && c / b >= y1&&c / b <= y2)printf("I64d\n", x2 - x1 + 1); 39 else printf("0\n"); 40 continue; 41 } 42 if (b == 0){ 43 if (c%a == 0 && c / a >= x1&&c / a <= x2)printf("%I64d\n", y2 - y1 + 1); 44 else printf("0\n"); 45 continue; 46 } 47 if (c%m != 0){printf("0\n"); continue;} 48 x *= c / m; y *= c / m; 49 k=(x1-x)/b*m; 50 x += abs(k*b / m); 51 y -= abs(k*a / m); 52 if (x < x1)x += abs(b / m), y -= abs(a / m); 53 if (y >y2)k = (y - y2) / a*m; 54 y -= abs(k*a / m); 55 x += abs(k*b / m); 56 k1=abs((x2 - x) / b*m); 57 k2 = abs((y - y1) / a*m); 58 k = min(k1, k2); 59 x += abs(k*b / m); 60 y -= abs(k*a / m); 61 if (x>x2)x -= abs(b / m), y +=abs( a / m); 62 if (x<x1 || x>x2 || y<y1 || y>y2)k = -1; 63 printf("%I64d\n", k+1); 64 } 65 return 0; 66 }
浙公网安备 33010602011771号