Xi'an Invitational tournament-L

Keywords: PHP

Title Link: https://nanti.jisuanke.com/t/39279

Given the sequence represented by n different numbers, two operations are defined: 1. Exchange the first half and the second half (if there are odd numbers, the middle doesn't matter). 2. Exchange each even bit with the number before it (if there are odd ones, the last one doesn't matter). Ask how many different sequences you can get from these two operations.

Train of thought: typical table playing to find regular questions, learn lessons from the next competition. The code for printing the table is as follows:

#include<cstdio>
using namespace std;

int a[10005],b[10005];
int n,ans;

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;
}

Then we can find the law:

n%4==0: ans=4

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

     else ans=2*n

n%4==2: ans=n

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

     else ans =12

AC Code:

#include<cstdio>
using namespace std;

int a[10005],b[10005];
int n,ans;

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;
}

Posted by Refused on Mon, 04 Nov 2019 10:07:44 -0800