Aaronson
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 346 Accepted Submission(s): 210
Problem Description Recently, Peter saw the equation x
+2x
1
+4x
2
+...+2
m
x
m
=n
. He wants to find a solution (x
,x
1
,x
2
,...,x
m
)
in such a manner that ∑
i=0
m
x
i
is minimum and every x
i
( 0≤i≤m
) is non-negative. Input There are multiple test cases. The first line of input contains an integer T
(1≤T≤10
5
)
, indicating the number of test cases. For each test case:
The first contains two integers n
and m
(0≤n,m≤10
9
)
. Output For each test case, output the minimum value of ∑
i=0
m
x
i
. Sample Input
10
1 2
3 2
5 2
10 2
10 3
10 4
13 5
20 4
11 11
12 3
Sample Output
1
2
2
3
2
2
3
2
3
2
Source BestCoder Round #84 Recommend wange2014 | We have carefully selected several similar problems for you: 5751 5750 5749 5748 5746
题解:模拟一下就好了。。。
AC代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int ans=0;
int d =1 <<(min(m,30));
while(n)
{
ans+=n/d;
n-=n/d*d;
d/=2;
}
printf("%d\n",ans);
}
return 0;
}