题目: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 ;
}
网友评论