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.
Output
For each case, print the case number and the desired result.
Sample Input | Output for Sample Input |
3
2 2 10
10 2 5681
16 1 ABCDEF0123456789 | Case 1: 1 Case 2: 12 Case 3: 20922789888000 |
题目大意:base表示base进制,给一个k(0 <= k <= 20),给一个base进制下的合法数,保证每位数字都不同,求这个数字的所有排列中是k的倍数的个数
题目分析:因为数字都不同,所以最多只有16位,因此可以状压做,dp[i][j]表示选数状态为i,模k为j的排列数个数,然后对一个合法状态扩展就是在前面加数字,取模的时候因为((x * base) + num[idx]) % k == ((x * base) % k + num[idx] % k) % k == (j * base + num[idx] % k ) % k所以转移方程就是
dp[i | (1 << idx)][(j * base + num[idx] % k) % k] += dp[i][j]
本题的处理方法太过于巧妙,因为一个排列数可以是子序列的排列数方法叠加起来,状压可以把情况全都遍历一遍。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 17;
long long dp[1<<N][20], num[20];
char str[20];
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
int base, mod;
scanf("%d %d %s", &base, &mod,str);
int len=strlen(str);
for(int i=0;str[i];i++)
{
if(str[i]<='9')
num[i]=str[i]-'0';
else
num[i]=str[i]-'A'+10;
}
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=0;i<(1<<len);i++)
{
for(int j=0;j<mod;j++)
{
if(dp[i][j])
{
for(int k=0;k<len;k++)
{
if(i&(1<<k))
continue;
else
dp[i|(1<<k)][(j*base+num[k])%mod]+=dp[i][j];
}
}
}
}
printf("Case %d: %lld\n",ncase++,dp[(1<<len)-1][0]);
}
return 0;
}
参考:
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 1<<16;
const int INF = 0x3f3f3f3f;
long long dp[maxn][20];
//dp[i][j]表示数字集合为i(数位i上为1,代表第i个数取了),对K取余为j的数的各个数位上总的排列数。
int num[20];
int base,K;
char s[20];
int main()
{
// freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
for(int cas=1;cas<=T;cas++){
scanf("%d %d",&base,&K);
scanf("%s",s);
int len = strlen(s);
for(int i=0;i<len;i++){
if(s[i]>='0' && s[i] <= '9')
num[i] = s[i] - '0';
else num[i] = 10 + s[i] - 'A';
}
memset(dp,0,sizeof(dp));
dp[0][0] = 1; //表示各位都没有数字,余数为0的个数有一个,即0.
for(int i=0;i<(1<<len);i++) //枚举不同数字集合
for(int j=0;j<K;j++){ //枚举不同数字集合下的余数情况。
if(!dp[i][j]) continue;
for(int k=0;k<len;k++){ //i在第k个数位已经有1,直接跳过。
if(i & (1<<k)) continue;
dp[i|(1<<k)][(j*base+num[k])%K] += dp[i][j];
//j*base+num[k])%K 表示把第k个数加到dp[i][j]中排列数的末尾,新组成的数对K取余后得到余数。
//假设dp[i][j]中的任一个数为num, 则 余数就为(num*base + num[k])% K; 而 num%K == j。 所以利用取模运算就可以化简得到(j*base+num[k])%K
}
}
printf("Case %d: %lld\n",cas,dp[(1<<len)-1][0]);
}
}
//参考别人的代码写的,非原著。