Once upon a time, Toma found himself in a binary cafe. It is a very popular and unusual place. The cafe offers visitors k different delicious desserts. The desserts are numbered from 0 to k−1. The cost of the i-th dessert is 2i coins, because it is a binary cafe! Toma is willing to spend no more than n coins on tasting desserts. At the same time, he is not interested in buying any dessert more than once, because one is enough to evaluate the taste. In how many different ways can he buy several desserts (possibly zero) for tasting? Input The first line of the input contains a single integer t (1≤t≤1000) — the number of test cases. Then follows t lines, each of which describes one test case. Each test case is given on a single line and consists of two integers n and k (1≤n,k≤109) — the number of coins Toma is willing to spend and the number of desserts in the binary cafe. Output Output t integers, the i-th of which should be equal to the answer for the i-th test case — the number of ways to buy desserts for tasting.用c++实现
时间: 2024-02-14 07:11:08 浏览: 118
算法:贪心
思路:
- 根据题目中的描述,可以得知每个点心的价格都是2的幂次方,因此可以从最便宜的点心开始购买,直到购买的点心总价格超过了n,或者购买的点心数量达到了k。
- 购买点心的过程中,可以使用一个变量sum记录购买的点心的总价格,另一个变量cnt记录购买的点心数量。
时间复杂度:$O(\log n)$
C++ 代码:
相关问题
Once upon a time, Toma found himself in a binary cafe. It is a very popular and unusual place. The cafe offers visitors k different delicious desserts. The desserts are numbered from 0 to k−1. The cost of the i-th dessert is 2i coins, because it is a binary cafe! Toma is willing to spend no more than n coins on tasting desserts. At the same time, he is not interested in buying any dessert more than once, because one is enough to evaluate the taste. In how many different ways can he buy several desserts (possibly zero) for tasting? Input The first line of the input contains a single integer t (1≤t≤1000) — the number of test cases. Then follows t lines, each of which describes one test case. Each test case is given on a single line and consists of two integers n and k (1≤n,k≤109) — the number of coins Toma is willing to spend and the number of desserts in the binary cafe. Output Output t integers, the i-th of which should be equal to the answer for the i-th test case — the number of ways to buy desserts for tasting.
题目描述
Toma来到了一个二进制咖啡馆,这里有k种不同的点心,第i个点心的价格是2^i个硬币,Toma最多愿意花n个硬币去品尝不同的点心,求他最多能品尝多少种不同的点心。
输入格式
第一行包含一个整数t,表示测试数据组数。
每组数据占一行,包含两个整数n和k。
输出格式
每组数据输出一个结果,每个结果占一行。
数据范围
1≤t≤1000,
1≤n,k≤10^9
输入样例
2
5 3
7 3
输出样例
1
2
算法1
(贪心) $O(logn)$
时间复杂度
参考文献
python3 代码
C++ 代码
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
请用C语言实现Once upon a time, Toma found himself in a binary cafe. It is a very popular and unusual place. The cafe offers visitors k different delicious desserts. The desserts are numbered from 0 to k−1 . The cost of the i -th dessert is 2i coins, because it is a binary cafe! Toma is willing to spend no more than n coins on tasting desserts. At the same time, he is not interested in buying any dessert more than once, because one is enough to evaluate the taste. In how many different ways can he buy several desserts (possibly zero) for tasting? Input The first line of the input contains a single integer t (1≤t≤1000 ) — the number of test cases. Then follows t lines, each of which describes one test case. Each test case is given on a single line and consists of two integers n and k (1≤n,k≤109 ) — the number of coins Toma is willing to spend and the number of desserts in the binary cafe. Output Output t integers, the i -th of which should be equal to the answer for the i -th test case — the number of ways to buy desserts for tasting.
以下是使用C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, k;
scanf("%d%d", &n, &k);
int ans = 0;
for(int i=0; i<=30; i++)
{
if(n >= (1<<i) - 1 && k >= i+1)
{
ans = i+1;
}
else
{
break;
}
}
int res = 0;
for(int i=0; i<ans; i++)
{
res += (1<<i);
}
if(res <= k)
{
ans += (k-res);
}
printf("%d\n", ans);
}
return 0;
}
```
程序先输入了t表示测试用例的数量,然后使用while循环读入t个测试用例。每个测试用例包含两个整数n和k。接下来,程序使用一个for循环,从0到30枚举甜点的数量i,如果n大于等于2^i-1并且k大于等于i+1,则说明可以购买i+1个甜点,同时更新ans的值。如果n小于2^i-1或者k小于i+1,则退出循环。接下来,程序使用另一个for循环,计算购买ans个甜点的花费res。如果res小于等于k,则说明可以购买所有这ans个甜点,此时ans就是答案。否则,程序需要再购买k-res个甜点,将ans加上这个数量即可。最后,程序输出ans作为答案。
阅读全文