D. Buying Jewels

D. Buying Jewels

Alice has $n$ 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 $k$ jewels. To set up the store, Bob can erect at most $60$ 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 $1$ and $10^{18}$.

Fortunately, Bob knows that Alice buys greedily: and she will go to stall $1$, buy as many jewels as possible, then go to stall $2$, 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 $k$ 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 $t$ ($1 \le t \le 1000$) — the number of test cases. The description of the test cases follows.

Each test case contains two positive integers $n$ and $k$ ($1 \le n, k \le 10^{18}$) — 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 $60$ stalls and set the prices for the stalls such that Alice buys exactly $k$ jewels, or "NO" if it is impossible to do so.

If the answer is "YES", on the second line, print an integer $s$ ($1 \le s \le 60$) — the number of stalls to be set up by Bob. On the third line, print $s$ positive integers $p_1, p_2, \ldots, p_s$ ($1 \le p_i \le 10^{18})$ that represent such a satisfactory pricing $p$, where $p_i$ is the price per jewel for stall $i$. If there are multiple such $p$'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 $3$ jewels and is left with $1$ coin. This is not enough to buy any jewels for any of the remaining stalls, so Alice buys exactly $3$ jewels at the end.

In the third test case,

  • At the first stall, Alice buys $1$ jewel and is left with $127$ coins.
  • At the second stall, Alice buys $1$ jewel and is left with $63$ coins.
  • At the third stall, Alice buys $1$ jewel and is left with $31$ coins.
  • At the fourth stall, Alice buys $1$ jewel and is left with $15$ coins.
  • At the fifth stall, Alice buys $1$ jewel and is left with $7$ coins.
  • At the sixth stall, Alice buys $1$ jewel and is left with $3$ coins.
  • At the seventh stall, Alice buys $1$ jewel and is left with $1$ coin.
  • At the eighth stall, Alice buys $1$ jewel and is left with $0$ coins.

Therefore, Alice buys exactly $8$ jewels in total.

 

解题思路

  显然当 $n < k$ 时无解。而当 $k \mid n$ 时,只需令 $p_1 = \frac{n}{k}$ 即可。下面主要讨论当 $n > k$ 且 $k \nmid n$ 时的情况。

  首先必然有 $p_1 \geq 2$,当 $p_1$ 确定后,可购买的物品数量为 $\left\lfloor \frac{n}{p_1} \right\rfloor$,剩余的金额为 $n \bmod p_1$。因此最多能购买的物品数量就是 $\left\lfloor \frac{n}{p_1} \right\rfloor+ n \bmod p_1$。事实上可以证明,对于 $\forall i \in [2, n]$,$\left\lfloor \frac{n}{i} \right\rfloor + n \bmod i \leq \left\lceil \frac{n}{2} \right\rceil$。

证明

  命题:对于 $\forall i \in [2, n]$,$\left\lfloor \frac{n}{i} \right\rfloor + n \bmod i \leq \left\lceil \frac{n}{2} \right\rceil$。

  首先当 $n=2$ 时,有 $\left\lfloor \frac{2}{2} \right\rfloor + 2 \bmod 2 = 1 \leq \left\lceil \frac{2}{2} \right\rceil$,命题成立。

  假设当 $n > 2$ 时命题成立,下证 $n+1$ 命题同样成立,即对于 $\forall i \in [2, n+1]$,$\left\lfloor \frac{n+1}{i} \right\rfloor + (n+1) \bmod i \leq \left\lceil \frac{n+1}{2} \right\rceil$。

  1. 对于 $i \in [2, n]$ 的情况:
    • $i \mid n+1$,那么 $\left\lfloor \frac{n+1}{i} \right\rfloor + (n+1) \bmod i = \left\lfloor \frac{n}{i} \right\rfloor + 1 \leq \left\lfloor \frac{n}{i} \right\rfloor + n \bmod i \leq \left\lceil \frac{n}{2} \right\rceil \leq \left\lceil \frac{n+1}{2} \right\rceil$。
    • $i \nmid n+1$,那么 $\left\lfloor \frac{n+1}{i} \right\rfloor + (n+1) \bmod i = \left\lfloor \frac{n}{i} \right\rfloor + n \bmod i + 1 \leq \left\lceil \frac{n}{2} \right\rceil + 1 = \left\lceil \frac{n+1}{2} \right\rceil$。
  2. 对于 $i = n+1$ 的情况:

$\left\lfloor \frac{n+1}{n+1} \right\rfloor + (n+1) \bmod (n+1) = 1 \leq \left\lceil \frac{n+1}{2} \right\rceil$。

  因此对于 $\forall i \in [2, n+1]$,$\left\lfloor \frac{n+1}{i} \right\rfloor + (n+1) \bmod i \leq \left\lceil \frac{n+1}{2} \right\rceil$ 成立。

  根据归纳法原理,命题对于所有满足 $n \geq 2$ 的正整数都成立。

  因此如果 $k > \left\lceil \frac{n}{2} \right\rceil$,则无解。否则 $k \leq \left\lceil \frac{n}{2} \right\rceil$ 必然有解。题解给出的构造方法是,用 $p_1$ 的价格购买一个物品,也就是 $\left\lfloor \frac{n}{p_1} \right\rfloor = 1$,使得剩余的金额恰好有 $n - p_1 = k-1$(以价格 $1$ 购买剩余的 $k-1$ 个物品),从而推出 $p_1 = n-k+1 \geq n - \left\lceil \frac{n}{2} \right\rceil + 1 = \left\lceil \frac{n}{2} \right\rceil$,恰好保证只能购买一个物品。

  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

posted @ 2024-04-12 20:37  onlyblues  阅读(16)  评论(0编辑  收藏  举报
Web Analytics