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. JackRabbit的来源

    题记 写这系列有点老调重弹的味道,比如ahuaxuan已经在他的博客里对于JackRabbit 1.0做了很详细的阐述.之所以再写,是因为JCR推出了JCR 2.0,个人觉得有必要将一些新的特性再罗列 ...

  2. 分库分表之后全局id咋生成?

    1.面试题 分库分表之后,id主键如何处理? 2.面试官心里分析 其实这是分库分表之后你必然要面对的一个问题,就是id咋生成?因为要是分成多个表之后,每个表都是从1开始累加,那肯定不对啊,需要一个全局 ...

  3. CF 1140B Good String

    Description You have a string ss of length nn consisting of only characters > and <. You may d ...

  4. MLS(移动最小二乘)

    https://blog.csdn.net/weixin_41484240/article/details/81204113 https://blog.csdn.net/baidu_38127162/ ...

  5. Electric Charges CodeForces - 623C (二分答案)

    大意: 平面上n个点每个点坐标为(x,0)或(0,y), 求任意两点距离平方最大值的最小值. 二分答案, 转化为判定最大值是否<=e, 按$x$排序后, 因为固定左端点, $y$绝对值的最大值是 ...

  6. [WPF]鼠标位置捕捉

    private void StackPanel_MouseMove(object sender, MouseEventArgs e) {     Debug.WriteLine("Move& ...

  7. luogu P4382 [九省联考2018]劈配

    luogu 我记得我第一次做这道题的时候屁都不会qwq 先考虑第一问,暴力是依次枚举每个人,然后从高到低枚举志愿,枚举导师,能选就选.但是可以发现前面的人选的导师可能会导致后面的人本来可以选到这个志愿 ...

  8. Flask开发系列之Web表单

    Flask开发系列之Web表单 简单示例 from flask import Flask, request, render_template app = Flask(__name__) @app.ro ...

  9. MySQL8连接数据库

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127. ...

  10. QQ第三方登陆

    第一步 引入第三方登陆类,实例化,调用类中方法getInstance()跳转到授权页面 第二步 登陆成功的回调方法,qq_return则是登陆成功会获取到的数据的处理方法 qq_return方法: 本 ...