【HDU】5747 - Aaronson(位元運算 & 貪心)

來源:互聯網
上載者:User

點擊開啟題目

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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.