ZOJ 3180 Number Game(数学啊 )

题目链接: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

2
6 10 15 7 13 26
17 1967 1983 3 3 3

Sample Output

No
Yes

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;
}
时间: 2024-08-09 18:07:22

ZOJ 3180 Number Game(数学啊 )的相关文章

ZOJ 3180 Number Game(模拟,倒推)

题目 思路: 先倒推!到最后第二步,然后: 初始状态不一定满足这个状态.所以我们要先从初始状态构造出它出发的三种状态.那这三种状态跟倒推得到的状态比较即可. #include<stdio.h> #include<string.h> #include <algorithm> using namespace std; int t,a[5],b[5]; int main() { scanf("%d",&t); while(t--) { scanf(

ZOJ 2836 Number Puzzle ( 容斥原理 )

ZOJ 2836 Number Puzzle( 容斥原理 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define CLR( a, b ) memset( a, b, sizeof(a) ) int m, n, A[11]; LL gcd( LL a, LL b ) { return b == 0 ? a :

HDU 1005 Number Sequence (数学规律)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 104190    Accepted Submission(s): 25232 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

[容斥原理] zoj 2836 Number Puzzle

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 Number Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers th

HDU 1018 Big Number (简单数学)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25649    Accepted Submission(s): 11635 Problem Description In many applications very large integers numbers are required. Some of these

ZOJ 3327 Friend Number(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3327 Friend Number Time Limit: 1 Second      Memory Limit: 32768 KB Given a positive integer x, let P(x) denotes the product of all x's digits. Two integers x and y are friend numbers

HDU 1018-Big Number(数学)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26383    Accepted Submission(s): 12006 Problem Description In many applications very large integers numbers are required. Some of thes

PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segments (20 分) Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3,

HDU 6216 A Cubic number and A Cubic Number【数学思维+枚举/二分】

Problem Description A cubic number is the result of using a whole number in a multiplication three times. For example, 3×3×3=27 so 27 is a cubic number. The first few cubic numbers are 1,8,27,64 and 125 . Given an prime number p . Check that if p is