Codeforces Round 1519 B The Cake Is a Lie

这是一个关于算法的问题,描述了如何在n×m的网格中从(1,1)移动到(n,m),每次移动花费不同,并且总花费必须恰好为k。题目提供了样例输入和输出,以及一个简单的判断条件来确定是否能按照预算完成路径。解答关键在于找到正确的移动策略。

题目描述

There is a n×mn×m grid. You are standing at cell (1,1)(1,1) and your goal is to finish at cell (n,m)(n,m).

You can move to the neighboring cells to the right or down. In other words, suppose you are standing at cell (x,y)(x,y). You can:

  • move right to the cell (x,y+1)(x,y+1) — it costs x burles;
  • move down to the cell (x+1,y)(x+1,y) — it costs y burles.

Can you reach cell (n,m)(n,m) spending exactly k burles?

输入

The first line contains the single integer t (1≤t≤100) — the number of test cases.

The first and only line of each test case contains three integers nn, mm, and k (1≤n,m≤1001≤n,m≤100; 0≤k≤10^4) — the sizes of grid and the exact amount of money you need to spend.

输出

For each test case, if you can reach cell (n,m)(n,m) spending exactly k burles, print YES. Otherwise, print NO.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

样例

输入 : 

6
1 1 0
2 2 2
2 2 3
2 2 4
1 4 3
100 100 10000

输出 :

YES
NO
YES
NO
YES
NO

思路分析

此题如果要追求速度,就要找规律:((m-1)*1+(n-1)*m)==s.


#include <iostream>
using namespace std;
int main ()  {
	int n,m,s;
	int a;
	cin>>a;
	string b[a];
	for (int i=0;i<a;i++){
		cin>>n>>m>>s;
		if (((m-1)*1+(n-1)*m)==s){
			b[i]="YES";
		}
		else 
			b[i]="NO";
	}
	for (int i=0;i<a;i++){
		cout<<b[i]<<endl;
	}
	return 0;
}

                                                                仅供参考,请多指教

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值