Educational Codeforces Round 108 (Rated for Div. 2) B. The Cake Is a Lie(简单DP)
There is a n×m grid. You are standing at cell (1,1) and your goal is to finish at cell (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). You can:
- move right to the cell (x,y+1) — it costs 𝑥x burles;
- move down to the cell (x+1,y) — it costs 𝑦y burles.
Can you reach cell (n,m) spending exactly k burles?
Input
The first line contains the single integer 𝑡t (1≤𝑡≤1001≤t≤100) — the number of test cases.
The first and only line of each test case contains three integers 𝑛n, 𝑚m, and 𝑘k (1≤n,m≤100; 0≤k≤10^4) — the sizes of grid and the exact amount of money you need to spend.
Output
For each test case, if you can reach cell (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).
Example
input
Copy
6
1 1 0
2 2 2
2 2 3
2 2 4
1 4 3
100 100 10000
output
Copy
YES
NO
YES
NO
YES
NO
简单dp, dp[i, j, k]表示从(1, 1)能否花费k走到(i, j),转移方程不难推出(见代码
注意数组开int会mle。
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
bool dp[101][101][10001];//不用开int
int main() {
int t;
cin >> t;
memset(dp, 0, sizeof(dp));
dp[1][1][0] = 1;
for(int i = 1; i <= 100; i++) {
for(int j = 1; j <= 100; j++) {
for(int w = 1; w <= 10000; w++) {
if(w - j >= 0) dp[i][j][w] |= dp[i - 1][j][w - j];
if(w - i >= 0) dp[i][j][w] |= dp[i][j - 1][w - i];
}
}
}
while(t--) {
cin >> n >> m >> k;
if(dp[n][m][k]) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!