<传送门>

126. Boxes

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

There are two boxes. There are A balls in the first box, and B balls in the second box (0 < A + B < 2147483648). It is possible to move balls from one box to another. From one box into another one should move as many balls as the other box already contains. You have to determine, whether it is possible to move all balls into one box.

Input

The first line contains two integers A and B, delimited by space.

Output

First line should contain the number N - the number of moves which are required to move all balls into one box, or -1 if it is impossible.

Sample Input

Sample Output

2 6

Sample Output

2

【题目大意】

简单地说就是:给你两个数a和b,现在你可以“将大数-小数,小数变为原来2倍”,问你能否在有限次的操作后使得其中哟个数等于0,另外一个数为原来两个数的和。

若可能,输出步数;不可能输出-1.

【题目分析】

一开始的时候,我用每次都使得a为大数,b为小数,但是这样会出现循环,因为每次都维护他们的前后大小,势必会造成循环。

如果我们只是一开始维护一次大小关系,以后就不管他了,这样当b增加到>=a的时候,还不满足一个为0的条件,那么就说明不可能实现这样的操作。

证明过程如下:

give two numbers: a and b;(a!=b)

a=max(a,b);    b=min(a,b);

重复:  a=a-b;  b=b+b;    当b大于等于a时,以后的都是重复开始的那两个数字,所以前面不能实现题目的操作的话,后面不可能实现。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int a,b; int sovle()
{
if(a == b) return 1;
else if(a == 0 || b == 0) return 0;
int cnt = 1;
int tmp;
if(a < b)
{
tmp = a;
a = 2*tmp;
b = b - tmp;
}
else if(a > b)
{
tmp = b;
b = 2*tmp;
a = a - tmp;
} if(a > b)
{
tmp = a;
a = b;
b = tmp;
}
if(b%a != 0) return -1;
else{
while(a < b)
{
tmp = a;
a = tmp + tmp;
b = b - tmp;
cnt ++;
if(a == b)
break;
}
if(a == b) return cnt + 1;
else return -1;
}
} int main()
{
while(scanf("%d%d",&a,&b) != EOF)
{
int ans = sovle();
printf("%d\n",ans);
}
return 0;
}

  

SGU 126. Boxes --- 模拟的更多相关文章

  1. SGU 126 Boxes(模拟题|二进制)

    Translate:Sgu/126 126. 盒子 time limit per test: 0.5 sec. memory limit per test: 4096 KB 有两个盒子. 第一个盒子里 ...

  2. 找规律 SGU 126 Boxes

    题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=126 /* 找规律,智商不够,看了题解 详细解释:http://blog.csdn. ...

  3. sgu 126 Boxes

    题意:较大的容量减较小的容量,较小的容量翻倍.问操作几回其中一个空. 开始用set判重,重复就不可行.不过状态最多有2e18种.不仅爆内存,还超时.然后找规律.发现只有比例为1:1,1:3,1:7,3 ...

  4. Boxes - SGU 126(找规律)

    题目大意:有两个箱子,一个箱子装了A个球,一个箱子装了B个球,可以从球多的那个箱子拿出来球少的箱子里面球的总数放在少的那个箱子里面,问能否把球全部放在一个箱子里面? 分析:很容易求出来最后放的拿一下一 ...

  5. Codeforces Round #158 (Div. 2) C. Balls and Boxes 模拟

    C. Balls and Boxes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. SGU 191.Exhibition(模拟)

    时间限制:0.25s 空间限制:4M 题意: 有两个公司A.B,他们要展览物品,但是A公司的展柜要放B公司的物品,B公司的展柜要放A公司物品.最开始只有一个空柜台,从指定的一个公司开始,轮流进行操作, ...

  7. Codeforces 821C Okabe and Boxes(模拟)

    题目大意:给你编号为1-n的箱子,放的顺序不定,有n条add指令将箱子放入栈中,有n条remove指令将箱子移除栈,移出去的顺序是从1-n的,至少需要对箱子重新排序几次. 解题思路:可以通过把栈清空表 ...

  8. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  9. 今日SGU 5.4

    SGU 127 题意:给你n个数字,和m,k,问你有多少个数字的m次幂可以被k整除 收获:快速幂 #include<bits/stdc++.h> #define de(x) cout< ...

随机推荐

  1. Frightful Formula Gym - 101480F (待定系数法)

    Problem F: Frightful Formula \[ Time Limit: 10 s \quad Memory Limit: 512 MiB \] 题意 题意就是存在一个\(n*n\)的矩 ...

  2. day39线程

    复习: C/S架构: 客户端     服务器模式 B/S架构: 浏览器     服务器模式 B/S架构的客户端对PC机的性能要求比较低.统一了应用的接口 B/S架构隶属于C/S架构 TCP UDP的区 ...

  3. SpringBoot:认认真真梳理一遍自动装配原理

    前言 Spring翻译为中文是“春天”,的确,在某段时间内,它给Java开发人员带来过春天,但是随着我们项目规模的扩大,Spring需要配置的地方就越来越多,夸张点说,“配置两小时,Coding五分钟 ...

  4. mknod

  5. .net core 从 ActionFilterAttribute 获取Request.Body 的正确方式

    由于 ModelBinding在动作过滤器之前运行,直接使用 context.ActionArguments["parameter"]  获取模型对象 This article s ...

  6. Spring Could Stream 基本用法

    Spring Cloud Stream是一个建立在Spring Boot和Spring Integration之上的框架,有助于创建事件驱动或消息驱动的微服务. 通过它可以更方便的访问消息服务,如消费 ...

  7. cf1191 解题报告

    cf1191 解题报告 A-简单模拟 脑内算出来让计算机输出 #include <bits/stdc++.h> #define ll long long using namespace s ...

  8. OpenFlow和SDN的历史和原理介绍

    OpenFlow相关的历史.新闻:http://blog.csdn.net/jincm13/article/details/7825754起源与发展[https://36kr.com/p/503598 ...

  9. Mongoose 入门以及实现数据的增、删、改、查

    mongoose 介绍 Mongoose 是在 node.js 异步环境下对 mongodb 进行便捷操作的对象模型工具.Mongoose 是 NodeJS 的驱动,不能作为其他语言的驱动. Mong ...

  10. ELK(Elasticsearch + Logstash + Kibana) 日志收集

    单体应用或微服务的场景下,每个服务部署在不同的服务器上,需要对日志进行集重收集,然后统一查看所以日志. ELK日志收集流程: 1.微服务器上部署Logstash,对日志文件进行数据采集,将采集到的数据 ...