Room Test 7: exam (Binary + analog)

topic:

 

 

 

 

 

 analysis:

In fact, we can not control the size k, we need only a combination method of construction, such that the minimum number of elements (will be less than or equal k)

Only one can be seen as a from 1 to n 1 jump process, analog look, feel how the jump is the fastest:

1:00000001

2:00000010

(1+2)

3:00000011

(……)

4:00001100

(3+4)

5:00001111

(……)

6:1111 1111

 In other words: the even met, it left it there with so many number 1 (the number 1 is doubled, doubled thought ), encounters an odd number, it will be the sum of two numbers become final several state is continuously 1.

It can be shown that this must be the fastest, for when n is not a power of 2, where special treatment at one stop.

And then push it a formula to obtain an initial value of k.

#include<bits/stdc++.h>
using namespace std;
#define ri register int
#define N 2005
int x[N][2*N],n,k,fl;
int read()
{
    int x=0,fl=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') fl=-1; ch=getchar(); }
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=getchar();
    return x*fl;
}
void print(int a,int cnt)
{
    for(ri i=1;i<=cnt+1;++i){
        for(ri j=i;j<=n+i-1;++j)
        printf("%d",x[a][j]);
        printf("\n");
        if(x[a][i]) { fl=1; return ; }
    }
}
int main()
{
    freopen("exam.in","r",stdin);
    freopen("exam.out","w",stdout);
    n=read(); k=read();
    int ans=0;
    for(ri i=1;i<=n;++i) 
    if((1<<i)>n) { ans=i-1; break; }
    if(n==(1<<ans)) printf("%d\n",(1<<ans)+ans);
    else printf("%d\n",ans+1+n);
    x[1][n]=1;
    int now=1,i=2;
    while(!fl){
        if(i&1){
            for(ri j=1;j<=n;++j)
            x[i][j]=x[i-1][j]+x[i-2][j];
            now*=2;
        }
        else{
            for(ri j=1;j<=n;++j) x[i][j]=x[i-1][j+now];
            print(i-1,now);
        }
        i++;
    }
    for(ri i=1;i<=n;++i) printf("1");
    return 0;
}
/*
3 5
*/
View Code

 

Guess you like

Origin www.cnblogs.com/mowanying/p/11632535.html