[CF 712C] Memory and De-Evolution

本文探讨了如何通过改变边长将一个等边三角形从边长x转换到边长y的问题。采用了一种贪心策略,即每次选择最短的一条边变为其它两边之和减一,直到形成目标三角形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

给出一个边长为x的等边三角形,每次你可以修改一条边的长度,保证修改完的图形仍然是三角形。问最少用多少步可以把它变成边长为y的等边三角形。
y<=x<=10^5

Solution

正难则反,我们考虑加法。
因为我们需要尽快逼近x,于是我们每次可以贪心的选择最短的那一条边变成余下两条边之和。
其实这道题应该可以有数学方法O(1)求解,但是蒟蒻并不会。

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int a[3],x,y,ans;
int main() {
    scanf("%d%d",&x,&y);
    a[0]=a[1]=a[2]=y;
    while (1) {
        a[0]=a[1]+a[2]-1;
        if (a[0]>x) {ans+=3;break;}
        sort(a,a+3);ans++;
    }
    printf("%d",ans);
}
为以下代码注释:<?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="ChangeListManager"> <list default="true" id="0fdc712c-b442-48b5-b5ec-72c0e9b163ca" name="Default Changelist" comment="" /> <option name="SHOW_DIALOG" value="false" /> <option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="LAST_RESOLUTION" value="IGNORE" /> </component> <component name="ProjectId" id="2QVuimGepuP2YY8QMsZ0qPrP6Yt" /> <component name="ProjectViewState"> <option name="hideEmptyMiddlePackages" value="true" /> <option name="showLibraryContents" value="true" /> </component> <component name="PropertiesComponent"> <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> <property name="WebServerToolWindowFactoryState" value="false" /> <property name="aspect.path.notification.shown" value="true" /> <property name="last_opened_file_path" value="$PROJECT_DIR$" /> <property name="settings.editor.selected.configurable" value="preferences.lookFeel" /> </component> <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> <component name="TaskManager"> <task active="true" id="Default" summary="Default task"> <changelist id="0fdc712c-b442-48b5-b5ec-72c0e9b163ca" name="Default Changelist" comment="" /> <created>1685451198775</created> <option name="number" value="Default" /> <option name="presentableId" value="Default" /> <updated>1685451198775</updated> <workItem from="1685451199972" duration="592000" /> <workItem from="1685453362061" duration="917000" /> </task> <servers /> </component> <component name="TypeScriptGeneratedFilesManager"> <option name="version" value="3" /> </component> </project>
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值