Educational Codeforces Round 78 (Rated for Div. 2)

1278B - 6

B. A and B 
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two integers aa and bb. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by 11; during the second operation you choose one of these numbers and increase it by 22, and so on. You choose the number of these operations yourself.

For example, if a=1a=1 and b=3b=3, you can perform the following sequence of three operations:

  1. add 11 to aa, then a=2a=2 and b=3b=3;
  2. add 22 to bb, then a=2a=2 and b=5b=5;
  3. add 33 to aa, then a=5a=5 and b=5b=5.

Calculate the minimum number of operations required to make aa and bb equal.

Input

The first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The only line of each test case contains two integers aa and bb (1≤a,b≤1091≤a,b≤109).

Output

For each test case print one integer — the minimum numbers of operations required to make aa and bb equal.

input

3
1 3
11 11
30 20

  output

3
0
4

  

Note

First test case considered in the statement.

In the second test case integers aa and bb are already equal, so you don't need to perform any operations.

In the third test case you have to apply the first, the second, the third and the fourth operation to bb (bb turns into 20+1+2+3+4=3020+1+2+3+4=30).

题意:给你两个数a和b,你每次可以选择一个数并给它加上n,操作后n++,重复多次,最后要使a==b,要你求出最少次数n。

刚开始我以为是暴力模拟题,就用自以为对的方式模拟了,居然WA。我就放弃了,赛后居然是打表找规律?!

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(void)
{
int t;
ll a,b;
scanf("%d",&t);
//printf("%lld\n",ss);
while(t--)
{
scanf("%lld %lld",&a,&b);
//直接模拟
ll n=0ll;
ll sn=0;
while(a!=b)
{
n++;//操作
if(a>b)
swap(a,b);//a小b大
sn=b-a;
//差==n
if(n<=sn)
a+=n;
else
b+=n; }
//printf("a:%d b:%d\n",a,b);
printf("%lld\n",n);
}
return 0;
}

  我发现比如,输入a=1 b=15时,输出为12,即当a+36=47,b+32=47。正确答案应为7,即a+21=22,b+7=22。故错误。

至于为什么会错,我还不知道。

下面是打表找规律的,不是我找的,还不太懂。

且听我瞎分析一波,上面代码是可以的我觉得,只是求的不是最小值。还是那上面的例子说,设sum[n]为前n项和,sum[7]=28;28-14 (就是a-b)=14(设为c,是个偶数,我们就可以把14平均分给a,b);

即a=1+14=15,b=15; c=14,把c平分给a,b即a=15+7=22,b=15+7=22;

也就是说先把a,b都变为sum[n]-c,然后都加上同一个数变为X,即要保证(sum[n]-abs(a-b))是个偶数。

似乎有点道理。如有错误欢迎指出

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll sum[1000000+5];
int main(void)
{
int t;
ll a,b;
scanf("%d",&t);
for(int i=1;i<=1000000;i++)
sum[i]=sum[i-1]+i;
while(t--)
{
scanf("%lld %lld",&a,&b);
ll n=0ll;
ll sn=0;
if(a!=b)
for(int i=1;i<=1000000;i++){
sn=abs(a-b);
if(sum[i]>=sn&&(sum[i]-sn)%2==0)
{
n=i;
break;
}
}
printf("%lld\n",n);
}
return 0;
}

  

 

codeforces B. A and B 找规律的更多相关文章

  1. Codeforces 870C Maximum splitting (贪心+找规律)

    <题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...

  2. Codeforces Gym 100015B Ball Painting 找规律

    Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...

  3. Codeforces Gym 100637B B. Lunch 找规律

    B. Lunch Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/B Des ...

  4. Codeforces 603A - Alternative Thinking - [字符串找规律]

    题目链接:http://codeforces.com/problemset/problem/603/A 题意: 给定一个 $01$ 串,我们“交替子序列”为这个串的一个不连续子序列,它满足任意的两个相 ...

  5. Codeforces 474D Flowers (线性dp 找规律)

    D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...

  6. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  7. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  8. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

  9. 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

    题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...

随机推荐

  1. C++完全二叉树的权值

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> int ...

  2. LaTeX 小试牛刀

    跟大家分享一下正式第一次使用 LaTex 的经验,之前数学建模的时候一直想用,但没有找到合适的软件.前段时间,实验室老师让我帮忙套个 IEEE ACCESS 的模板. 尝试过 TexPad,的确 UI ...

  3. Kubernetes(k8s)网络插件(CNI)的基准测试对比

      Kubernetes是一个伟大的容器"乐队".但它不管理Pod-to-Pod通信的网络.这是容器网络接口(CNI)插件的使命,它是实现容器集群工具(Kubernetes,Mes ...

  4. 一.Linux

    1.常用命令 Linux 命令的语法格式命令[选项][参数] Ctrl + l #清屏 clear #清屏 Ctrl +c #结束命令 man su #查看su 帮助信息,q 退出 su --help ...

  5. 2019 美柚java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.美柚等公司offer,岗位是Java后端开发,因为发展原因最终选择去了美柚,入职一年时间了,也成为了面试官,之 ...

  6. python爬虫---js加密和混淆,scrapy框架的使用.

    python爬虫---js加密和混淆,scrapy框架的使用. 一丶js加密和js混淆 js加密 ​ 对js源码进行加密,从而保护js代码不被黑客窃取.(一般加密和解密的方法都在前端) http:// ...

  7. HTTP发展简史

    HTTP发展简史 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...

  8. 使用input的file进行上传进行预览

    在使用file上传文件的时候,想到了图片预览的功能,然后查询了一些资料,一种是需要后端配合,将数据变成base64或者buff等数据传给后端然后调取接口进行显示,但是这种需要后端的配合和网络请求,感觉 ...

  9. grid网格布局——色子布局

    一.基本概念 样式 含义 grid-area 定义名称 grid-auto-columns 定义列数 grid-auto-flow 定义单元格流动方向(想象水流的样子) grid-auto-rows ...

  10. 编写可维护的JavaScript-随笔(五)

    事件处理 当事件触发时,事件对象(event对象)会作为回调参数传入事件处理程序中,event对象包含所有和事件相关的信息 function handleClick(event){ var popup ...