zoukankan      html  css  js  c++  java
  • FJUT 倒水(倒水问题)题解

    题意:开学了, fold拿着两个无刻度, 容量分别是5L和7L的量筒来问Anxdada, 说水是无限的, 并且可以无限次将杯子装满或者清空, 那怎么用这个两个量筒倒出恰好4L水了? 我说简单啊, 先装满7L的量筒, 倒给5L的量筒, 然后7L的还剩2L, 接着把2L的倒进5L的量筒内, 然后再装满7L的量筒, 再倒满5L的量筒, 此时7L的量筒内就恰好剩4L啦. fold又说, 那我任意指定两个量筒的容量和一直最终要得到的容量, 你知道怎么倒吗? 这个就难到Anxdada了, 请你帮帮他! 为了简化问题, 求给定的两个容量的量筒, 和一个最终要得到的容量, 输出是否能通过一定的步骤得到即可(YES/NO). 

    单组测试数据

    第一行一个n, 表示数据总数

    后面接着n行数据, 每行以格式x y z输入

    x y 代表两个无刻度的量筒的容量, z 表示最终要得到的容量

    (n<1000, 0 < max(x, y) < 2000, 0 < z < 2000)

    思路:显然首先不能倒出大于x+y的水。假设让我们倒出z的水,那么必然z是由x、x + y、x - y(x > y)、y组成,所以设z = A1 * x + A2 * (x + y) + A3 * (x - y) + A4 * y,整理得A * x + B * y = z,所以有解即方程有非负整数解,那么根据扩展欧几里得可知z整除gcd(x,y)即有解。

    代码:

    #include<set>
    #include<map>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<string>
    #include<cstdio>
    #include<cstring>
    #include<sstream>
    #include<iostream>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    const int maxn = 100000 + 10;
    const int MOD = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    int gcd(int a, int b){
        return b == 0? a : gcd(b, a % b);
    }
    int main(){
        int x, y, z, t;
        scanf("%d" ,&t);
        while(t--){
            scanf("%d%d%d", &x, &y, &z);
            if(z % gcd(x, y) != 0 || z > x + y) printf("NO
    ");
            else printf("YES
    ");
        }
        return 0;
    }
  • 相关阅读:
    Visual Studio 2008 菜单:工具+选项+文本编辑器+HTML+格式,选中“键入时插入属性值引号”
    itemarray的意思
    SQL技巧大全
    IIS调用com组件的权限问题
    网站快速备案法(1小时)
    ASP.NET 2.0中WEB应用程序的部署
    c#中MessageBox的使用
    推荐一款DataGridView的打印解决方案
    使用C#格式化字符串
    关于MSSQL导入导出时主键与约束丢失的问题解决
  • 原文地址:https://www.cnblogs.com/KirinSB/p/10464925.html
Copyright © 2011-2022 走看看