點擊開啟題目
Aaronson Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 723 Accepted Submission(s): 400
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 ( 0≤i≤m) is non-negative.
Input There are multiple test cases. The first line of input contains an integer T (1≤T≤105), indicating the number of test cases. For each test case:
The first contains two integers n and m (0≤n,m≤109).
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
Source BestCoder Round #84
沒有總結出來公式=。=用位元運算先湊最大的數,然後一個個往下算出來的。
代碼如下:
#include <cstdio>int main(){ int n,m; int u; int k; //n的二進位位元 scanf ("%d",&u); while (u--) { scanf ("%d %d",&n,&m); int t = n; k = 0; int ans = 0; while (t) { t >>= 1; k++; } if (k - 1 <= m) { while (n) { n = n & (n-1); ans++; } } else { while (m--) { if (n & 1) ans++; n >>= 1; } for (int i = 0 ; n ; i++) { if (n & 1) ans += (1 << i); n >>= 1; } } printf ("%d\n",ans); } return 0;}