Doraemon's Cake Machine 将一个圆柱体横切和竖切以及克隆成N份,共M次操作,最少克隆多少次

Doraemon's Cake Machine

--------------------------------------------------------------------------------

Time Limit: 2 Seconds      Memory Limit: 65536 KB

--------------------------------------------------------------------------------

One day, N kids got one cake (a cylinder in shape). They decided to divide the cake so that everyone can have one piece of that cake. In the interest of fairness, pieces of the cake should be equal in size.

However, the kids had no knives to cut the cake. Luckily, Doraemon borrowed them a magic cake machine that can cut and clone cake.

To use Doraemon's cake machine, exactly M instructions must be entered. Otherwise the cake machine will destroy the cake (Doraemon's machines are not stable enough and have strange limits, you know).

For each instruction, the machine can perform one of the following actions:

Cut the cake vertically. The cut should be perpendicular to the top surface, and is a diameter of the circle viewed from the top.
Cut the cake horizontally. The cut should be parallel to the top surface.
Clone a piece of cake. Due to a defect in design, the machine can not perform 'cut' action after any 'clone' action.
The two kinds of 'cut' actions are shown below:

 

The cake is always cutted as a whole. You can not cut just one or several pieces. For example, if you cut horizontally and then vertically, you always get 4 pieces.

A 'cut' action should make the cake into more pieces (i.e. you can not cut at a same position twice). Finally, there should be exactly N pieces of cake, which are in a same size. Thus each kid can get one and no pieces of cake are left.

Since the 'clone' action is extremely slow, use it as less as possible.

Input
This problem contains multi test cases.

The first line contains an integer T (0 ≤ T ≤ 2000), indicating the number of test cases.

Following T lines, each line contains one test case.

Each test case contains two integers N (1 ≤ N ≤ 6000000) and M (0 ≤ M ≤ 3000000), separated by one space, indicating there are N kids and M instructions should be used.

Output
For each case, output one integer R in one line, where R is the minimal number of 'clone' instructions. If exactly N pieces of cake can not be made from exactly M instructions, output -1 instead of R.

Sample Input
3
5 3
4 2
5 1

Sample Output
1
0
-1

 


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int inf=(1<<30);
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        int N,M;scanf("%d%d",&N,&M);
        int P=N-M;
        int _min=(1<<30);
        //横切k刀,竖切n刀
        //n=0;
        if(N==M+1) _min=0;
        //n>0
        //2n(k+1)+x=N,n+k+x=M;
        int h=2*P-1;
        double l=sqrt(0.0+h);
        int t=-1;
        for(int i=1;i+i-1<=l;i++)
        {
            if(h%(i+i-1)==0&&((h/(i+i-1))&1))
            {
                int res=i+(h/(i+i-1)-1)/2;
                if(res>M) continue;
                if(M-res<_min) _min=M-res;
            }
        }
        if(_min!=inf) printf("%d/n",_min);
        else printf("-1/n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值