Pendant |
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 115 Accepted Submission(s): 70 |
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 个,做一条项链,每种珍珠都要用上。 问 1 到 N的长度能组成几种。
首先想dp,很明显可以想到:
dp[i][j] = (k-(j-1))*dp[i-1][j-1] + j*dp[i-1][j]
(dp[i][j]表示长度为i的并且有j种珍珠的垂饰有多少个)
g[n] = g[n-1] + dp[n][K], g[n] 为最后求解的答案(表示 1 到 n 的 长度可以组成几种
但是 数据量太大 1e9 数组开不了那么大,观察这个式子是一个齐次线性方程,可以通过矩阵来求解。
设第i阶段的一维数组(dp[i][0~j])状态设为F,转移矩阵为A(k+1阶方阵) 则有:A * F = F';(F'为新状态)
这个代码的初始化:
|
HDU2294 Pendant (齐次线性方程的矩阵解法 + 矩阵快速幂)
最新推荐文章于 2019-01-08 08:27:20 发布