hiho 1461 暴力+随机

http://hihocoder.com/problemset/problem/1461

 

题意:中文题目简单说,有a和b分别是0和1,可操作a+=b或者b+=a,构造60次只能a或b变成n

 

思路:完全没见过的思路,所以只能瞎bb一下,写blog也是为了做个笔记算是。。如有不对还请指出。。我们手写画个树大概可以发现同一个数可以和很多与他互质的数出现在这个树里(有待考证),算是一个比较密集的。。随意采用随机的方法去ran出一个最终状态,结合gcd和60限制的判断,直接暴力搞。。。

 

感想:第一次见这种题目。。。完全无从下手。。。

 

代码:

 

#include<bits/stdc++.h>
#define bug(x) cout<<"bug "<<x<<endl;
#define mpi(x,y) make_pair(x,y)
using namespace std;

int s[100];
int gcd(int x,int y){
    return x%y==0?y:gcd(y,x%y);
}

int sl(int a,int b){
    if(gcd(a,b)!=1) return 0;
    int cnt=0;
    while(!(max(a,b)==1&&min(a,b)==0)){
        if(a>b){
            a-=b;
            s[cnt]=1;
        }
        else{
            b-=a;
            s[cnt]=0;
        }
        if(++cnt>60) return 0;
        if(min(a,b)==0&&a+b!=1) return 0;
    }
    for(int i=cnt-1;i>=0;i--) printf("%d",s[i]);
    puts("");
    return 1;
}

int main() {
    int t,n,a,b;
    scanf("%d",&t);
    while(t--){
        memset(s,0,sizeof(s));
        scanf("%d",&n);
        while(!sl(rand()%n+1,n));
    }
    return 0;
}



 

posted @ 2017-01-01 22:09  zhangxianlong  阅读(86)  评论(0编辑  收藏  举报