hdu 4596 Yet another end of the world 线性同余方程组

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4596

 

日本人白书P293面最上的公式

 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>

using namespace std;

typedef long long ll;

const int maxn = 1010;

int x[maxn], y[maxn], z[maxn];

int gcd(int a, int b)
{
    if(b == 0)
        return a;
    return gcd(b, a % b);
}

int main()
{
    //freopen("in.txt", "r", stdin);

    int n;
    while(scanf("%d", &n) == 1)
    {
        for(int i = 0; i < n; i++)
            scanf("%d%d%d", &x[i], &y[i], &z[i]);

        bool ans = true;
        for(int i = 0; i < n-1 && ans; i++)
            for(int j = i + 1; j < n && ans; j++)
            {
                int e = gcd(x[i], x[j]);
                int maxnum = max(abs(z[j] - y[i]), abs(z[i] - y[j]));

                int minnum;
                if(z[j] > z[i])
                {
                    minnum = max(y[j] - z[i], 0);
                }
                else
                {
                    minnum = max(y[i] - z[j], 0);
                }

                int k = minnum / e;
                if(k * e == minnum)
                {
                    ans = false;
                    break;
                }
                else if((k+1) * e <= maxnum)
                {
                    ans = false;
                    break;
                }
            }

        if(ans)
            printf("Can Take off\n");
        else
            printf("Cannot Take off\n");
    }

    return 0;
}

 

posted @ 2015-05-26 09:11  地鼠地鼠  阅读(184)  评论(0编辑  收藏  举报