Number (CQOI2012, BZOJ2665)

Number (CQOI2012)

Subject to the effect

You need to give a number of product number, which each number is a 7-bit hexadecimal number (of 0 ~ 9, af composition). In order to prevent inadvertent wrong number when manual handling is required at any two positions corresponding to at least three ID numbers are not the same. The first number is 0000000, the lowest number at the second number is not contrary to the provisions of the above premise ... every time a new number is assigned, and do not always choose the lowest number the previous number of conflicts (note number is 16 hexadecimal number, you can compare the size). Click the law, in front of several numbers are: 0000000, 0000111, 0000222, ..., 0000fff, 0001012, 0001103,0001230,0001321,0001456, ...

Enter k, your task is to find the k small numbers.

Thinking

Compare Fairy \ (DP \) title.

Any two numbers are at least three different position, indicating that as long as there exist two five digit number is completely equal, not illegal.

Then we mark direct violence five positions, denoted \ (the DP [W] [A] [B] [C] [D] [E] \) , \ (W \) represents the current digital presence of 5 to 7 which five bits. \ (a, b, c, d, e \) represents the value of the five values. Then there is a total state \ (\ tbinom}. 7 {5} {\) 21 th, the array so that a total open \ (21 5 * 16 ^ \) .

We violence \ (16 ^ 7 \) times each enumeration figures, if the preceding 21 kinds of state are \ (false \) , can be included, and these states are marked as \ (to true \) .

code

#include<bits/stdc++.h>
#define FOR(i,l,r) for(register int i=(l),i##R=(r);i<=i##R;i++)
#define DOR(i,r,l) for(register int i=(r),i##L=(l);i>=i##L;i--)
#define loop(i,n) for(register int i=0,i##R=(n);i<i##R;i++)
#define mms(a,x) memset(a,x,sizeof a)
#pragma GCC optimize(3)
using namespace std;
typedef long long ll;
template<typename A,typename B>inline void chkmax(A &x,const B y){if(x<y)x=y;}
template<typename A,typename B>inline void chkmin(A &x,const B y){if(x>y)x=y;}
const int N=(1<<28),M=2e5+5;
int K;
int dp[25][16][16][16][16][16];
void Print(int a[]){
    loop(i,7)
        printf("%c",a[i]>=10?a[i]-10+'a':a[i]+'0');
}
void solve(){
    loop(a,16)loop(b,16)loop(c,16)loop(d,16)loop(e,16)loop(f,16)loop(g,16){
        if(
        !dp[1][a][b][c][d][e]&&
        !dp[2][a][b][c][d][f]&&
        !dp[3][a][b][c][d][g]&&
        !dp[4][a][b][c][e][f]&&
        !dp[5][a][b][c][e][g]&&
        !dp[6][a][b][c][f][g]&&
        !dp[7][a][b][d][e][f]&&
        !dp[8][a][b][d][e][g]&&
        !dp[9][a][b][d][f][g]&&
        !dp[10][a][b][e][f][g]&&
        !dp[11][a][c][d][e][f]&&
        !dp[12][a][c][d][e][g]&&
        !dp[13][a][c][d][f][g]&&
        !dp[14][a][c][e][f][g]&&
        !dp[15][a][d][e][f][g]&&
        !dp[16][b][c][d][e][f]&&
        !dp[17][b][c][d][e][g]&&
        !dp[18][b][c][d][f][g]&&
        !dp[19][b][c][e][f][g]&&
        !dp[20][b][d][e][f][g]&&
        !dp[21][c][d][e][f][g]
        )
        {
            K--;
            if(!K){
                int A[7]={a,b,c,d,e,f,g};
                Print(A);
                return;
            }
            dp[1][a][b][c][d][e]=1;
            dp[2][a][b][c][d][f]=1;
            dp[3][a][b][c][d][g]=1;
            dp[4][a][b][c][e][f]=1;
            dp[5][a][b][c][e][g]=1;
            dp[6][a][b][c][f][g]=1;
            dp[7][a][b][d][e][f]=1;
            dp[8][a][b][d][e][g]=1;
            dp[9][a][b][d][f][g]=1;
            dp[10][a][b][e][f][g]=1;
            dp[11][a][c][d][e][f]=1;
            dp[12][a][c][d][e][g]=1;
            dp[13][a][c][d][f][g]=1;
            dp[14][a][c][e][f][g]=1;
            dp[15][a][d][e][f][g]=1;
            dp[16][b][c][d][e][f]=1;
            dp[17][b][c][d][e][g]=1;
            dp[18][b][c][d][f][g]=1;
            dp[19][b][c][e][f][g]=1;
            dp[20][b][d][e][f][g]=1;
            dp[21][c][d][e][f][g]=1;    
        }
    }
}
int main(){
    scanf("%d",&K);
    solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/Heinz/p/11443156.html