HDU 4596 Yet another end of the world(解一阶不定方程)

Yet another end of the world

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 920    Accepted Submission(s):
400

Problem Description

In the year 3013, it has been 1000 years since the
previous predicted rapture. However, the Maya will not play a joke any more and
the Rapture finally comes in. Fortunately people have already found out
habitable planets, and made enough airships to convey all the human beings in
the world. A large amount of airships are flying away the earth. People all bear
to watch as this planet on which they have lived for millions of years.
Nonetheless, scientists are worrying about anther problem…
As we know that
long distance space travels are realized through the wormholes, which are given
birth by the distortion of the energy fields in space. Airships will be driven
into the wormholes to reach the other side of the universe by the suction
devices placed in advance. Each wormhole has its configured attract parameters,
X, Y or Z. When the value of ID%X is in [Y,Z], this spaceship will be sucked
into the wormhole by the huge attraction. However, the spaceship would be tear
into piece if its ID meets the attract parameters of two wormholes or more at
the same time.
All the parameters are carefully adjusted initially, but some
conservative, who treat the Rapture as a grain of truth and who are reluctant to
abandon the treasure, combine with some evil scientists and disrupt the
parameters. As a consequence, before the spaceships fly into gravity range, we
should know whether the great tragedy would happen or not. Now the mission is on
you.

Input

Multiple test cases, ends with EOF.
In each case,
the first line contains an integer N(N<=1000), which means the number of the
wormholes.
Then comes N lines, each line contains three integers
X,Y,Z(0<=Y<=Z<X<2*109).

Output

If there exists danger, output “Cannot Take off”, else
output “Can Take off”.

Sample Input

2
7 2 3
7 5 6
2
7 2 2
9 2 2

Sample Output

Can Take off
Cannot Take off

题目大意:

  这道题是说,给你n个x,y,z。问是否存在某个id,使得 id%x>=y&&id%x<=z

  y <= z < 2*10^9

解题思路:

  设  id/x1 = a1; id/x2 = a2;

   id%x1=b1; id%x2=a2;

-> id = x1*a1+b1,  id = x2*a2+b2

->x1*a1+b1 = x2*a2+b2;

->x1*a1-x2*a2 = b2-b1;

现在就来看看该怎么求解这个线性方程组了。首先,线性方程组有整数解的条件是b2-b1是gcd(x1,x2)的整数倍数。

那么,我们就可以得到只需要判断一下两个区间[yi,zi]和[yj,zj]的差值区间[yj-zi,zj-yi]是否可能存在gcd(xi,xj)的倍数的结论

代码:

# include<cstdio>
# include<iostream>

using namespace std;

# define MAX 1234

int x[MAX],y[MAX],z[MAX];
int n;

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

int ok ( int t,int l,int r )
{
    if ( l%t==0||r%t==0 )
        return 1;
    if ( l<0&&r>=0 )
        return 1;
    if ( r/t-l/t > 0 )
        return 1;
    return 0;
}

int solve()
{
    for ( int i = 1;i <= n;i++ )
    {
        for ( int j = i+1;j <= n;j++ )
        {
            int tmp = gcd(x[i],x[j]);
            if ( ok(tmp,y[i]-z[j],z[i]-y[j]) )
                return 1;
        }
    }
    return 0;
}

int main(void)
{
    while ( scanf("%d",&n)!=EOF )
    {
        for ( int i = 1;i <= n;i++ )
            scanf("%d%d%d",&x[i],&y[i],&z[i]);
        if ( solve() )
            puts("Cannot Take off");
        else
            puts("Can Take off");
    }

    return 0;
}

  

时间: 2024-10-30 00:26:44

HDU 4596 Yet another end of the world(解一阶不定方程)的相关文章

HDU 4596 - Yet another end of the world(扩展欧几里得)

题意:给定一系列的虫洞,每个虫洞都有自己的x,y,z,当你的 id 对 x 取余后结果落在[ y,z ]区间内,则会被吸引,被两个或两个以上的虫洞吸引会有危险,求能否宇宙飞船能否起飞. 枚举每两个虫洞,有 id - k1 * x1 = u id - k2 * x2 = v 其中k1与k2分别为 id / x1 与 id / x2,u与v分别为求余后的结果. 两式相减得  k2 * x2 - k1 * x1 = u - v 根据扩展欧几里得,判断 u - v 的取值是否能取到 gcd(x1, x2

HDU 1503 Advanced Fruits(LCS变形且输出解)

http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给你两个字符串s1和s2, 要你输出它们的并串s. 其中s1是s的一个子序列且s2也是s的一个子序列且s是所有符合前面要求的最短字符串. 分析: 令dp[i][j]==x表示s1串的前i个字符和s2串的前j个字符组成的串的LCS长度为x. 我们先求出LCS的dp数组值. 然后按照POJ2250: http://blog.csdn.net/u013480600/article/details/40

HDU 4049 Tourism Planning (状压dp 详解)

Tourism Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1125    Accepted Submission(s): 487 Problem Description Several friends are planning to take tourism during the next holiday. Th

HDU 2639 (01背包第k优解)

/* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并且去重生成ij状态的前k优解 */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 using namespace std; int T,n,m,k,c[maxn],v[maxn

HDU 4115 Eliminate the Conflict(2-sat 判解存在性)

题意: 有两个人玩一个石头剪刀布的游戏,两个人连续玩N轮,给出其中一个人的N轮出的情况和该人对另外一个人的一些限制条件,有两种限制:每种限制表示为:(a,b,c) ,如果c==0 则表示该人对另外一个人的限制为第a局和第b局出的应该一样,如果c==1表示不一样,问另外一个人是否有赢(规定每轮都不输就称赢)的可能. 思路:所以可以推出每轮必须出能平或赢的动作(两种选择)所以是2-sat.再找到约束关系即可 #include<cstdio> #include<iostream> #in

2013 ACM-ICPC南京赛区全国邀请赛

题目链接:http://acm.hdu.edu.cn/search.php?field=problem&key=2013 ACM-ICPC南京赛区全国邀请赛--题目重现&source=1&searchmode=source A(HDU4586) Play the Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1

转自kuangbin的AC自动机(赛前最后一博)

有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配.           AC自动机 其实 就是创建了一个状态的转移图,思想很重要.           推荐的学习链接: http://acm.uestc.edu.cn/bbs/read.php?tid=4294 http://blog.csdn.net/niushuai666/article/details/7002823 http://hi.baidu.com/nial

nyoj655 光棍的yy(大数的斐波那契数)

题目655 题目信息 运行结果 本题排行 讨论区 光棍的yy 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 yy经常遇见一个奇怪的事情,每当他看时间的时候总会看见11:11,这个很纠结啊. 现在给你m个1,你可以把2个1组合成一个2,这样就不是光棍了,问这样的组合有多少种?? 例如(111  可以拆分为 111 12 21  有三种) 输入 第一行输入一个n表示有n个测试数据 以下n行,每行输入m个1 (1 <= n,m <= 200) 输出 输出这种组合种数,

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;