Xi'an Invitational -L (playing table to find the law)

Topic link: https: //nanti.jisuanke.com/t/39279

The meaning of problems: Given n different sequence number, with the definition of two operations: a first half and a rear half-exchange (if there is an odd number, regardless of the intermediate). 2. Each exchange before it even bit and the number (if there is an odd number, the last matter). Q. With these two operations, the number of different sequences can be obtained.

Ideas: Typical playing table to find the law of the question, the next game to learn. Hit the table the following code:

#include<cstdio>
using namespace std;

int a [ 10005 ] b [ 10005 ];
int n years;

bool check(){
    bool flag=1;
    for(int i=1;i<=n;++i)
        if(a[i]!=b[i]){
            flag=0;
            break;
        }
    return flag;
}

int main(){
    for(n=1;n<=30;++n){
        int hf=n/2;
        ans=0;
        for(int i=1;i<=n;++i)
            a[i]=i,b[i]=i;
        while(1){
            for(int i=1;i<=hf;++i){
                int tmp=b[i];
                b[i]=b[n-hf+i];
                b[n-hf+i]=tmp;
            }
            if(check()) break;
            ++ans;
            for(int i=1;i<=n-1;i+=2){
                int tmp=b[i];
                b[i]=b[i+1];
                b[i+1]=tmp;
            }
            if(check()) break;
            ++ans;
        }
        printf("%d:%d\n",n,ans+1);
    }
    return 0;
}
View Code

Then you can find the law:

n% 4 == 0: 4 years =

n%4==1: if(n==1) ans=1

     presence ans = 2 * n

n% 4 == 2 years n =

n%4==3: if(n==3) ans=6

     presence ans = 12

AC Code:

#include<cstdio>
using namespace std;

int a [ 10005 ] b [ 10005 ];
int n years;

bool check(){
    bool flag=1;
    for(int i=1;i<=n;++i)
        if(a[i]!=b[i]){
            flag=0;
            break;
        }
    return flag;
}

int main(){
    for(n=1;n<=30;++n){
        int hf=n/2;
        ans=0;
        for(int i=1;i<=n;++i)
            a[i]=i,b[i]=i;
        while(1){
            for(int i=1;i<=hf;++i){
                int tmp=b[i];
                b[i]=b[n-hf+i];
                b[n-hf+i]=tmp;
            }
            if(check()) break;
            ++ans;
            for(int i=1;i<=n-1;i+=2){
                int tmp=b[i];
                b[i]=b[i+1];
                b[i+1]=tmp;
            }
            if(check()) break;
            ++ans;
        }
        printf("%d:%d\n",n,ans+1);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/10927161.html