题目大意:给你n进制的数中的几个(比如10进制中的1568这四个数),问有这几个数组成的排列中有多少个能被k整除
题目思路:dp[i][j]表示使用过的数的状态为i且余数为j这种状态的个数。i是状态压缩
这道题的思路其实不是很难,但是我卡了半天。。因为最后输出写的是%d而不是%lld,留作纪念= =
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
long long dp[(1<<16)+100][22];
bool have[20];
int main(){
int T,cas = 1;
cin>>T;
while(T --){
int base,k;
cin>>base>>k;
printf("Case %d: ",cas ++);
char s[100];
cin>>s;
int l = strlen(s);
memset(have,0,sizeof(have));
for(int i = 0;i < l;i ++){
if(s[i] >= 'A'&&s[i] <= 'Z')have[s[i]-'A'+10] = 1;
else if(s[i] >= '0'&&s[i] <= '9')have[s[i]-'0'] = 1;
}
memset(dp,0,sizeof(dp));
for(int i = 0;i < base;i ++){
if(have[i])dp[(1<<i)][i%k] = 1;
}
//for(int i = 0;i < base;i ++)cout<<have[i];
for(int i = 0;i < (1<<base);i ++){
for(int j = 0;j < k;j ++){
if(dp[i][j] == 0)continue;
for(int t = 0;t < base;t ++){
if((i&(1<<t)) == 0&&have[t]){
dp[i|(1<<t)][(j*base+t)%k] += dp[i][j];//cout<<(i|(1<<t))<<' '<<((j*base+t)%k)<<' ';
}
}//cout<<i<<j<<dp[i][j]<<endl;
}
}
int now = 0;
for(int i = 0;i < base;i ++)if(have[i])now = (now|(1<<i));
printf("%lld\n",dp[now][0]);
}
return 0;
}