| 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 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.
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 |
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define bug printf("hihi\n")
#define eps 1e-8
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define N 17
int a[N];
char c[N];
ll dp[1<<N][21];
int B,k;
int len;
inline int fdd(int i)
{
if(c[i]>='0'&&c[i]<='9') return c[i]-'0';
return c[i]-'A'+10;
}
void DP()
{
int i,j;
memset(dp,0,sizeof(dp));
for(int i=0;i<len;i++)
dp[1<<i][a[i]%k]=1;
int all=1<<len;
for(int cur=0;cur<all;cur++)
for(int i=0;i<len;i++)
{
if(cur&(1<<i)) continue;
for(int res=0;res<k;res++)
{
int to=cur|(1<<i);
dp[to][(res*B+a[i])%k]+=dp[cur][res];
}
}
}
int main()
{
int i,j,t,ca=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&B,&k);
scanf("%s",c);
len=strlen(c);
for(int i=0;i<len;i++)
a[i]=fdd(i);
DP();
printf("Case %d: %lld\n",++ca,dp[(1<<len)-1][0]);
}
return 0;
}
本文探讨了在2到16的基数范围内,如何通过给定一个基数、整数K和一个包含不同数字的合法数字字符串,计算出该数字在所有可能的排列中被K整除的数量。主要涉及数字转换、排列组合和模运算的应用。

860

被折叠的 条评论
为什么被折叠?



