标签:
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 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.
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 |
题意:给你的n为进制,后面的m为模数,然后给你一串数字为当前进制下的数,问你拆分这个数,然后再全排列组成新的数,问这些数中有多少是m的倍数;
思路:状态压缩dp;
dp[i][j]表示在i状态下对m取模为j的种数;我们可以将这些数组合,那么种数就是2n,然后每一种组合就是一种状态,那么每种状态下可能的模数有m-1种,那么咋实现全排列,
全排列就是组合数承n!;那么每种状态可以由前面的状态推来那么这就是全排列的过程,只不过将相同的合并。
状态转移方程看下面代码:
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<stdlib.h> 6 #include<math.h> 7 #include<queue> 8 #include<stack> 9 #include<vector> 10 using namespace std; 11 typedef long long LL; 12 char ans[100]; 13 int shu[100]; 14 LL dp[1<<16+1][22]; 15 int main(void) 16 { 17 int i,j,k; 18 scanf("%d",&k); 19 int s; 20 for(s=1; s<=k; s++) 21 { 22 int n,m; 23 scanf("%d %d",&n,&m); 24 scanf("%s",ans); 25 int l=strlen(ans); 26 for(i=0; i<l; i++) 27 { 28 if(ans[i]>=‘0‘&&ans[i]<=‘9‘) 29 { 30 shu[i]=ans[i]-‘0‘; 31 } 32 else 33 { 34 shu[i]=ans[i]-‘A‘+10; 35 } 36 } 37 memset(dp,0,sizeof(dp)); 38 dp[0][0]=1; 39 for(i=0; i<(1<<l); i++) 40 { 41 for(j=0; j<=m-1; j++) 42 { 43 for(int s=0; s<l; s++) 44 { 45 int ak=j; 46 if(!(i&(1<<s))) 47 { 48 dp[i|(1<<s)][(ak*n+shu[s])%m]+=dp[i][j]; 49 } 50 } 51 } 52 } 53 printf("Case %d: %lld\n",s,dp[(1<<l)-1][0]); 54 } 55 return 0; 56 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5601552.html