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 } 
  • 相关阅读:
    ASP.NET身份验证机制membership入门——API篇
    测试SQL语句的执行时间
    ASP.NET身份验证机制membership入门——配置篇(2)
    ASP.NET身份验证机制membership入门——配置篇(1)
    ASP.NET身份验证机制membership入门——项目
    ASP.NET用户个性化设置Profile——配置1
    POJ 2762 强连通分量中存在单相连通边 【tarjan+toposort+缩点】.cpp
    POJ 2516 【最小费用最大流】.cpp
    POJ 1904 【强连通分量】.cpp
    POJ 1236 【强连通图+缩点】.cpp
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7484918.html
Copyright © 2011-2022 走看看