HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)
Description
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
Technical Specification
1 ≤ T ≤ 10
1 ≤ N ≤ 1,000,000,000
1 ≤ K ≤ 30
Output
Sample Input
2
2 1
3 2
Sample Output
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|
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef unsigned long long ll;
const int maxn = 35;
const int mod = 1234567891; int cnt;
struct Matrix {
ll v[maxn][maxn];
Matrix() {}
Matrix(int x) {
init();
for (int i = 0; i < maxn; i++)
v[i][i] = x;
}
void init() {
memset(v, 0, sizeof(v));
}
Matrix operator *(Matrix const &b) const {
Matrix c;
c.init();
for (int i = 0; i < cnt; i++)
for (int j = 0; j < cnt; j++)
for (int k = 0; k < cnt; k++)
c.v[i][j] = (c.v[i][j] + (ll)(v[i][k]*b.v[k][j])) % mod;
return c;
}
Matrix operator ^(int b) {
Matrix a = *this, res(1);
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
} a, b, tmp; int main() {
int t, n, k;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &k);
a.init();
a.v[0][0] = a.v[0][k] = 1;
for (int j = 1; j <= k; j++) {
if (j > 1)
a.v[j][j-1] = k-(j-1);
a.v[j][j] = j;
}
cnt = k + 1;
ll num[maxn];
memset(num, 0, sizeof(num));
num[1] = k;
tmp = a^n;
ll ans[maxn];
memset(ans, 0, sizeof(ans));
for (int i = 0; i < cnt; i++)
if (num[i])
for (int j = 0; j < cnt; j++)
if (tmp.v[j][i])
ans[j] = (ans[j]+ (ll)(tmp.v[j][i]*num[i])) % mod;
cout << ans[0] << endl;
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)的更多相关文章
- hdu 4576(概率dp+滚动数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 思路:由于每次从某一位置到达另一位置的概率为0.5,因此我们用dp[i][j]表示第i次操作落在 ...
- HDU - 6578 Blank DP + 滚动数组
HDU - 6578 Blank 题意 给你\(\{0,1,2, 3\}\)四个数,分别填入长度为\(n\)的数列中,有\(m\)个限制条件,\(l_{i}, r_{i}, x_{i}\)表示在\([ ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
- HDU_1024.MaxSumPlusPlus(基础DP + 滚动数组优化讲解)
这道题打破了我常规的做题思路,因为这是我刚开始训练DP,感觉这道题目好晕眼呀,emm其实就是感觉自己是真的菜...... 为什么说打破了我的做题思路呢,因为我平时看题解都是在已经AC或者完全不懂的情况 ...
- POJ 3666 Making the Grade (DP滚动数组)
题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...
- USACO 2009 Open Grazing2 /// DP+滚动数组oj26223
题目大意: 输入n,s:n头牛 s个栅栏 输入n头牛的初始位置 改变他们的位置,满足 1.第一头与最后一头的距离尽量大 2.相邻两头牛之间的距离尽量满足 d=(s-1)/(n-1),偏差不超过1 3. ...
- HDU 1176 免费馅饼 矩阵取数, dp + 滚动数组
http://acm.hdu.edu.cn/showproblem.php?pid=1176 首先可以处理出整张地图的状态. book[T][POS]表示第T秒,在第pos个地方有多少个馅饼. dp[ ...
- hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...
随机推荐
- image-base64互转
package base64StringToImage; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStrea ...
- PatentTips - Highly-available OSPF routing protocol
BACKGROUND OF THE INVENTION FIG. 1A is a simplified block diagram schematically representing a typic ...
- windows下perl使用
windows下perl使用 windows下perl开发工具 Perl 的官方网址提供下载的windows perl开发环境 ActiveState Perl Strawberry Perl DWI ...
- thinkphp3.1 发送email
//*********************发送邮件************************** Vendor('email'); //******************** 配置信息 * ...
- Global Git ignore - Stack Overflow
https://stackoverflow.com/questions/7335420/global-git-ignore git config --global core.excludesfile ...
- html常用样式margin、border怎么使用
html常用样式margin.border怎么使用 一.总结 一句话总结:1.margin:auto配合width才能居中:2.border的三个属性依次是边框宽度,边框样式,边框颜色 1.html中 ...
- [Ramda] Refactor a Promise Chain to Function Composition using Ramda
Promise chains can be a powerful way to handle a series of transformations to the results of an asyn ...
- Erlang 日期和时间处理、时间戳转换
http://www.csdn 123.com/html/blogs/20131113/95993.htm 获取当前时间 erlang:now()得到的是从1970年1月1日零时起,到现在经过的时间, ...
- 小强的HTML5移动开发之路(33)—— jqMobi基础
一.什么是jqMobi jqMobi是由appMobi针对HTML5浏览器和移动设备开发的javascript框架,是个极快速的查询选择库,支持W3C查询. 版本 jqMobi源码最初在2012年1月 ...
- js一些编写的函数
第一:它是最常见的 function A(){ } 说明 A(); 第二: var B = function(){ } 方法 B();//这是匿名函数 第三: (function () { ...