lightoj 1021 Painful Bases (状压dp)


Time Limit: 2 second(s)Memory Limit: 32 MB

As you know that sometimes base conversion is a painful task. But still there are interesting facts in bases.

For convenience let's assume that we are dealing with the bases from 2 to 16. The valid symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. And you can assume that all the numbers given in this problem are valid. For example 67AB is not a valid number of base 11, since the allowed digits for base 11 are 0 to A.

Now in this problem you are given a base, an integer K and a valid number in the base which contains distinct digits. You have to find the number of permutations of the given number which are divisible by K. K is given in decimal.

For this problem, you can assume that numbers with leading zeroes are allowed. So, 096 is a valid integer.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. After that there will be two integers, base (2 ≤ base ≤ 16) and K (1 ≤ K ≤ 20). The next line contains a valid integer in that base which contains distinct digits, that means in that number no digit occurs more than once.

思路 : 定义 dp[S][k]  为 当前状态为 S, mod K 余数是k 的种类

状态S是每一位选与没有选的集合。在此集合下,它的排列(permutation mod K )会出现k种情况,余数分别为0 ~ K

故 dp[S | (1 << j)][(base * k + s[j]) % K] =  dp[S][k];

表示原本的状态为S, 余数为k,那么我新加上一个位j,新余数就会变成 (k *base + s[j]) % K

/***********************************************
 * Author: fisty
 * Created Time: 2015-07-11 22:38:30
 * File Name   : 1021.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 20
int t;
string _s;
int s[MAX_N];
int base, K;
LL dp[(1<<16)+10][MAX_N+1];
int main(){
    //freopen("in.cpp", "r", stdin);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> t;
    int cnt = 1;
    while(t--){
        Memset(dp, 0);
        cin >> base >> K;
        cin >> _s;
        int len = _s.length();
        for(int i = 0;i < len; i++){
            if(_s[i] <= '9' && _s[i] >= '0')
                s[i] = _s[i]-'0';
            else
                s[i] = _s[i]-'A' + 10;
        }
        dp[0][0] = 1;
        for(int i = 0;i < (1<<len); i++){
            for(int k = 0;k < K; k++){
                if(!dp[i][k]) continue;
                for(int j = 0;j < len; j++){
                    if((1<<j) & i) continue;
                    dp[i | (1<<j)][(k*base + s[j]) % K] += dp[i][k];
                }
            }
        }
        cout << "Case " << cnt++ << ": ";
        cout << dp[(1<<len) - 1][0] << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值