• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

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)

    • 因为等式ax+by=gcd(a, b)必定有解(定理1),所以可以解出来,解法如下:
        因为gcd(a, b) = gcd(b, a % b),所以有bx1+(a%b)y1=gcd(a,b),注意!此时x1并不等于x,y1也不等于y!这个过程可以循环(就想求最大公约数一样),将b视为新的a',a%b视为新的b',可以得到b'x2+(a'%b')y2=gcd(a,b)一直到b''''(n个')=0时,等式就变成:a''..'xn+0y=gcd(a,b),此时根据辗转相除法gcd(a, b)就应该等于a''...',所以xn = 1, yn = 任意数,取0就好。然后根据ax+by=bx1+(a%b)y1 => ax+by=bx1+(a-a/b*b)y1 => ax+by=ay1+b(x1+a/b*y1) 根据恒等定理得x=y1,y=(x1-a/b*y1),接着……递归吧。
    • 对于等式ax+by=c,abc皆为整数且c是gcd(a, b)的倍数,且(x1, y1)是方程ax+by=gcd(a, b)一组整数解,则(x1*(c/gcd(a, b)), y1*(c/gcd(a, b)))是方程ax+by=c的一组解
    • 对于等式ax+by=c,abc皆为整数且c是gcd(a, b)的倍数,且(x1, y1)是方程一组整数解,那么方程的所有整数解为:x=x1+k*(b/gcd(a,b)),y=y1-k*(a/gcd(a,b)),k是任意整数,对于一组x, y,k相同。
        因为(x1, y1)是原方程的一组解,所以有ax1+by1=c,则易得a(x1+k*b)+b(y1-k*a)=c,但是x每增加b,y就会减少a,是否有更小的范围,设d是a,b的约数,但不是最大公约数(若最大公约数是1则d=1)易得a(x1+k*(b/d))+b(y1-k*(a/d))=c,可以知道增加量变小了,现在要求什么时候最小,当然是分母最大的时候变化值最小,也就是当x1+b/gcd(a,b),就是x1的下一个整数解!

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 }
View Code

 

posted @ 2013-11-01 00:15  HaibaraAi  阅读(151)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3