As you know this sometimes base conversion is a painful task. But still there is interesting facts in bases.
For convenience Let's assume that we're dealing with the bases from 2 to 16. The valid symbols is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. And you can assume it all the numbers given in this problem is valid. For example 67AB was not a valid number of base one, since the allowed digits for base was 0 to a.
Now on this problem is 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 be divisible by K. K was given in decimal.
For the problem, you can assume that numbers with leading zeroes is 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 would be the integers, base (2≤base≤16) and K (1≤k≤20). The next line contains a valid integer in that base which contains distinct digits, which means in that number no digit OCC URS 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:20,922,789,888,000
Problem Setter:jane Alam Jan
Dp[sta][mod] Indicates the current selection state is STA, modulo K is mod when the number of scenarios
/************************************************************************* > File Name:LightOJ1021.cpp > Author:alex > Mail: [email protected] > Created time:2015 June 09 Tuesday 19:45 53 Seconds *************************** *********************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL; LL dp[(1<< -) +Ten][ -];Charstr[ -];intTransCharc) {if(c >=' 0 '&& C <=' 9 ') {returnC' 0 '; }returnC' A '+Ten;}intMain () {intT, Icase =1;scanf("%d", &t); while(t--) {intBase, K;scanf("%d%d", &base, &k);scanf('%s ', str);intLen =strlen(str); for(inti =0; I < (1<< Len); ++i) { for(intj =0; J < K; ++J) {Dp[i][j] =0; }} dp[0][0] =1; for(inti =0; I < (1<< Len); ++i) { for(intj =0; J < Len; ++J) {if(I & (1<< j)) {Continue; } for(intRest =0; Rest < K; ++rest) {if(!dp[i][rest]) {Continue; } dp[i | (1<< j) [(rest * base + trans (str[j]))% K] + = dp[i][rest]; } } }printf("Case%d:%lld\n", icase++, dp[(1<< Len)-1][0]); }return 0;}
LightOJ1021---Painful Bases (pressure DP)