Pairs of Numbers
#include<stdio.h> //we have defined the necessary header files here for this problem.
//If additional header files are needed in your program, please import here. /* * 函数int deal(int n,int m):处理达到(n,m)转化为(1,1)时需要的次数
首先保证n<=m
当n==1时,deal(1,m)应该返回 m-1 (规律)
当n不为1且n==m时、或当n < 1时,无法转化到(1,1) 返回无效值-1
将deal(n,m)转化为deal(n,m%n),这里在n远远小于m时,远远比用减号要快,也不会Stackoverflow
如果deal(n,m%n)返回有效值,就把m/n这个转化次数加上返回
函数 resolve(n):对n进行从(1,n-1)(2,n-2)…(n/2,n-n/2)进行遍历调用
如果返回了合法值,就和min比较替换
返回时注意要加上首次拆开时的1次 */ int deal(int n,int m);
int resolve(int n); int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(n==){
printf("%d\n",);
}else{
printf("%d\n",resolve(n));
} }
return ;
} int deal(int n,int m){
int t = ;
if(n>m){
t=m;
m=n;
n=t;
} if(n==){
return m-;
} if((n==m && n!=) || n<){
return -;
} int changeTimes = m/n;
int d = deal(n,m%n);
if(d!=-){
return changeTimes+d;
} return -;
} int resolve(int n){
int min = ;
if(n<=){
return ;
} for (int i = ; i <= n/; i++)
{
int temp = -;
temp = deal(i,n-i);
if( (temp>=) && (temp<min) ){
min = temp;
}
}
return min+;
}
Let's assume that we have a pair of numbers (a,b). We can get a new pair (a+b,b) or (a,a+b) from the given pair in a single step.
Let the initial pair of numbers be (1,1). Your task is to find number k, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals n.
The input contains the only integer n (1 ≤ n ≤ 106).Process to the end of file.
Print the only integer k.
Pairs of Numbers的更多相关文章
- CF 403D Beautiful Pairs of Numbers
The sequence of integer pairs (a1, b1), (a2, b2), ..., (ak, bk) is beautiful, if the following state ...
- Find Unique pair in an array with pairs of numbers 在具有数字对的数组中查找唯一对
给定一个数组,其中每个元素出现两次,除了一对(两个元素).找到这个唯一对的元素. 输入:第一行输入包含一个表示测试用例数的整数T.然后T测试用例如下.每个测试用例由两行组成.每个测试用例的第一行包含整 ...
- 【题解】CF#403 D-Beautiful Pairs of Numbers
这题还挺对胃口的哈哈~是喜欢的画风!回家路上一边听歌一边想到的解法,写出来记录一下…… 首先,由于 \(b_{k} < a_{k + 1}\) ,所以我们可以看作是在一个长度为 n 的序列上选择 ...
- [CF403D]Beautiful Pairs of Numbers
题意:给定$n,k$,对于整数对序列$\left(a_1,b_1\right),\cdots,\left(a_k,b_k\right)$,如果$1\leq a_1\leq b_1\lt a_2\leq ...
- Codeforces 403D: Beautiful Pairs of Numbers(DP)
题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...
- POJ 1320 Street Numbers 【佩尔方程】
任意门:http://poj.org/problem?id=1320 Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- Codeforces 395 D.Pair of Numbers
D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- POJ 1320 Street Numbers 解佩尔方程
传送门 Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2529 Accepted: 140 ...
- Codeforces Round #209 (Div. 2) D. Pair of Numbers (模拟)
D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- Codeforces Round #588 (Div. 2) A. Dawid and Bags of Candies
链接: https://codeforces.com/contest/1230/problem/A 题意: Dawid has four bags of candies. The i-th of th ...
- 01_Deepin15 下搭建python开发环境
https://blog.csdn.net/iimpact/article/details/90239193 https://github.com/josonle/Coding-Now#Linux系统 ...
- PHP实现yii项目转发有哪几种方法?
header("location: index.php"); 如果header后面有代码, 会执行, 如果不想执行, 则加上exit;或者die();等, 应该可以应用到yii项目 ...
- 数据结构实验之链表七:单链表中重复元素的删除(SDUT 2122)
#include <bits/stdc++.h> using namespace std; typedef struct node { int data; struct node* nex ...
- C#字符串和值转换 以及万能转换
2.使用万能转换器进行不同类型转换 Convert.ToXxx(object value) int iRet = Convert.ToInt32("201"); float fR ...
- mysql 5.7.16 安装配置
环境变量在path中添加一个 E:\soft\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin 查看mysql版本mysql -V 生成无密码账户进入到mysql ...
- javascript实现集合Set、字典Dictionary、HashTable
集合是由一组无序且唯一(即不能重复)的项组成的.这个数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中. function Set() { this.items = {}; } S ...
- 关于Math.random()
关于 Math.random() ,以前经常搞混淆,这次写个笔记专门记录下: Math.random() : 返回的是 0~1 之间的一个随机小数0<=r<1,即[0,1); 注意:这里 ...
- Echarts4+EchartsGL 3D迁徙图(附源码)
最近遇到些Echarts迁徙图问题,在实现二维地图的迁徙图后开始开发3D迁徙图,在网上一查,发现3D版本迁徙图资料较少,自己研究并借鉴一些资料后写了一个小demo,希望能帮大家少走些弯路,共同学习. ...
- Django中三种方式写form表单
除了在html中自己手写form表单外,django还可以通过 继承django.forms.Form 或django.forms.ModelForm两个类来自动生成form表单,下面依次利用三种方式 ...