首页 > 其他分享 >D. Buying Jewels

D. Buying Jewels

时间:2024-04-12 20:45:18浏览次数:16  
标签:lceil right frac Alice Jewels stall Buying left

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

标签:lceil,right,frac,Alice,Jewels,stall,Buying,left
From: https://www.cnblogs.com/onlyblues/p/18131872

相关文章

  • CF103E Buying Sets(最大权闭合子图模型)
    题意简述有\(n\)个元素和\(n\)个集合,保证任意\(k\)个集合的并\(\gek\)。每个集合有权值\(a_i\),你需要选出一些集合使得集合数等于集合并大小,并在此基础上最小化选出的集合权值和。\(n\le300,|a_i|\le10^6\)。分析将集合和元素看成物品,我们发现,若选择了一个集合,则......
  • B. Buying gifts[贪心]
    Problem-1801B-Codeforces题意是需要给两个人买礼物,有n个商店,每个商店只能给一个人买,而且每个商店给两个人买的礼物的价钱也可能不同,问给两人买的礼物的最大价格之差最小是多少。我们考虑这种情况。如果当前给b买的礼物最大值为x,那么那些商店里给b礼物价格小于等于x的我们......
  • [USACO08NOV]Buying Hay S
    [USACO08NOV]BuyingHayS题目描述FarmerJohnisrunningoutofsuppliesandneedstopurchaseH(1<=H<=50,000)poundsofhayforhiscows.HeknowsN(1<=N<=100)haysuppliersconvenientlynumbered1..N.Supplierisellspackagesthatcon......
  • leetcode 771. Jewels and Stones
    You'regivenstrings J representingthetypesofstonesthatarejewels,and S representingthestonesyouhave. Eachcharacterin Sisatypeofstoneyouhave. Youwanttoknowhowmanyofthestonesyouhavearealsojewels.Thelettersin J areg......
  • 洛谷 P4544 [USACO10NOV]Buying Feed G - 题解
    https://www.luogu.com.cn/problem/P4544感觉是很没意思的DP+DS优化,以前做过几道更难的题:https://codeforces.com/contest/1788/problem/Ehttps://atcoder.jp/contests/abc288/tasks/abc288_f这种题只要是让我写总是能把代码整的伤痕累累(逃第一眼:我艹不就是一个sbDP吗......
  • P4544 Buying Feed G
    dp&单调队列优化 这个题:k<=i,决策点k可以等于i,所以在i入队后递推#include<iostream>#include<algorithm>#include<cstring>usingnamespacestd;constintN=503,M=1e4+2;#defineintlonglongintn,m,E,X[N],F[N],C[N],f[N][M];intq[N],hh,tt;......
  • [leeccode]771. Jewels and Stones
    J representingthetypesofstonesthatarejewels,and S representingthestonesyouhave. Eachcharacterin Sisatypeofstoneyouhave. Youwantto......
  • Codeforces Round 644 (Div. 3) D. Buying Shovels(数论)
    https://codeforces.com/contest/1360/problem/DD.BuyingShovels题目大意:一个人想买正好n把铲子。店内有k种包装的铲子:第i种包装正好由i把铲子组成(1≤i≤k)。这家......
  • D. Buying gifts
    D.BuyinggiftsLittleSashahastwofriends,whomhewantstopleasewithgiftsontheEighthofMarch.Todothis,hewenttothelargestshoppingcenterin......
  • Pros and Cons of buying Premium Tech Tool
    Ifyou’veworkedonaVolvoorMackbefore,chancesareyou’veusedPremiumTechToolatsomepointinthepast.Iftheprogramactuallyworks,it’sagreat......