编程介的小学生 2017-12-06 11:05 采纳率: 20.5%
浏览 801
已采纳

Gem Combining

Problem Description
In the game "Gem Craft", we create gems in the item bar.
There are N grids in the item bar, each grid can only accomodate one gem.
Each gem has its level.
When the level of a gem upgrade by 1, the power of the gem will be doubled.
The key to win the game is to get a high level gem.
Gems with level 0 are available directly.
To get gems with higher level, the only way is to combine low level gems.
Suppose we have 2*N gems with level K now, they can be turned into N gems with level (K+1) afert a combining operation.
Your task is to get at least one gem with level M in minimum steps of operation.
For one step of operation, you can do one of the following two things:
(1) Get gems with level 0 directly, filling up the empty grids.
(2) Combine even number of gems with same level.

Input
The input contain some test cases.
Each test case contains two integers N and M (1<=N<=36, 0<=M<=20).
The input ends when N=M=0, this test case doesn't need processing.

Output
For each test case, output one line.
The line contains only one integer, the minimum steps of operation.
If there is no solution to get a gem with level M, output -1 instead.

Sample Input
4 3
8 3
0 0

Sample Output
9
4

Hint
We use dots to present empty grid, use number to present the level of a gem.

The first sample can be solved like this:

**** Initial
0000 Get level 0 gems
**11 Combine level 0 gems
***2 Combine level 1 gems
0002 Get level 0 gems
012 Combine level 0 gems
0012 Get level 0 gems
*112 Combine level 0 gems
**22 Combine level 1 gems
*
*3 Combine level 2 gems

In total of 9 steps of operation.

The second sample can be solved like this:

******** Initial
00000000 Get level 0 gems
****1111 Combine level 0 gems
******22 Combine level 1 gems
*******3 Combine level 2 gems

It only takes 4 steps of operation.

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-01-28 14:13
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?