Description
有一种机器,只有两个寄存器X和Y。初始两个寄存器都是1。有两种操作(括号中的是操作名称):
[X]X:=X+Y
[Y]Y:=X+Y
可以看出,每种操作都是将两个寄存器相加存入其中一个。一个程序就是由X和Y组成的字符串,表示依次做这两种操作。现在给一个r表示你需要寻找一个最短的程序使得在程序结束时,X寄存器中存着r,Y寄存器中可以储存任意数。如果有多解,输出字典序最小的答案。
Input
一个整数r。
Output
一行字符串,表示最短的程序。
Sample Input
【样例输入1】
10
【样例输入2】
20
Sample Output
【样例输出1】
XXYYX
【样例输出2】
XYYYYXX
Hint
【数据范围】
对于20%的数据,r<=100。
对于50%的数据,r<=10000。
对于100%的数据,2<=r<=1000000。
2333!!
互除法????不会啊。
迭代加深搜索。
斐波那契减枝。嘻嘻:)
还是能A
速度还挺快的。
应该是数据水了吧。
/*苍天负我笑,痴情待明朝*/
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#define mp makepair
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int>pii;
int r,d;
int A[100005];
bool dfs(int dep,int x,int y){
if(x==r)return 1;
if(dep==d)return 0;
if(x>r)return 0;
if(y>r)return 0;
int tx=x,ty=y;
if(tx>ty)swap(tx,ty);
for(int i=dep;i<d;i++){
tx=ty+tx;
swap(tx,ty);
}
if(ty<r)return 0;
A[dep]=1;
if(dfs(dep+1,x+y,y))return 1;
A[dep]=0;
if(dfs(dep+1,x,x+y))return 1;
return 0;
}
int main(){
scanf("%d",&r);
for(d=1;;d++){
if(dfs(0,1,1)){
for(int i=0;i<d;i++)if(A[i])cout<<"X";else cout<<"Y";
break;
}
}
return 0;
}