SGU 126. Boxes --- 模拟
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 --- 模拟的更多相关文章
- SGU 126 Boxes(模拟题|二进制)
Translate:Sgu/126 126. 盒子 time limit per test: 0.5 sec. memory limit per test: 4096 KB 有两个盒子. 第一个盒子里 ...
- 找规律 SGU 126 Boxes
题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=126 /* 找规律,智商不够,看了题解 详细解释:http://blog.csdn. ...
- sgu 126 Boxes
题意:较大的容量减较小的容量,较小的容量翻倍.问操作几回其中一个空. 开始用set判重,重复就不可行.不过状态最多有2e18种.不仅爆内存,还超时.然后找规律.发现只有比例为1:1,1:3,1:7,3 ...
- Boxes - SGU 126(找规律)
题目大意:有两个箱子,一个箱子装了A个球,一个箱子装了B个球,可以从球多的那个箱子拿出来球少的箱子里面球的总数放在少的那个箱子里面,问能否把球全部放在一个箱子里面? 分析:很容易求出来最后放的拿一下一 ...
- 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 ...
- SGU 191.Exhibition(模拟)
时间限制:0.25s 空间限制:4M 题意: 有两个公司A.B,他们要展览物品,但是A公司的展柜要放B公司的物品,B公司的展柜要放A公司物品.最开始只有一个空柜台,从指定的一个公司开始,轮流进行操作, ...
- Codeforces 821C Okabe and Boxes(模拟)
题目大意:给你编号为1-n的箱子,放的顺序不定,有n条add指令将箱子放入栈中,有n条remove指令将箱子移除栈,移出去的顺序是从1-n的,至少需要对箱子重新排序几次. 解题思路:可以通过把栈清空表 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- 今日SGU 5.4
SGU 127 题意:给你n个数字,和m,k,问你有多少个数字的m次幂可以被k整除 收获:快速幂 #include<bits/stdc++.h> #define de(x) cout< ...
随机推荐
- SpringBoot:认认真真梳理一遍自动装配原理
前言 Spring翻译为中文是“春天”,的确,在某段时间内,它给Java开发人员带来过春天,但是随着我们项目规模的扩大,Spring需要配置的地方就越来越多,夸张点说,“配置两小时,Coding五分钟 ...
- bzoj 3992: [SDOI2015]序列统计 NTT+原根
今天开始学习丧心病狂的多项式qaq...... . code: #include <bits/stdc++.h> #define ll long long #define setIO ...
- Unicode-objects must be encoded before hashing 错误解决办法
提交注册用户数据后出来这个,错误原因是update()必须指定要加密的字符串的字符编码 #encryptions1 = sha1()s1.update(upwd.encode("utf8&q ...
- kafk设计要点
kafka的设计目标是高吞吐量,所以kafka自己设计了一套高性能但是不通用的协议,他是仿照AMQP( Advanced Message Queuing Protocol 高级消息队列协议)设计的 ...
- 同余方程组(EXCRT)(luogu4777)
#include<cstdio> #include<algorithm> #define ll long long using namespace std; ll k; ll ...
- 第03组 Alpha冲刺(3/4)
队名:不等式方程组 组长博客 作业博客 团队项目进度 组员一:张逸杰(组长) 过去两天完成的任务: 文字/口头描述: 制定了初步的项目计划,并开始学习一些推荐.搜索类算法 GitHub签入纪录: 暂无 ...
- 如何使用Processing获取图片中每一个像素的坐标
剛好碰到有同學問如何在Processing中進行像素級的圖片處理, =============================================================此時需要一 ...
- 解决vue视图不渲染
动态添加对象属性 //声明var travelMainVueObj;travelMainVueObj = new Vue({ "el" : "#portletConten ...
- Linux系统下zookeeper客户端命令使用
1. 启动客户端 [admin@yrjk bin]$ ./zkCli.sh [zk: localhost:2181(CONNECTED) 0] 2. 显示所有操作命令 [zk: localhost:2 ...
- Microsoft.Owin 使用 文件服务
添加引用: <package id="Microsoft.Owin" version="4.0.1" targetFramework="net4 ...