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< ...
随机推荐
- JS的ES6的Symbol
一.Symbol 1.什么是Symbol: Symbol是ES6新添加的原始类型(ES5已有原始数据类型:String,Number,boolean,function,undefined,object ...
- linux下 调试C#代码
参考两位大神的帖子: https://blog.csdn.net/soband_xiang/article/details/82914195 https://blog.csdn.net/weixin_ ...
- xcode7: Undefined symbols for architecture i386: "_iconv_open", referenced from:
在整合cocos和quick时,出现这个错误,我按照以前的方法 link binary with libraries 中add libiconv.2.dylib ,发现已经没有了这个库. 网上找了一下 ...
- 【转】vim复制与粘贴
用vim写代码时,经常遇到这样的场景,复制多行,然后粘贴. 这样做:1. 将光标移动到要复制的文本开始的地方,按v进入可视模式.2. 将光标移动到要复制的文本的结束的地方,按y复制.此时vim会自动将 ...
- 混合高斯分布与 EM 算法
极大似然估计在混合高斯分布中遇到的困难 在一般的情况下,对于所得到的样本集,\(X=\left\{x_{1}, \dots, x_{N}\right\}\),我们的目标是最大化似然函数,通过最大化似然 ...
- Server Tomcat v8.5 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
Server Tomcat v9.0 Server at localhost was unable to start within 45 seconds. If the server requires ...
- 微信小程序支付接口之Django后台
本文链接:https://blog.csdn.net/qq_41860162/article/details/89098694Python3-django-微信小程序支付接口调用工具类生成一系列微信官 ...
- response.redirect 正在中止线程
问题描述:正在中止线程问题原因:Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件.不执行 Response.End ...
- Xamarin.FormsShell基础教程(4)Shell项目内容列表页面运行效果
Xamarin.FormsShell基础教程(4)Shell项目内容列表页面运行效果 在创建好Shell解决方案后,就可以运行程序了.本小节将讲解运行后的效果. 内容列表页面 运行程序,初始效果如图1 ...
- BGP MPLS IP V匹N基本概念
BGP/MPLS IP VPN基本概念 Site 在介绍VPN时经常会提到"Site",Site(站点)的含义可以从下述几个方面理解: · Site是指相互之间具备IP连通性的一组 ...