4th IIUC Inter-University Programming Contest, 2005 |
|
H |
Simple Minded Hashing |
Input: standard input |
|
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.
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 |
Case 1: 4 |
题意: 用个l字母,组合出和为s。要求字母不重复,并且组合出来的方式不重复,既同一种最小字典序唯一。
思路:dp,不要被题目l < 10000 迷惑。因为字母不重复, 最多26个字母,超过26的肯定不行。所以开三维数组,存放状态,当前长度,当前末位大小,总和。然后进行状态转移。主要要打表。不然会超时
代码:
#include <stdio.h> #include <string.h> int min(int a, int b) {return a < b ? a : b;} const int N = 10005, M = 30; int l, s, dp[M][M][N]; void init() { dp[0][0][0] = 1; for (int i = 1; i <= 26; i ++) for (int j = 1; j <= 26 * i; j ++) { for (int k = 1; k <= 26 && k <= j; k ++) for (int l = 0; l < k; l ++) dp[i][k][j] += dp[i - 1][l][j - k]; } } int main() { int tt = 1; init(); while (~scanf("%d%d", &l, &s) && l + s) { l = min(l, 26); int ans = 0; for (int i = 1; i <= 26; i ++) ans += dp[l][i][s]; printf("Case %d: %d\n", tt ++, ans); } return 0; }