Good questions, interesting questions, and troublesome questions

Source: Internet
Author: User
Tags cmath

Sgu 106:

First, let's solve a linear equation AX + by =-C, which can be done by extending Euclidean. However, it requires us to output the number of solutions that meet X1 <= x <= x2, Y1 <= Y <= Y2, which is not that simple...

At first I wanted to record the boundary values of X and Y, But I thought for a long time and divided them into n cases... because it was too cumbersome, I simply gave up the score and handed in a wa.

After reading someone's problem-solving report, we found that we can find the number of K that satisfies X1 <= x0 + K * R1 <= x2, Y1 <= y0-k * R2 <= Y2. R1 = LCM/a, R2 = LCM/B, LCM = a * B * gcd (A, B ).

Then, solve the equation and calculate the intersection length of the interval.

(The Ceil and floor functions need to be # include <cmath>, while the cmath contains a function named Y1, which is really painful. But in fact, you only need to get Y1 from the global system to the inside of the main function)

1 # include <iostream> 2 # include <cstdio> 3 # include <cmath> 4 # define debug (X) cout <# x <"=" <x <Endl 5 using namespace STD; 6 typedef long ll; 7 8 LL gcd (ll a, LL B) 9 {10 LL temp; 11 if (a <0) A =-A; If (B <0) B =-B; 12 while (B! = 0) {temp = B; B = A % B; A = temp;} 13 return a; 14} 15 // input, B must be mutually qualitative (because at the end of a * 1 + B * 0 = 1, ensure that A is 1) 16 void _ gcd (ll a, LL B, ll & X, ll & Y) 17 {18 if (B = 0) {x = 1, y = 0; return;} 19 _ gcd (B, A % B, X, y); 20 ll temp = y; 21 y = x-A/B * Y; X = temp; 22} 23 int main () 24 {25 ll A, B, c, x1, x2, Y1, Y2; 26 ll X, Y; 27 ll tx1, tx2, ty1, ty2; 28 CIN> A> B> C> x1> X2> Y1> Y2; 29 ll r = gcd (a, B), R1, R2; 30 if (a = 0 & B = 0) 31 {32 If (C = 0) cout <(x2-x1 + 1) * (y2-y1 + 1) <Endl; 33 else cout <0 <Endl; 34} 35 else if (a = 0 | B = 0) 36 {37 if (a = 0) Swap (a, B), swap (x1, Y1), swap (X2, Y2); 38 If (C %! = 0) cout <0 <Endl; 39 else if (-C/A> = x1 &-C/A <= x2) cout <(y2-y1 + 1) <Endl; 40 else cout <0 <Endl; 41} 42 else if (C % R! = 0) printf ("0 \ n"); 43 else44 {45 _ gcd (A/R, B/R, x, y ); 46 X * =-C/R; y * =-C/R; 47 r = a * B/R; R1 = r/a; r2 = r/B; 48 tx1 = x1-x; tx2 = x2-x; If (R1 <0) Swap (tx1, tx2), tx1 =-tx1, tx2 =-tx2, R1 =-R1; 49 ty1 =-y2 + Y; ty2 =-Y1 + Y; If (R2 <0) Swap (ty1, ty2), ty1 =-ty1, ty2 =-ty2, r2 =-R2; 50 tx1 = Ceil (double (tx1)/R1); tx2 = floor (double (tx2)/R1); 51 ty1 = Ceil (double (ty1) /R2); ty2 = floor (double (ty2)/R2); 52 tx1 = max (tx1, ty1); tx2 = min (tx2, ty2 ); 53 If (tx2> = tx1) cout <tx2-tx1 + 1 <Endl; 54 else cout <0 <Endl; 55} 56 return 0; 57}
The equation

 

Good questions, interesting questions, and troublesome questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.