LightOJ 1021 - Painful Bases(状压DP)

大致题意:求 ’0‘ ~ ‘F' 的排序,组成16进制数,能被K整除,求排列的方案数

思路: 基础状压,注意从低位到高位DP,因为全是F在最高位时对于的十六进制会爆long long


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i< int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
#define PB push_back
#define MP make_pair
typedef pair<int,int> pii;

template <class T>
inline bool RD(T &ret) {
        char c; int sgn;
        if (c = getchar(), c == EOF) return 0;
        while (c != '-' && (c<'0' || c>'9')) c = getchar();
        sgn = (c == '-') ? -1 : 1 , ret = (c == '-') ? 0 : (c - '0');
        while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
        ret *= sgn;
        return 1;
}
template <class T>
inline void PT(T x) {
        if (x < 0) putchar('-') ,x = -x;
        if (x > 9) PT(x / 10);
        putchar(x % 10 + '0');
}

const int N = 17;
ll dp[1<<N][21];
ll vs[333];
ll p[N];
char s[N];
inline int cal(int x){
	int ans = 0;
	while( x ) ans += x & 1 , x >>= 1;
	return ans ;
}
int main() {
	p[0] = 1;
	for(char ch = '0' ; ch <= '9' ; ch ++ ) vs[ch] = ch - '0';
	for(char ch = 'A' ; ch <= 'F' ; ch ++ ) vs[ch] = 10 + ch - 'A' ;
	int T;
	cin >> T;
	REP(cas, T) {
		ll base, K ;
		RD( base) ,RD(K); 
		scanf("%s", s);
		int n = strlen(s);
		for(int i = 1; i < n; i ++) p[i] = p[i-1] * base ;
		rep(i, 1 << n ) rep( j , K ) dp[i][j] = 0;
		dp[0][0] = 1;
		for(int mask = 0; mask < ( 1 << n ); mask ++) {
			for(int i = 0; i < n ; i ++) {
				if( (mask & (1 << i)) == 0) {
					rep(mod, K ) {
						int nmod = ( vs[s[i]] + mod * base ) % K;
						dp[mask|(1 << i)][nmod] += dp[mask][mod] ;
					}
				}	
			}
		}
		printf("Case %d: %lld\n", cas, dp[(1<<n)-1][0]);	
	}
}


你可能感兴趣的:(LightOJ 1021 - Painful Bases(状压DP))