题意:
Alice and Bob are on an adventure trip. Deep in the woods they discover a mysterious deep cave which they enter flutteringly. They find an old console with a giant bar of gold in it. On the bar, there is a number n. Both tried to carry the gold out the cave, but it was still to heavy for one of them.
Suddenly a little fairy appears in the corner of the cave and approaches Alice and Bob: “This gold is heavy. It weights 2^n femto-grams (10^−15) and nncan reach 62.”
Bob answered: “What luck! Alice’s knapsack can carry up to aa femto-grams and mine bb femto-grams with a+b=2^n.” Alice interjected: “But how can we divide the gold?”
Fairy: “I can help you with a spell that can burst one piece of gold into two equally weighted ones. But for each single spell, the cave will be locked one additional day.”
Alice consults with Bob to use the help of the fairy and take all of the gold. How long will they be trapped if they are clever?
Input
The input starts with the number t≤1000 of test cases.
Then tt lines follow, each describing a single test case consisting of three numbers n,an,a and bb with a,b≥1,a+b=2^n, and 1≤n≤62.
Output
Output one line for every test case with the minimal number of days that Alice and Bob are locked in the cave.
样例输入
3
2 2 2
2 1 3
10 1000 24
样例输出
1
2
7
翻译成汉语大概意思就是有T组样例,每组样例有一个数是2^n,把它分成a,b,每次只能把一个数分成两个该数的一半,问最少分多少次能够分成a,b巴拉巴拉~
思路:
我们只需要得到a,b的最小值t,然后循环把2^n累除2,若比t小就用t-该数,直到t为0为止
代码:
#include <stdio.h>
#include <algorithm>
using namespace std;
long long n;
long long quick(long long a, long long b) {
long long ans = 1;
while(b) {
if(b&1) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
int main() {
int T;
long long n, a, b;
scanf("%d", &T);
while(T--) {
scanf("%lld%lld%lld", &n, &a, &b);
long long res = quick(2, n);
long long minn = min(a, b);
int num = 0;
while(minn) {
res /= 2;
num++;
if(res <= minn) {
minn -= res;
}
}
printf("%d\n", num);
}
return 0;
}
如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢