C - Memory and De-Evolution CodeForces - 712C(三角形)

2024-04-16 03:32

本文主要是介绍C - Memory and De-Evolution CodeForces - 712C(三角形),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Input
The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.

Output
Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.

Examples
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .

In the second sample test, Memory can do .

In the third sample test, Memory can do:

题意: 边长分别为b和a的等边三角形,每次变上一个三角形的一个边,保证变完还是三角形,问多少次可以变成边长为a的等边三角形。

思路: 一开始我是从b开始变化的。题目中给的例子是6 6 6 -> 6 6 3,8 8 8 -> 8 8 5,
22 22 22 -> 7 22 22。于是按照这个变化规则,我就猜测枚举第一次变化的数字,之后都是最优变化(假设从小到大是x y z,那么变化完就是 y - x + 1 x y),其中一旦出现了a + x > y的情况,那么就一定可以变出一个a来,此时特殊判断。然后模拟过程。这个过程实现上就很麻烦,而且并不能保证最优。

  • 但是正解还是很简单的,从后往前变很难保证最优,如果逆向思考,就是从a a a 变到 b b b。变化过程依然是按照三角形规则的最优变化,但是不同的是只要变成了大于等于b,也就得出了结果。
#include <cstdio>
#include <iostream>using namespace std;int main()
{int a,b;scanf("%d%d",&a,&b);int x = b,y = b,z = b;int ans = 0;while(true){if(x >= a && y >= a && z >= a){break;}ans ++;if(ans % 3 == 0){x = y + z - 1;}else if(ans % 3 == 1){y = x + z - 1;}else if(ans % 3 == 2){z = x + y - 1;}}printf("%d\n",ans);
}

这篇关于C - Memory and De-Evolution CodeForces - 712C(三角形)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907692

相关文章

Codeforces 158B

很久没有刷题了,刚刚有小学弟问了这道题,AC之后贴上来吧。水~~~ #include <cstdio>#include <cmath>int main() {int n;while(scanf("%d", &n) != EOF) {int a = 0, b = 0, c = 0, d = 0;int arr[100001];for (int i = 0; i < n; ++

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

java编程:命令行输入的三个整数判断是否构成三角形,不能就抛异常。

写一个方法void sanjiao(int a,int b,int c),判断三个参数是否能构成一个三角形,如果不能则抛出 异常IllegalArgumentException,显示异常信息“a,b,c不能构成三角形”, 如果可以构成则显示三角形三个边长,在主方法中得到命令行输入的三个整数,调用此方法,并捕获异常。 附源代码: package 异常;public class Sa

32 - 判断三角形(高频 SQL 50 题基础版)

32 - 判断三角形 select *,if(x+y>z and x+z>y and z+y > x,'Yes','No') triangle fromTriangle;

OpenCV:已知三角形的两边 求夹角的问题(余弦定理)

// 找余弦角度: 线段pt0-pt1 和线段 pt0-pt2: double angle( Point pt1, Point pt2, Point pt0 ) {  double dx1 = pt1.x - pt0.x; double dy1 = pt1.y - pt0.y; double dx2 = pt2.x - pt0.x; double dy2 = pt2.y - pt0.

内存分析工具MAT(Memory Analyzer Tool)从安装到使用,配合jconsole jvisualvm分析

一.安装 首先,你得有一个Eclipse(因为MAT是Eclipse的插件) 然后,你要在Eclipse上安装MAT,步骤如下: 1.点击Help,Install New Soft,就出现了以下Install界面:然后我们点击ADD,在弹出的框中填上Mat插件的地址:http://download.eclipse.org/mat/1.6/update-site/,确定后, 点击Select

Documentation/memory-devices/ti-emif.txt

如果想评论或更新本文的内容,请直接联系原文档的维护者。 如果你使用英文交流有困难的话,也可以向中文版维护者求助。 如果本翻译更新不及时或者翻译存在问题,请联系中文版维护者。 中文版维护者: 姚家珺AriosYao    ks666dejia@163.com 中文版翻译者: 姚家珺AriosYao    ks666dejia@163.com 中文版校译者: 姚家珺AriosYao    k

leetcode 动态规划(基础版)三角形最小路径和

题目: 题解:  一种可行的方案是从下到上,避免了从上到下的下标特判。走到每一个位置的最小值等于该位置的上两个位置中的最小值加上该位置的值。 int minimumTotal(vector<vector<int>>& triangle) {int dp[205][205]={0};for(int i=triangle.size()-1;i>=0;i--){for(int j=0;j<=i

torch/lib/libgomp-d22c30c5.so.1: cannot allocate memory in static TLS block的正解

torch/lib/libgomp-4dbbc2f2.so.1.0.0: cannot allocate memory in static TLS block的正解 只需要一行命令即可解决 export LD_PRELOAD=/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/torch/lib/../../to