Aaronson

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 231    Accepted Submission(s): 149

Problem Description
Recently, Peter saw the equation x0+2x1+4x2+...+2mxm=n. He wants to find a solution (x0,x1,x2,...,xm) in such a manner that i=0mxi is minimum and every xi (0im) is non-negative.
 
Input
There are multiple test cases. The first line of input contains an integer T (1T105), indicating the number of test cases. For each test case:

The first contains two integers n and m (0n,m109).
 
Output
For each test case, output the minimum value of i=0mxi.
 
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

题目出处:中文翻译

从2的最大次幂开始除以保证最终得数最小。

附AC代码:

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 
 5 /*
 6 int Pow(int a,int b){
 7     int temp=1;
 8     for(int i=0;i<b;i++){
 9         temp*=a;
10     }
11     return temp;
12 }
13 */
14 
15 int main(){
16     int t,m,n,k,sum;
17     cin>>t;
18     int s[31];
19     s[0]=1;
20     for(int i=1;i<31;i++){//打表 
21         s[i]=s[i-1]+s[i-1];
22     }
23     while(t--){
24         cin>>n>>m;
25         sum=0;
26         for(int j=min(m,30);j>=0;j--)  //当n可被除时,从最大2的最大次幂开始除以保证结果最小 
27             if(n>=s[j])  
28             {  
29                 k=n/s[j];  
30                 sum+=k;  
31                 n-=k*s[j];  
32             }  
33             cout<<sum<<endl;
34     }
35     return 0;
36 } 
原文地址:https://www.cnblogs.com/Kiven5197/p/5700111.html