枚举假设: n = 10 , k = 4,i 就有四种情况 2^0 ,2^1 , 2^2 , 2^3 (1,2,4,8)
便有0 0 0 0;0 0 0 1; 0 0 1 0……1 1 1 1种
花费的硬币数量位0到2^i-1,即为2^i种
自己硬币数量位0到n,即为n+1种
具体代码实现:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while (t--) {
int n, i;
scanf("%d%d", &n, &i);
// 将k的值限制在30或以下,确保1 << i的结果不会超过int类型能够表示的最大正整数,从而避免了整数溢出的问题。
i = min(30, i);
int cnt = (1 << i) - 1;
printf("%d\n", min(cnt, n) + 1);
}
}
int cnt = (1 << i) - 1;