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

  1. 2
  2. 2 1
  3. 3 2
 

Sample Output

  1. 2
  2. 8

题意:求用k种珍珠组成长度为n的项链的个数

思路:用dp[i][j]表示长度为i。j种珍珠的个数。

非常easy推出dp[i][j] = dp[i]-1[j]*j+ dp[i-1][j-1]*(k-j+1),由于数据量非常大,所以我们须要用矩阵优化。关键构造出矩阵。本来我们是用k维的矩阵构造关系矩阵,可是如今我们要求的是:

dp[1][k]+dp[1][k]+....dp[n][k],所以我们都加一维来记录和。

首先我们利用滚动数组降维的思路构造一个矩阵:f[j] = f[j-1]*j + f[j]*(k-j+1), 由于我们须要的是和以及fk。所以第一维就确定下来了

| 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|

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7. typedef unsigned long long ll;
  8. const int maxn = 35;
  9. const int mod = 1234567891;
  10.  
  11. int cnt;
  12. struct Matrix {
  13. ll v[maxn][maxn];
  14. Matrix() {}
  15. Matrix(int x) {
  16. init();
  17. for (int i = 0; i < maxn; i++)
  18. v[i][i] = x;
  19. }
  20. void init() {
  21. memset(v, 0, sizeof(v));
  22. }
  23. Matrix operator *(Matrix const &b) const {
  24. Matrix c;
  25. c.init();
  26. for (int i = 0; i < cnt; i++)
  27. for (int j = 0; j < cnt; j++)
  28. for (int k = 0; k < cnt; k++)
  29. c.v[i][j] = (c.v[i][j] + (ll)(v[i][k]*b.v[k][j])) % mod;
  30. return c;
  31. }
  32. Matrix operator ^(int b) {
  33. Matrix a = *this, res(1);
  34. while (b) {
  35. if (b & 1)
  36. res = res * a;
  37. a = a * a;
  38. b >>= 1;
  39. }
  40. return res;
  41. }
  42. } a, b, tmp;
  43.  
  44. int main() {
  45. int t, n, k;
  46. scanf("%d", &t);
  47. while (t--) {
  48. scanf("%d%d", &n, &k);
  49. a.init();
  50. a.v[0][0] = a.v[0][k] = 1;
  51. for (int j = 1; j <= k; j++) {
  52. if (j > 1)
  53. a.v[j][j-1] = k-(j-1);
  54. a.v[j][j] = j;
  55. }
  56. cnt = k + 1;
  57. ll num[maxn];
  58. memset(num, 0, sizeof(num));
  59. num[1] = k;
  60. tmp = a^n;
  61. ll ans[maxn];
  62. memset(ans, 0, sizeof(ans));
  63. for (int i = 0; i < cnt; i++)
  64. if (num[i])
  65. for (int j = 0; j < cnt; j++)
  66. if (tmp.v[j][i])
  67. ans[j] = (ans[j]+ (ll)(tmp.v[j][i]*num[i])) % mod;
  68. cout << ans[0] << endl;
  69. }
  70. return 0;
  71. }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)的更多相关文章

  1. hdu 4576(概率dp+滚动数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 思路:由于每次从某一位置到达另一位置的概率为0.5,因此我们用dp[i][j]表示第i次操作落在 ...

  2. HDU - 6578 Blank DP + 滚动数组

    HDU - 6578 Blank 题意 给你\(\{0,1,2, 3\}\)四个数,分别填入长度为\(n\)的数列中,有\(m\)个限制条件,\(l_{i}, r_{i}, x_{i}\)表示在\([ ...

  3. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  4. HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...

  5. HDU_1024.MaxSumPlusPlus(基础DP + 滚动数组优化讲解)

    这道题打破了我常规的做题思路,因为这是我刚开始训练DP,感觉这道题目好晕眼呀,emm其实就是感觉自己是真的菜...... 为什么说打破了我的做题思路呢,因为我平时看题解都是在已经AC或者完全不懂的情况 ...

  6. POJ 3666 Making the Grade (DP滚动数组)

    题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...

  7. USACO 2009 Open Grazing2 /// DP+滚动数组oj26223

    题目大意: 输入n,s:n头牛 s个栅栏 输入n头牛的初始位置 改变他们的位置,满足 1.第一头与最后一头的距离尽量大 2.相邻两头牛之间的距离尽量满足 d=(s-1)/(n-1),偏差不超过1 3. ...

  8. HDU 1176 免费馅饼 矩阵取数, dp + 滚动数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1176 首先可以处理出整张地图的状态. book[T][POS]表示第T秒,在第pos个地方有多少个馅饼. dp[ ...

  9. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

随机推荐

  1. 【C++竞赛 D】树的深度

    时间限制:1s 内存限制:32MB 问题描述 数据结构中定义,树的高度为一棵树中所有节点的层次的最大值.现在yyy有一棵树请你帮他求出该树的高度. 输入描述 第一行一个整数T(1≤T≤20)表示数据组 ...

  2. 【Record】9.16..9.23

  3. [SCSS] Organize Styles with SCSS Nesting and the Parent Selector

    SCSS nesting can produce DRYer code by targeting child elements without having to write the parent c ...

  4. 从多路搜索树到 B-树

    1. 什么是 B 树 B 树是为磁盘或其他直接存取的辅助存储设备而设计的一种平衡二叉树: B 树类似于红黑树,但它们在降低磁盘 I/O 操作数方面要更好一点, 许多数据库系统使用 B 树或者 B 树的 ...

  5. GTID的限制

    1.不支持非事务引擎(从库报错,stop slave;start slave;忽略). 2.不支持create table ... select 语句复制(主库直接报错). 3.不允许一个SQL同时更 ...

  6. Linux安装.Net core 环境并运行项目

    原文:Linux安装.Net core 环境并运行项目 一 安装环境 1.  从微软官网下载 Linux版本的.NetCoreSdk 2.0 安装包 打开终端: 第一步: sudo yum insta ...

  7. [UWP]使用Acrylic(亚克力)

    原文:[UWP]使用Acrylic(亚克力) 1. 前言 在 如何使用Fluent Design System 这篇文章里已经简单介绍过Reveal的用法,这篇再详细介绍其它内容. 自Windows ...

  8. WPF入门(三)->两个几何图形合并(CombinedGeometry)

    原文:WPF入门(三)->两个几何图形合并(CombinedGeometry) 在WPF中,提供了一个CombinedGeometry对象可以使两个几何图形合并产生效果 CombinedGeom ...

  9. 【noip模拟】德充符

    时间限制:2s 内存限制:512MB [题目描述] 申徒嘉和郑子产都是伯昏无人的学生,子产因为申徒嘉是残疾人,非常看不起他,于是想要刁难他. 子产给了申徒嘉 n个数 a1,a2...an. 现在他要求 ...

  10. Docker Xshell

    Windows安装Docker Xshell无法连接虚拟机解决方案 DOCKER windows安装 6.1 下载地址 6.2 用FTP工具上传tar包 6.3 安装 6.4 查看镜像 6.5 运行 ...