BZOJ-1072: [SCOI2007]排列perm(状压DP)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1072

状压DP搞一搞,记得弄掉重复数字导致的重复方案数。。。

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std ;
 
#define maxs ( 1 << 11 )
#define maxd 1010
#define maxn 11
 
int f[ maxs ][ maxd ] , n , tot , d , cnt[ 10 ] ;
char s[ maxn ] ;
 
int main(  ) {
    scanf( "%d" , &tot ) ;
    while ( tot -- ) {
        scanf( "%s%d" , s , &d ) ;
        for ( int i = 0 ; i < ( 1 << strlen( s ) ) ; ++ i ) {
            for ( int j = 0 ; j < d ; ++ j ) {
                f[ i ][ j ] = 0 ;
            }
        }
        memset( cnt , 0 , sizeof( cnt ) ) ;
        for ( int i = 0 ; i < strlen( s ) ; ++ i ) {
            cnt[ s[ i ] - '0' ] ++ ;
        }
        f[ 0 ][ 0 ] = 1 ;
        for ( int i = 0 ; i < ( 1 << strlen( s ) ) ; ++ i ) {
            for ( int j = 0 ; j < d ; ++ j ) if ( f[ i ][ j ] ) {
                for ( int k = 0 ; k < strlen( s ) ; ++ k ) if ( ! ( ( 1 << k ) & i ) ) {
                    int st = i | ( 1 << k ) , sd = ( j * 10 + s[ k ] - '0' ) % d ;
                    f[ st ][ sd ] += f[ i ][ j ] ;
                }
            }
        }
        int ans = f[ ( 1 << strlen( s ) ) - 1 ][ 0 ] ;
        for ( int i = 0 ; i < 10 ; ++ i ) {
            for ( int j = 1 ; j <= cnt[ i ] ; ++ j ) {
                ans /= j ;
            }
        }
        printf( "%d\n" , ans ) ;
    }
    return 0 ;
}
    原文作者:AmadeusChan
    原文地址: https://www.jianshu.com/p/45a53ae6f4aa
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞