zoj 3407 Doraemon's Cake Machine

24 篇文章 0 订阅

点击打开题目链接


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

题意:有n个人,要求通过m次操作把一个蛋糕分成n块,操作一共有3种:横切(y次),纵切(x次),还有克隆(z次);克隆的话只能一次克隆一块。求克隆的最小值

刚看到这个题,感觉应该很难,需要一个高大上的算法,就没做,结果比完赛发现是一个水题,还是题目做的少啊!!!

由上可得方程组:

2*x*(y+1)+z=n

x+y+z=m

由上两式相减可得:

n-m=2*x*y+x-y>=x*y 所以x只要遍历从1到sqrt(n-m)就可以了。

并且y=(n-m-x)/(2*x-1);只要遍历x,就可以同时求出y和z,从中求出z的最小值就可以了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

int main()
{
    int n,m,t;
    long long x,y,z;
    int i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
       if(n<=m)
        {
            printf("-1\n");
            continue;
        }
       if(n-m==1)//这时只需要横切m下就可以,不需要克隆
       {
           printf("0\n");
           continue;
       }
       long long Min=1e10;
      for(i=1;i<=sqrt(n-m)+1;i++)
      {
          if((n-m-i)%(2*i-1)==0)
          {
              y=(n-m-i)/(2*i-1);
            z=m-i-y;
            //cout<<i<<"$$"<<y<<"%%"<<z<<"&&"<<endl;
          if(z<Min&&z>=0) { Min=z;}

          }

      }
      if(Min>m)
        printf("-1\n");
      else
        printf("%lld\n",Min);


    }


  return 0;
}




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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值