UVA 618 - Doing Windows(數論)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   2014   

題目連結:618 - Doing Windows題意:給定一個大小不能變的螢幕,和四個大小可以變的視窗,變化要保持長寬比,問這四個視窗能不能調整後全部放下正好填滿螢幕,不能重疊思路:情況一共就幾種:4個疊一起,3個疊一起+一個,2個和2個,一個和兩個疊一起在一個,把這幾種情況全判斷了就可以了,判斷過程利用gcd,lcm可以求邊長。代碼:
#include <stdio.h>#include <string.h>long long gcd(long long a, long long b) {    if (b == 0) return a;    return gcd(b, a % b);}long long lcm(long long a, long long b) {    return a / gcd(a, b) * b;}struct Win {    long long x, y;    bool scanf_() {scanf("%lld%lld", &x, &y);if (x == 0 || y == 0)     return false;long long t = gcd(x, y); x /= t; y /= t;return true;    }    void swap() {long long t = x; x = y; y = t;    }} win, w[4];bool check1(Win a, Win b, Win c, Win d) {    long long sum = 0;    if (win.y % a.y) return false;    sum += win.y / a.y * a.x;    if (win.y % b.y) return false;    sum += win.y / b.y * b.x;    if (win.y % c.y) return false;    sum += win.y / c.y * c.x;    if (win.y % d.y) return false;    sum += win.y / d.y * d.x;    if (sum != win.x) return false;    return true;}bool check(Win a, Win b, Win c, long long wx, long long wy) {    if (wy <= wx / a.x * a.y) return false;    long long yy = wy - wx / a.x * a.y;    long long sum = 0;    if (yy % b.y) return false;    sum += yy / b.y * b.x;    if (yy % c.y) return false;    sum += yy / c.y * c.x;    if (sum != wx) return false;    return true;}bool judge3(Win a, Win b, Win c, long long wx, long long wy) {    if (check(a, b, c, wx, wy)     || check(b, a, c, wx, wy)     || check(c, a, b, wx, wy)) return true;    return false;}bool judge2(Win a, Win b, Win c, long long wx, long long wy) {    if (judge3(a, b, c, wx, wy))return true;    a.swap();     b.swap();     c.swap();    if (judge3(a, b, c, wy, wx))return true;    return false;}bool check2(Win a, Win b, Win c, Win d) {    long long sum = 0;    if (win.x % d.x) return false;    long long wy = win.x / d.x * d.y;    if (wy >= win.y) return false;    long long yy = win.y - wy;    if (judge2(a, b, c, win.x, yy)) return true;    if (yy % a.y) return false;    sum += yy / a.y * a.x;    if (yy % b.y) return false;    sum += yy / b.y * b.x;    if (yy % c.y) return false;    sum += yy / c.y * c.x;    int flag = 0;    if (sum == win.x) flag = 1;    return flag;}bool check3(Win a, Win b, Win c, Win d) {    long long sum = 0;    long long wy = lcm(a.y, b.y);    if (wy >= win.y) return false;    sum += wy / a.y * a.x;    sum += wy / b.y * b.x;    if (win.x % sum) return false;    if (win.y <= win.x / sum * wy) return false;    long long yy = win.y - win.x / sum * wy;    sum = 0;    if (yy % c.y)       return false;    sum += yy / c.y * c.x;    if (yy % d.y) return false;    sum += yy / d.y * d.x;    if (win.x != sum) return false;    return true;}bool judge() {    if (check1(w[0], w[1], w[2], w[3])) return true;    if (check2(w[0], w[1], w[2], w[3])    || check2(w[0], w[1], w[3], w[2])    || check2(w[0], w[3], w[2], w[1])    || check2(w[3], w[1], w[2], w[0]))       return true;    if (check3(w[0], w[1], w[2], w[3])    || check3(w[0], w[2], w[1], w[3])    || check3(w[0], w[3], w[1], w[2])) return true;    return false;}bool solve() {    if (judge()) return true;    win.swap();     for (long long i = 0; i < 4; i++) w[i].swap();    if (judge()) return true;    return false;}int main() {    long long cas = 0;    while (~scanf("%lld%lld", &win.x, &win.y) && win.x || win.y) {for (long long i = 0; i < 4; i++)    w[i].scanf_();printf("Set %lld: %s\n", ++cas, solve()? "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.