uva 618 - Doing Windows(暴力+數學)

來源:互聯網
上載者:User

標籤:style   blog   class   code   tar   ext   

題目連結:uva 618 - Doing Windows


題目大意:給出電腦案頭的大小W和H,現在在案頭上有4個視窗,給出視窗的初始大小,問說能不能通過調整各個視窗的大小(長寬比例不能變)使得4個螢幕剛好佔滿整個螢幕,並且互相不覆蓋。


解題思路:其實可以直接暴力出所有情況,不過細節比較多,而且要考慮所有的細節。

我的做法的是先將4個視窗縮小至最小的狀態,然後枚舉左下角的視窗,

有四種可能


藍色部分為另外枚舉的視窗,3,4種情況要分別保證說長、寬相等,然後S部分就是子問題。

所以用一個位元來表示視窗被使用的情況,然後如果只剩一塊,看隨後一塊和剩下的矩形長寬是否成比例。

#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();printf("Set %d: %s\n", cas++, solve(R, C, 15) ? "Yes" : "No");}return 0;}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.