D. Buying Jewels

onlyblues發表於2024-04-12

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

相關文章