快速切题sgu126. 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 思路 1 只记录两个箱子的差值X,那么X=0的时候就能1次成功了
2 X的转移是X=SUM-2*X或者X=2*X-SUM(也即绝对值,X>0),注意到每次分母扩2倍,也就是说不超过32次
之后随便写一写就好啦,这个纯属胡整,我要睡觉!
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
int a,b,sum;
queue<int >que;
set <int >st;
int main(){
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
if(a==0||b==0)puts("0");
else if(a==b)puts("1");
else if((b-a)*2==a+b)puts("2");
else if((b+a)&1)puts("-1");
else {
int sub=b-a;
sum=a+b;
st.insert(sub);
que.push(sub);
int step=0;
while(!que.empty()){
sub=que.front();que.pop();step++;
if(sub==0||step>64)break;
int t=abs(sum-2*sub);
if(st.count(t)==0){que.push(t);st.insert(t);}
}
if(sub==0)printf("%d\n",step);
else puts("-1");
}
return 0;
}
快速切题sgu126. Boxes的更多相关文章
- 快速切题sgu127. Telephone directory
127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...
- 快速切题 sgu123. The sum
123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...
- 快速切题 sgu120. Archipelago 计算几何
120. Archipelago time limit per test: 0.25 sec. memory limit per test: 4096 KB Archipelago Ber-Islan ...
- 快速切题 sgu119. Magic Pairs
119. Magic Pairs time limit per test: 0.5 sec. memory limit per test: 4096 KB “Prove that for any in ...
- 快速切题 sgu118. Digital Root 秦九韶公式
118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...
- 快速切题 sgu117. Counting 分解质因数
117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...
- 快速切题 sgu116. Index of super-prime bfs+树思想
116. Index of super-prime time limit per test: 0.25 sec. memory limit per test: 4096 KB Let P1, P2, ...
- 快速切题 sgu115. Calendar 模拟 难度:0
115. Calendar time limit per test: 0.25 sec. memory limit per test: 4096 KB First year of new millen ...
- 快速切题 sgu113 Nearly prime numbers 难度:0
113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...
随机推荐
- 強化 Python 在 Vim 裡的顏色
我習慣用 putty 連 Unix server 開 screen,再用 vim 寫 Python.這篇記錄如何改善 Python 的顏色. 啟動 256 色 terminal 首先將可用的色彩數增加 ...
- word2vec 中的数学原理详解(一)目录和前言【转】
本文转载自:https://blog.csdn.net/itplus/article/details/37969519 word2vec 是 Google 于 2013 年开源推出的一个用于获取 wo ...
- hdu 6444 Neko's loop 单调队列优化DP
Neko's loop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- json字符窜转对象
第一种方法: 注意引用:using System.Runtime.Serialization.Json; using System.IO; static void Main(string[] args ...
- BZOJ5293: [Bjoi2018]求和 树上差分
Description master 对树上的求和非常感兴趣.他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k 次方和,而且每次的k 可能是不同的.此处节点深度的定义是这个节点 ...
- ajax post 参数说明
- Web前端代码规范
新增:http://materliu.github.io/code-guide/#project-naming HTML 原则1.规范 .保证您的代码规范,保证结构表现行为相互分离.2.简洁.保证代码 ...
- ros 编译包含脚本文件以及launch文件
目录结构如下: 修改CMakeLists.txt文件 install(PROGRAMS scripts/initial_pos.py DESTINATION ${CATKIN_PACKAGE_BIN_ ...
- RabbitMQ入门_06_深入了解ack
A. Delivery Tag 参考资料:https://www.rabbitmq.com/confirms.html 仔细查看一下 Consumer 的回调方法: public void handl ...
- 监督学习--k近邻算法
2017-07-20 15:18:25 k近邻(k-Nearest Neighbour, 简称kNN)学习是一种常用的监督学习方法,其工作机制非常简单,对某个给定的测试样本,基于某种距离度量找出训练集 ...