lightoj 1021 - Painful Bases - dblank

lightoj 1021 - Painful Bases

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 byK. K is given in decimal.

For this problem, you can assume that numbers with leading zeroes are allowed. So, 096 is a valid integer.
<h1>Input</h1>
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.
<h1>Output</h1>
For each case, print the case number and the desired result.

<h1>Sample Input</h1> <h1>Output for Sample Input</h1>
3   2 2 10   10 2 5681   16 1 ABCDEF0123456789 Case 1: 1 Case 2: 12 Case 3: 20922789888000

题意:给你一个base和一个k,还有一个base进制的数,问你这个数的全排列有多少对k取摸为0,base下的每一位数都只会出现一次。

思路:因为0~base-1的每一位都最多只会出现一次,还最多16位,我们可以状态压缩,用一个数的二进制下第i位的0和1代表第i个有没有选取。于是就可以以dpi表示选了前i个数并且当前的余数为j的方案数。然后就可以用大数取余来状态转移。

#include<bits/stdc++.h>
const int inf = 0x3f3f3f3f;
typedef long long ll;
ll dp(1<<16) + 10;
int a[20];
using namespace std;
int main()
{
    int t, base, k;
    char s[20];
    cin>>t;
    for(int cas = 1; cas<=t; cas++)
    {
        memset(dp, 0, sizeof(dp));
        dp0 = 1;
        scanf("%d%d%s", &base, &k, s);
        int len = strlen(s);
        for(int i = 0; s[i]; i++)
        {
            if(s[i]>='0' && s[i] <= '9')
                a[i] = s[i] - '0';
            else a[i] = s[i] - 'A' + 10;
        }

        int sz =  1<<len;
        for(int i = 0; i<sz; i++)
        {
            for(int r = 0; r<len; r++)
            {
                if(((1<<r) & i) == 0)
                {
                    for(int j = 0; j<k; j++)
                    {
                         dpi|(1<<r))%k] += dpi;
                    }
                }
            }
        }
        printf("Case %d: %lldn",cas,  dpsz-1);
    }
    return 0;
}

 

相关文章

发表新评论