【贪心算法】HDU 5747 Aaronson

题目大意

vjudge链接

给你一个n,m,求解满足等式x0+2x1+4x2+...+2mxm=n的x0~xm的最小和(xi为非负整数)

数据范围

0≤n,m≤109

思路

n和m都在int范围内,所以当i≥30时,xi必为0

求最小和用贪心解决

代码

 

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int n,m;
 5 
 6 int main(){
 7     int T;
 8     scanf("%d",&T);
 9     while(T--){
10         scanf("%d%d",&n,&m);
11         int ans=0;
12         int sta=min(30, m);//确定开始循环的数,大于30的话没必要
13         for(int i=sta;i>=0;i--){//从后向前贪心
14             int temp;
15             temp=1<<i;
16             if(temp>n)continue;
17             ans+=n/temp;
18             n%=temp;
19         }
20         printf("%d\n",ans);
21     }
22     return 0;
23 }
HDU 5747

 

posted @ 2020-04-04 15:19  Midoria7  阅读(55)  评论(0编辑  收藏  举报