The equation
Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10789
First explain test instructions: first give you three number a B C have ax+by+c=0, and then give you x and y range x1,x2,y1,y2, how many pairs in this range (x, y) is satisfied with the condition ...
Idea: If the time complexity is thrown aside, this is a very simple question, can be said to be linear congruence template problem, directly find the initial x, and then first by adding minus B (A, b) to make X the >=x1 of the smallest one, and then add B (A, b) toward the X2, In this process again determine whether the corresponding y satisfies the condition. This is a very easy way to think, and it seems that time complexity is only O (n), but the fact is: This will be timed out. That is to say, the above paragraph is useless ... 2.R ...
Of course, another way to find the problem is not difficult, because all x, y are from a number plus minus, and just at the beginning of the satisfaction conditions, plus and minus the same number of get is a pair, yes, they still match, they exist recursive formula: x=xt+k*b/(A, b); Y=yt-k*a/(A, b); In other words, as long as K is the same, then the two x, Y, which satisfies both equations, is a pair ...
The first is to determine a minimum x greater than or equal to X1, with K1 Remember that now he is the initial value plus how many times B/b, and then find the largest x in the range, the same, Mark K2, Y also use K3, K4 to mark the minimum and maximum y, and then like K1,k2-> (1,10), K3,k4-> (4,16), then from the fourth pair to the 10th pair satisfies the condition (x, y are within range ...) ), so the answer is equal to k2-k3+1;
The idea is finished, can write code, quietly tell you, I acm.hust.edu.cn in the wrong 70 times, is looking at that [wrong answer on test x] from 1 slowly to the AC ... Written all day, IQ really does not give strength AH. 0.0 ... come to an experience: The 13th sample is mostly a=0, b=0, if the card is in the 13th sample, try to change this. At the same time his background data is not very strong, estimated there is no a==0&&b!=0, a!=0&&b==0, these two cases, even if you in these two conditions to fill the number of 10086 is not wrong ...
1#include <stdio.h>2typedefLong LongLL;3 voidEXGCD (LL a,ll b,ll &d,ll &x,ll &y)4 {5 if(b==0)6 {7D=A;8x=1;9y=0;Ten return ; One } AEXGCD (b,a%b,d,x,y); -LL t=x; -x=y; they=t-a/b*y; - } - ll Max (ll A,ll b) - { + returnA>b?a:b; - } + ll min (ll a,ll b) A { at returnA<b?a:b; - } - intMain () - { - LL a,b,c,x1,x2,y1,y2,tmp,d,x,y; - while(SCANF ("%i64d%i64d%i64d", &a,&b,&c)! =EOF) in { -scanf"%i64d%i64d",&x1,&x2); toscanf"%i64d%i64d",&y1,&y2); +C=-c;//move C to the right of the equation -LL k1,k2,k3,k4,co=0; the if(a==0&&b==0) * { $ if(c==0)Panax NotoginsengCo= (y2-y1+1) * (x2-x1+1); - } the Else if(a==0) + { A if(c%b==0) the { +tmp=c/b; - if(tmp>=y1&&tmp<=y2) co= (x2-x1) +1; $ } $ } - Else if(b==0) - { the if(c%a==0) - {Wuyitmp=c/A; the if(x1<=tmp&&tmp<=x2) co= (y2-y1) +1; - } Wu } - Else About { $ EXGCD (a,b,d,x,y); - if(c%d==0)//cannot divide to represent no solution - { -x=x* (c/d); Ay=y* (c/d); +LL xt=x,yt=y; theLL mx=b/d,my=a/D; -Mx=mx>0? mx:-mx;//avoid trouble and make it all right. $My=my>0? my:-my; the if(x>=x1) the { thex=x-(x-x1)/mx*mx;//if x is greater than X1, it is continuously reduced until the nearest or equal to X1 the } - Else in { thex=x+ (x1-x)/mx*mx;//close to X1 the if(x<x1) x+=mx;//if x is less than X1, add one more time About } thek1= (X-XT)/(b/d);//Record K, we are completely discard what positive and negative, anyway, according to the formula is absolutely not wrong the if(X>=X2)//Ibid . the { +x=x-(X-X2)/mx*MX; - if(X>X2) x-=MX; the }Bayi Else the { thex=x+ (x2-x)/mx*MX; - } -K2= (X-XT)/(b/d); the if(y>=y1) the { they=y-(y-y1)/my*my; the } - Else the { they=y+ (y1-y)/my*my; the if(y<y1) y+=my;94 } thek4= (Y-YT)/(-a/d); the if(y>=y2) the {98y=y-(y-y2)/my*my; About if(y>y2) y-=my; - }101 Else102 {103y=y+ (y2-y)/my*my;104 } thek3= (Y-YT)/(-a/d);106 //to this, k1,k2,k3,k4 all to find out, and then pay attention to certain circumstances may make k2<k1, negative oh. 107Co=min (K4,K2)-max (K3,K1) +1;108 //if (co<0) co=0;109 if(co<0) co=-co+2;//turn it into positive, plus 1 more minus. the //printf ("mx =%i64d\tmy =%i64d\n", mx,my); Help find the wrong. 111 //printf ("XT =%I64D\TK1 =%I64D\TK2 =%i64d\nyt =%I64D\TK3 =%i64d\tk4 =%i64d\n", xt,k1,k2,yt,k3,k4); the }113 } theprintf"%i64d\n", CO); the } the return 0;117}
It says there's a k2<k1 situation.
At the same time, this slag because of unintentional careful study, this line of thought this code only guarantee AC, not guarantee error, also may be background data too Lou, and then fine research it ...
SGU 106 the equation