ZOJ 3180 Number Game(數學)__數學

來源:互聯網
上載者:User

題目連結:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3221


Gordon is recently reading about an interesting game. At the beginning of the game, there are three positive numbers written on a blackboard. In each round, you are asked to delete one of these three numbers. And you should write the sum of the remaining two numbers minus one back to the blackboard. For example, there are three numbers, 2, 3 and 10 on the blackboard. If you decide to change the number 3 in this round, you will delete the number 3 first and put the number 11=2+10-1 back to the blackboard. So it would be 2, 10 and 11 on the blackboard then. The target of this game is to reach a given number in the minimal steps.

One day, when Gordon was playing the game, his best friend Mike came in. Mike saw that the numbers on the blackboard were 17, 1967 and 1983, and asked Gordon if he had played this game from the beginning numbers 3, 3 and 3. Since Gordon didn't leave a trace on the game, he asked you, a young brilliant programmer, to help them check out if Mike made the right guess.

Input

The first line of the input contains an integer T (T < 200), indicating the number of cases. Each test case consists of a line with six positive integers. The first three are the numbers currently on Gordon's blackboard and the last three are the numbers that Mike guessed. It is guaranteed that every number in a game is positive and is less than 1,000,000.

Output

For each test case, you should write a single word in a line. If it is possible to get Gordon's numbers from Mike's guess, you would give the word "Yes". Otherwise you need to output the word "No".

Sample Input

26 10 15 7 13 2617 1967 1983 3 3 3

Sample Output

NoYes
Author:  GAO, Fei
Contest:  The 9th Zhejiang University Programming Contest


題意:

求後面三個數能否通過一個規則 變換為前面的三個數字。

規則:

最後三個數中每次可以選擇一個數字刪掉,添加一個數字為:剩下的兩個數位和減去一。

PS:

我們可以從前面三個數字,逆推回去,只要逆推回去等於 後面三個數字刪掉其中一個數字後的情況即可。

代碼如下:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int a[4],b[4];long long num[100000];int dp[5][3];int main(){    int n,t;    cin>>t;    while(t--)    {        for(int i=0; i<3; i++)            cin>>a[i];        for(int i=0; i<3; i++)            cin>>b[i];//b- a        int flag=1;        sort(b,b+3);        dp[0][0]=b[0];        dp[0][1]=b[1];        dp[0][2]=b[2];        //qu0        dp[1][0]=b[1]+b[2]-1;        dp[1][1]=b[1];        dp[1][2]=b[2];        //qu1        dp[2][0]=b[0];        dp[2][1]=b[0]+b[2]-1;        dp[2][2]=b[2];        //qu0        dp[3][0]=b[0];        dp[3][1]=b[1];        dp[3][2]=b[0]+b[1]-1;        for(int i=0; i<4; i++)            sort(dp[i],dp[i]+3);        while(1)        {            sort(a,a+3);            for(int j=0; j<4; j++)            {                flag=1;                for(int k=0; k<3; k++)                {                    if(dp[j][k]!=a[k])                        flag=0;                }                if(flag)                    break;            }            if(flag==1)//PIPEI            {                puts("Yes");                break;            }            if(a[1]+1-a[0]==a[2])//死迴圈            {                puts("No");                break;            }            a[2]=a[1]+1-a[0];            flag=1;            for(int j=0; j<3; j++)//負數            {                if(a[j]<=0)                    flag=0;            }            if(flag==0)            {                puts("No");                break;            }        }    }    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.