D. Buying Jewels
D. Buying Jewels
Alice has coins and wants to shop at Bob's jewelry store. Today, although Bob has not set up the store yet, Bob wants to make sure Alice will buy exactly jewels. To set up the store, Bob can erect at most stalls (each containing an unlimited amount of jewels) and set the price per jewel for each stall to be an integer number of coins between and .
Fortunately, Bob knows that Alice buys greedily: and she will go to stall , buy as many jewels as possible, then go to stall , buy as many jewels as possible, and so on until the last stall. Knowing this, Bob can choose the number of stalls to set up, as well as set the price for each stall so that Alice buys exactly jewels. Help Bob fulfill the task, or determine if it is impossible to do so.
Note that Alice does not need to spend all her coins.
Input
Each test contains multiple test cases. The first line contains an integer () — the number of test cases. The description of the test cases follows.
Each test case contains two positive integers and () — the number of coins Alice has and the number of jewels Bob wants Alice to have bought at the end.
Output
For each test case, print on one line "YES" if Bob can erect at most stalls and set the prices for the stalls such that Alice buys exactly jewels, or "NO" if it is impossible to do so.
If the answer is "YES", on the second line, print an integer () — the number of stalls to be set up by Bob. On the third line, print positive integers ( that represent such a satisfactory pricing , where is the price per jewel for stall . If there are multiple such 's, print any of them.
Example
input
3
7 3
6 4
255 8
output
YES
10
2 3 4 5 6 7 8 9 10 11
NO
YES
8
128 64 32 16 8 4 2 1
Note
In the first test case, at the first stall, Alice buys jewels and is left with coin. This is not enough to buy any jewels for any of the remaining stalls, so Alice buys exactly jewels at the end.
In the third test case,
- At the first stall, Alice buys jewel and is left with coins.
- At the second stall, Alice buys jewel and is left with coins.
- At the third stall, Alice buys jewel and is left with coins.
- At the fourth stall, Alice buys jewel and is left with coins.
- At the fifth stall, Alice buys jewel and is left with coins.
- At the sixth stall, Alice buys jewel and is left with coins.
- At the seventh stall, Alice buys jewel and is left with coin.
- At the eighth stall, Alice buys jewel and is left with coins.
Therefore, Alice buys exactly jewels in total.
解题思路
显然当 时无解。而当 时,只需令 即可。下面主要讨论当 且 时的情况。
首先必然有 ,当 确定后,可购买的物品数量为 ,剩余的金额为 。因此最多能购买的物品数量就是 。事实上可以证明,对于 ,。
证明
命题:对于 ,。
首先当 时,有 ,命题成立。
假设当 时命题成立,下证 命题同样成立,即对于 ,。
- 对于 的情况:
- ,那么 。
- ,那么 。
- 对于 的情况:
。
因此对于 , 成立。
根据归纳法原理,命题对于所有满足 的正整数都成立。
因此如果 ,则无解。否则 必然有解。题解给出的构造方法是,用 的价格购买一个物品,也就是 ,使得剩余的金额恰好有 (以价格 购买剩余的 个物品),从而推出 ,恰好保证只能购买一个物品。
AC 代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve() {
LL n, m;
scanf("%lld %lld", &n, &m);
if (n < m) printf("NO\n");
else if (n % m == 0) printf("YES\n1\n%lld\n", n / m);
else if (m > (n + 1) / 2) printf("NO\n");
else printf("YES\n2\n%lld 1\n", n - m + 1);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
参考资料
Codeforces Global Round 25 Editorial:https://codeforces.com/blog/entry/128116
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18131872
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效