#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==1){ printf("%d\n",0); }else{ printf("%d\n",resolve(n)); } } return 0; } int deal(int n,int m){ int t = 0; if(n>m){ t=m; m=n; n=t; } if(n==1){ return m-1; } if((n==m && n!=1) || n<1){ return -1; } int changeTimes = m/n; int d = deal(n,m%n); if(d!=-1){ return changeTimes+d; } return -1; } int resolve(int n){ int min = 1000000; if(n<=0){ return 0; } for (int i = 1; i <= n/2; i++) { int temp = -1; temp = deal(i,n-i); if( (temp>=0) && (temp<min) ){ min = temp; } } return min+1; }
題目描述
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.
解答要求時間限制:1000ms, 內存限制:64MB
輸入
The input contains the only integer n (1 ≤ n ≤ 106).Process to the end of file.
輸出
Print the only integer k.