【hdu2294】Pendant

2014年3月27日1,2130

 

Problem Description
On Saint Valentine’s Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Alex is very rich, and he has N pearls of each kind. Pendant can be told apart according to permutation of its pearls. Now he wants to know how many kind of pendant can he made, with length between 1 and N. Of course, to show his wealth, every kind of pendant must be made of K pearls.
Output the answer taken modulo 1234567891.
Input
The input consists of multiple test cases. The first line contains an integer T indicating the number of test cases. Each case is on one line, consisting of two integers N and K, separated by one space.
Technical Specification
1 ≤ T ≤ 10
1 ≤ N ≤ 1,000,000,000
1 ≤ K ≤ 30
Output
Output the answer on one line for each test case.
Sample Input
2 2 1 3 2
Sample Output
2 8
题解

* [题意]
* 有k种珍珠,每种珍珠N个,问长度<=N且有k种珍珠的垂饰有多少个?
* [解题方法]
* dp[i][j]表示长度为i的并且有j种珍珠的垂饰有多少个
* 则有状态转移:dp[i][j] = (k-(j-1))*dp[i-1][j-1] + j*dp[i-1][j];
* 由于N太大,所以把i看成“阶段”,构造矩阵,通过矩阵快速转移
* 设第i阶段的一维数组(dp[i][0~j])状态设为F,转移矩阵为init(k+1阶方阵)
* 则有:init * F = F’;(F’为新状态)
* 设答案 = gn = dp[1][k] + dp[2][k] + … + dp[n][k]
*
* 所以有矩阵:
* | 1 0……………0 1 | |g0| |g1’|
* | 0 1 0……………0 | |f1| |f1’|
* | 0 k-1 2………….0 | |f2| |f2’|
* | ………………… | * |..| = |..’|
* | 0…0 k-(j-1) j 0…0 | |fj| |fj’|
* | ………………… | |..| |..’|
* | 0……………0 1 k | |fk| |fk’|
*
* 这个代码的初始化:[g0, f1, f2, …, fk] = {0, k, 0, …, 0}