zoukankan      html  css  js  c++  java
  • Painful Bases LightOJ

    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

    3

    2 2

    10

    10 2

    5681

    16 1

    ABCDEF0123456789

    Sample Output

    Case 1: 1

    Case 2: 12

    Case 3: 20922789888000

    题解:dp[ i ][ j ]表示状态为 i 时模K余数为 j 的方案数。由整除K而想到模K的余数进而用DP求解,实在是厉害!其二是求每个状态的余数。

     1 #include<cstdio>                  //非原著,orz!!!
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long ll;
     7 
     8 const int INF=0x3f3f3f3f;
     9 const int maxn=1<<16;
    10 
    11 int base,K,kase,n;
    12 int num[25];
    13 ll dp[maxn][20];
    14 string S;
    15 
    16 void Inite(){
    17     n=S.size();
    18     for(int i=0;i<n;i++){
    19         if(S[i]<='9'&&S[i]>='0') num[i]=S[i]-'0';
    20         else num[i]=S[i]-'A'+10; 
    21     }
    22 }
    23 
    24 void solve(int t){
    25     memset(dp,0,sizeof(dp));
    26     dp[0][0]=1;
    27     for(int i=0;i<(1<<n);i++){
    28         for(int j=0;j<K;j++){
    29             if(!dp[i][j]) continue;
    30             for(int k=0;k<n;k++){
    31                 if(i&(1<<k)) continue;
    32                 dp[i|(1<<k)][(base*j+num[k])%K]+=dp[i][j];   //假设余数为j时所代表的数是mnum,那么将k添加到末尾所新组成的数是(mnum*base+num[k]),然后对
    33             }                             //K取模,又mnum%K=j,所以由模运算可得(mnum*base+num[k])%K=(j*base+num[k])%K;
    34         }
    35     }
    36     printf("Case %d: %lld
    ",t,dp[(1<<n)-1][0]);
    37 }
    38 
    39 int main()
    40 {   cin>>kase;
    41     for(int t=1;t<=kase;t++){
    42         cin>>base>>K;
    43         cin>>S;
    44         Inite();
    45         solve(t);
    46     }
    47     return 0;
    48 } 
  • 相关阅读:
    windows service 安装和卸载指令
    jackson 进行json与java对象转换 之四
    jackson 进行json与java对象转换 之三
    jackson 进行json与java对象转换 之二
    jackson 进行json与java对象转换 之一
    jackson2.x与Jackson1.9的比较
    判断Integer值相等不能用==
    java:String使用equals和==比较的区别
    Java中自定义枚举(Enum)项的值,可设置为指定的值
    Java enum(枚举)使用详解之四
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7484918.html
Copyright © 2011-2022 走看看