题目:

  Recently, Peter saw the equation x0+2x1+4x2+...+2mxm=nx0+2x1+4x2+...+2mxm=n. He wants to find a solution (x0,x1,x2,...,xm)(x0,x1,x2,...,xm) in such a manner that ∑i=0mxi∑i=0mxi is minimum and every xixi (0≤i≤m0≤i≤m) is non-negative.

Input 

  There are multiple test cases. The first line of input contains an integer TT (1≤T≤105)(1≤T≤105), indicating the number of test cases. For each test case:


  The first contains two integers nn and mm (0≤n,m≤109)(0≤n,m≤109).

Output

  For each test case, output the minimum value of ∑i=0mxi∑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

题意:

  给你一个方程:x0+2x1+4x2+...+2mxm=n,找一组非负的解,使得x0+x1+...+xm最小。能不能有小数呢?题目没说。。。但是根据样例可以发现,是不会有小数的。

分析:

  我们想一想如果想要x的和最小,那肯定要让系数大的x最大,所以解就是:n/2m。但是,我也不知道为啥它就不能有小数,这可能是题意不明,我们就把xi属于N当成条件就好了。既然这样,n/2m不一定是小数,那么直接计算就不行了,但是总体的思路没有变,要x前面系数大的x大。然后,我们就考虑把n拆成二进制,于是每一位对应一个x,我们把m+1位及以上的数都加到xm身上,剩下的哪一位有数加在哪就好了,问题直接解决。

代码

  

#include <cstdio>
int main(){
int t;
scanf("%d",&t);
for(int jsjs=;jsjs<=t;jsjs++){
int n,m;
scanf("%d%d",&n,&m);
int ans=;
for(int i=;i<=m&&n;i++){//m可能很大,防止t掉
ans+=(n&);
n>>=;
}
ans+=n;
printf("%d\n",ans);
}
return ;
}

最新文章

  1. topcoder SRM 628 DIV2 BishopMove
  2. 使用iScroll时,input等不能输入内容的解决方法(share)
  3. JQuery Pagenation 知识点整理——arguments,callee,caller,apply应用(20150517)(转)
  4. C++ 命名规范小结
  5. 简单总结下 cookie、session
  6. maven到Gradle,一些对比和分享
  7. 用Git将本地项目推送到github
  8. LeetCode算法题-Valid Anagram(Java实现)
  9. Win10系列:C#应用控件基础21
  10. ILBC 运行时 (ILBC Runtime) 架构
  11. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
  12. KVM使用入门
  13. POJ - 2187 Beauty Contest(最远点对)
  14. SpringMVC(二)高级
  15. string 和 wstring
  16. POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏
  17. LoadRunner 测试Socket接口函数说明
  18. 求出每个team粉丝数最多的3个国家
  19. Linux磁盘管理命令(fdisk,mount,umount,mkfs)
  20. codeforce452DIV2——E. Segments Removal

热门文章

  1. Jmeter之Json提取器详解(史上最全)
  2. Go 语言入门教程:变量
  3. Android开发之修改Manifest中meta-data的数据
  4. ASP.NET Core 3.1 WebApi+JWT+Swagger+EntityFrameworkCore构建REST API
  5. springmvc使用&lt;mvc:default-servlet-handler/&gt;导致的handler失效
  6. eclipse中testNG的两种安装方式
  7. 别再写一摞if-else了!再写开除!两种设计模式带你消灭它!
  8. Tournament Chart【模拟+vector+map+string】
  9. String Problem(模板)【最短路】
  10. Oracle VM VirtualBox 连接 Centos7 minimal版