Topic Link: UVA 618-doing Windows
The main topic: give the computer desktop size W and H, now on the desktop there are 4 windows, give the initial size of the window, asked if you can adjust the size of each window (the length and width of the ratio can not be changed) so that 4 screens just occupy the entire screen, and do not cover each other.
Problem-solving ideas: In fact, can be directly violent out of all situations, but more details, but also to consider all the details.
My approach is to reduce the 4 windows to the smallest state, then enumerate the lower left corner of the window,
There are four kinds of possibilities
The blue part is another enumeration of the Windows, 3, 4 cases to ensure that the length and width are equal, then the S part is a sub-problem.
So use a binary number to indicate what the window is used for, and then if there is only one piece left, see if the next piece is proportional to the remaining rectangle's length.
#include <cstdio> #include <cstring>const int N = 5;typedef long Long ll;inline ll gcd (ll A, ll b) {return b = = 0? A:GCD (b, a%b);} inline int bitcount (int x) {return x = = 0? 0:bitcount (X/2) + (x&1);} struct State {ll R, c;state (ll r = 0, ll c = 0) {this->r = R;this->c = C;} void Get () {scanf ("%lld%lld", &r, &c); ll d = gcd (R, c); R/= d;c/= D;}} W[n];bool Solve (ll R, ll C, int s), inline BOOL CMP (state A, State B) {return A.R * B.C = = B.R * A.C;} void Cat (State U, State v, state& A, state& b, int s) {ll P, q;if (s = = 0) {ll d = gcd (U.R, V.R);p = V.r/d;q = u.r/d;} else {ll d = gcd (U.C, v.c);p = V.c/d;q = U.C/D;} A.R = U.R * p;a.c = u.c * P;B.R = V.R * Q;B.C = v.c * q;} BOOL Judge (ll R, LL C, State v, int s, int. sign) {if (sign = = 0) {if (r% v.r) return false;ll k = R/V.R; c-= v.c * K;IF (c <= 0) return false;} else {if (C% v.c) return false;ll k = c/v.c; R-= V.R * K;IF (r <= 0) return false;} return solve (R, C, s);} BOOL Judgetow (LL R, ll C, State v, int s) {for (int i = 0; i < 4; i++) {if ((s& (1<<i)) = = 0) continue;state Add, A, b;for (int j = 0; J < 2; J + +) {cat (V, W[i], A, B, j); if (j = = 0) {ADD.R = A.R;ADD.C = A.C + B.C;} else {ADD.R = A.R + b.r;add.c = A.C;} if (judge (R, C, add, S (1<<i), 1-j)) return true;}} return false;} BOOL Solve (ll R, ll C, int s) {int cnt = Bitcount (s), if (cnt = = 1) {for (int i = 0; i < 4; i++) if (s& (1<<i)) Return CMP (State (R, C), W[i]);} for (int i = 0; i < 4; i++) {if ((s& (1<<i)) = = 0) continue;for (int j = 0; J < 2; J + +) {if (Judge (R, C, W[i ], S (1<<i), J)) return true; if (CNT > 2 && Judgetow (R, C, W[i], S (1<<i))) return true; return false;} int main () {int cas = 1;ll R, C;while (scanf ("%lld%lld", &r, &c) = = 2 && R + C) {for (int i = 0; i < 4 ; i++) W[i].get ();p rintf ("Set%d:%s\n", cas++, Solve (R, C, 15)? "Yes": "No");} return 0;}