Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 107 Accepted Submission(s): 69

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

Source

The 4th Baidu Cup final

Recommend

lcy

想当然的以为这是一道组合数学,根本没有往动规方向想(也有组合公式,不过不会推。。。)

状态转移方程为:

以F[i][j]表示长度为i的pendant,用了j种珍珠,所构成的方案数,

则 F[i][j]=F[i-1][j]*j+F[i-1][j-1]*(k-j+1)。

可是i最大值太过巨大,所以用矩阵来优化(万能的矩阵,只要是递推就能优化。。。。)

然后注意需要求的结果是和,所以在构造矩阵时需要多加一维来计算和

矩阵如下(当然也可以有其他的构造方法):

| 1 0...............0 1  |           |g|

| 0 1 0...............0  |           |f1|  
| 0 k-1 2.............0 |           |f2|
| .....................      |      *    .

| 0...0 k-(j-1) j 0...0|           .
| .....................     |            .
| 0...............0 1 k |            |fk|

HDU2294--Pendant(DP,矩阵优化)的更多相关文章

  1. HDU - 2294: Pendant(矩阵优化DP&前缀和)

    On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...

  2. hdu 4576(简单概率dp | 矩阵优化)

    艰难的一道题,体现出菜菜的我... 首先,先吐槽下. 这题到底出题人是怎么想的,用普通概率dp水过??? 那为什么我概率dp写的稍微烂点就一直tle?  感觉很不公平.大家算法都一致,因为我程序没有那 ...

  3. CF1151F Sonya and Informatics (计数dp+矩阵优化)

    题目地址 Solution (duyi是我们的红太阳) (这里说一句:这题看上去是一个概率dp,鉴于这题的概率dp写法看上去不好写,我们其实可以写一个计数dp) 首先拿到这个题目我们要能设出一个普通d ...

  4. Codeforces 917C - Pollywog(状压 dp+矩阵优化)

    UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...

  5. New Year and Old Subsequence CodeForces - 750E (dp矩阵优化)

    大意: 给定字符串, 每次询问区间[l,r]有子序列2017, 无子序列2016所需要删除的最小字符数 转移用矩阵优化一下, 要注意$(\mathbb{Z},min,+)$的幺元主对角线全0, 其余全 ...

  6. BZOJ4000 [TJOI2015]棋盘 【状压dp + 矩阵优化】

    题目链接 BZOJ4000 题解 注意题目中的编号均从\(0\)开始= = \(m\)特别小,考虑状压 设\(f[i][s]\)为第\(i\)行为\(s\)的方案数 每个棋子能攻击的只有本行,上一行, ...

  7. [Vijos1067]Warcraft III 守望者的烦恼(DP + 矩阵优化)

    传送门 可知 f[i] = f[i - 1] + f[i - 2] + ... + f[i - k] 直接矩阵优化就好了 #include <cstdio> #include <cs ...

  8. BZOJ 1009: [HNOI2008]GT考试(kmp+dp+矩阵优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 思路:真的是好题啊! 对于这种题目,很有可能就是dp,$f[i][j]$表示分析到第 ...

  9. bzoj 1009 DP 矩阵优化

    原来的DP: dp[i][j]表示长度为i的合法串,并且它的长度为j的后缀是给定串的长度为j的前缀. 转移: i==0 dp[0][0] = 1 dp[0][1~m-1] = 0 i>=1 dp ...

  10. [POJ2778]DNA Sequence(AC自动机 + DP + 矩阵优化)

    传送门 AC自动机加DP就不说了 注意到 m <= 10,所以模式串很少. 而 n 很大就需要 log 的算法,很容易想到矩阵. 但是该怎么构建? 还是矩阵 A(i,j) = ∑A(i,k) * ...

随机推荐

  1. 洛谷P1029 最大公约数和最小公倍数问题 (简单数学题)

    一直懒的写博客,直到感觉不写不总结没有半点进步,最后快乐(逼着)自己来记录蒟蒻被学弟学妹打压这一年吧... 题目描述 输入22个正整数x_0,y_0(2 \le x_0<100000,2 \le ...

  2. cs244a-Introduction to Computer Networking-Unit1

    Unit 1 学习目标: how an application use the Internet The structure of the Internet:The 4 layer model The ...

  3. Codeforces - 1203D2 - Remove the Substring (hard version) - 双指针

    https://codeforces.com/contest/1203/problem/D2 上次学了双指针求两个字符串之间的是否t是s的子序列.但其实这个双指针可以求出的是s的前i个位置中匹配t的最 ...

  4. css sticky footer 布局

    方法一:footer 上用负的 margin-top 在内容外面需要额外包一层元素(wrap)来让它产生对应的 padding-bottom.是为了防止负 margin 导致 footer 覆盖任何实 ...

  5. vue-resource对比axios import ... from和import {} from 的区别 element-ui

    1.vue-resource对比axios 文章1 文章2 1.0 axios  params 配置参数在url 显示,form-data 用于 图片上传.文件上传 1.1 axios 全局配置 ax ...

  6. bcdedit删除uefi多余项

    1.检查是否有多余的启动项:用管理员权限的cmd运行Bcdedit /enum firmware 2.保存现在的所有引导项Bcdedit /export savebcdsavebcd是导出的文件名 3 ...

  7. Delphi 赋值语句和程序的顺序结构

  8. Linux之RedHat7如何更换yum源

    目前,我们常见的系统大概就是Windows.Linux和Mac OS了.Windows系统应该是大部分人最早开始接触的系统,毕竟Windows系统使用起来相当方便,只需要点点鼠标,外加会简单的打字,一 ...

  9. RHEL6 中/etc/fstab文件解析

    1.系统环境 [root@natsha ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.5 (Santiag ...

  10. EFCore, 输出执行的Sql语句到控制台或者调试窗口

    .net core 已经集成的各种日志功能,使用efcore时,只需要按情况引入相应的包即可,如果你用的是.net core调试,那么可以引入 Microsoft.Extensions.Logging ...