SGU_106
这个题目AC了的程序也写的不尽相同,但就NOWCOW上面的那个C++程序来说,处理某些数据确实存在问题,所以最好还是写自己的思路了。
我的思路就是首先把一个基本解求出来,然后看在x1、x2的范围内x的范围是多少,然后找到对应的y的范围,再看y的范围有多少个解是在y1、y2范围之内的,这个就是最后的答案。
当然,对于含有a=0或b=0的情况要特判一下。
#include<stdio.h>
#include<string.h>
long long int a, b, c, x1, x2, y1, y2;
long long int abs(int x)
{
return x < 0 ? (-x) : x;
}
void swap(long long int &x, long long int &y)
{
long long int k;
k = x, x = y, y = k;
}
long long int gcd(long long int p, long long int q, long long int &d, long long int &x, long long int &y)
{
if(q == 0)
x = 1, y = 0, d = p;
else
{
gcd(q, p % q, d, y, x);
y -= x * (p / q);
}
}
void solve()
{
long long int d, x, y, cnt, lx, ly, rx, ry, dx, dy, p;
c = -c;
scanf("%I64d%I64d%I64d%I64d", &x1, &x2, &y1, &y2);
if(x1 > x2 || y1 > y2)
printf("0\n");
else if(a == 0 && b == 0)
{
if(c == 0)
printf("%I64d\n", (x2 - x1 + 1) * (y2 - y1 + 1));
else
printf("0\n");
}
else if(a == 0)
{
if(c % b == 0 && c / b <= y2 && c / b >= y1)
printf("%I64d\n", x2 - x1 + 1);
else
printf("0\n");
}
else if(b == 0)
{
if(c % a == 0 && c / a <= x2 && c / a >= x1)
printf("%I64d\n", y2 - y1 + 1);
else
printf("0\n");
}
else
{
gcd(abs(a), abs(b), d, x, y);
if(c % d != 0)
printf("0\n");
else
{
if(a < 0)
x = -x;
if(b < 0)
y = -y;
x = x * c / d, y = y * c / d;
dx = abs(b / d), dy = abs(a / d);
p = (x % dx - x1 % dx + 2 * dx) % dx;
lx = x1 + p;
p = (x % dx - x2 % dx + 2 * dx) % dx;
rx = x2 + p - (p == 0 ? 0 : dx);
if(lx > rx)
printf("0\n");
else
{
ly = (c - a * lx) / b, ry = (c - a * rx) / b;
if(ly > ry)
swap(ly, ry);
if(ly > y2 || ry < y1)
printf("0\n");
else
{
if(ly < y1)
{
p = (y % dy - y1 % dy + 2 * dy) % dy;
ly = y1 + p;
}
if(ry > y2)
{
p = (y % dy - y2 % dy + 2 * dy) % dy;
ry = y2 + p - (p == 0 ? 0 : dy);
}
printf("%I64d\n", (ry - ly) / dy + 1);
}
}
}
}
}
int main()
{
while(scanf("%I64d%I64d%I64d", &a, &b, &c) == 3)
{
solve();
}
return 0;
}