BSOJ 3023 -- 寄存器

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章