Uva 10912 Simple Minded Hashing (计数DP)

4th IIUC Inter-University Programming Contest, 2005

H

Simple Minded Hashing

Input: standard input
Output: standard output

Problemsetter: Sohel Hafiz

All of you know a bit or two about hashing. It involves mapping an element into a numerical value using some mathematical function. In this problem we will consider a very ‘simple minded hashing’. It involves assigning numerical value to the alphabets and summing these values of the characters.

For example, the string “acm” is mapped to 1 + 3 + 13 = 17. Unfortunately, this method does not give one-to-one mapping. The string “adl” also maps to 17 (1 + 4 + 12). This is called collision.

In this problem you will have to find the number of strings of length L, which maps to an integer S, using the above hash function. You have to consider strings that have only lowercase letters in strictly ascending order.

Suppose L = 3 and S = 10, there are 4 such strings.

  1. abg
  2. acf
  3. ade
  4. bce

agb also produces 10 but the letters are not strictly in ascending order.

bh also produces 10 but it has 2 letters.

Input

There will be several cases. Each case consists of 2 integers L and S(0 < L, S < 10000). Input is terminated with 2 zeros.

Output

For each case, output Case #: where # is replaced by case number. Then output the result. Follow the sample for exact format. The result will fit in 32 bit signed integers.

Sample Input

Output for Sample Input

3 10
2 3
0 0

Case 1: 4
Case 2: 1


题意: 给你 m 和 n ,要你用找出 m 个依次 递增的数 (编号从 1 开始)(数字范围1---26),使他

们的和为 n ,输出满足条件的方案数。

思路:因为数字范围是 1---26 ,而题目要找的是m 个依次递增的数 ,所以可知 m 〉26 时  方案数  ans = 0;

           同理我们可以知道   n 的最大值为 : 1+2+3+......+26 = 351     即  n 〉351 时  方案数 ans = 0;

所以我们只需讨论  m 〈 = 26  , n 〈= 351 时的情况 。

 dp[ i ] [ j ][ k ]   表示 第 i 个数 取的是 k ,并且总和是 j 的方案数 。

即    dp[ i ] [ j ][ k ]  + =   dp[ i-1 ] [ j-k ][ t ] (0〈= t 〈 k) ;


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=405;

int n,m,cnt,dp[30][maxn][30];

void initial()
{
    memset(dp,0,sizeof(dp));
}

void solve()
{
    int ans=0;
    if(m>26 || n>351)  ans=0;
    else
    {
        dp[0][0][0]=1;
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
                for(int k=1; k<=26; k++)
                {
                    int w=j-k;
                    if(w<0)  break;
                    for(int t=0;t<k;t++)  dp[i][j][k]+=dp[i-1][w][t];
                }
        }
        for(int i=1; i<=26; i++)  ans+=dp[m][n][i];
    }
    printf("Case %d: %d\n",++cnt,ans);
}

int main()
{
    while(scanf("%d %d",&m,&n)!=EOF)
    {
        if(m==0 && n==0)  break;
        initial();
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值