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减去 ...
随机推荐
- SpringMVC上传图片总结(1)---常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest
原文地址:https://blog.csdn.net/chenchunlin526/article/details/70945877 SpringMVC上传图片总结(1)---常规方法进行图片上传,使 ...
- ssl 内存泄露
http://i.mtime.com/chevalier/blog/1824652/ openssl内存分配 chevalier 发布于: 2009-04-20 10:31 openssl内存分配 ...
- Windows Phone 8.1 FilePicker API
在 Windows Phone 8.1 中,增加了 FilePicker 的方式与文件打交道,最大的亮点在于这种方式不仅可以浏览手机上的文件,还可以浏览符合协议的应用里的文件! 比如点击 OneDri ...
- 【27.77%】【BZOJ 4066】简单题
Time Limit: 50 Sec Memory Limit: 20 MB Submit: 1919 Solved: 533 [Submit][Status][Discuss] Descript ...
- js进阶正则表达式6转义字符(加\转义)(.符号)(|符号)
js进阶正则表达式6转义字符(加\转义)(.符号)(|符号) 一.总结 转义字符:{} () / $ # & * . ....... //3.特殊字符,都要加转义\ 点符号:var reg2= ...
- C++ 工具类 —— 词条类(Entry)
Entry 以键值对(key-value pair)的形式定义. template <typename K, typename V> struct Entry{ K key; V valu ...
- 【z06】观光公交
描述 风景迷人的小城Y市,拥有n个美丽的景点.由于慕名而来的游客越来越多,Y市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第0分钟出现在1号景点,随后依次前往2.3.4--n号景 ...
- Local database deployment problems and fixtures
/*By Jiangong SUN*/ After encountering some problems in deploying databases to local server, here ar ...
- 高CPU、数据库无法读写
高CPU.数据库无法读写的真凶 有兴趣的同学可以参考如下系列文章,都是针对dump分析的实战和总结: Windbg DUMP分析(原创汇总) http://www.cnblogs.com/Love ...
- 【codeforces 768A】Oath of the Night's Watch
[题目链接]:http://codeforces.com/contest/768/problem/A [题意] 让你统计这样的数字x的个数; x要满足有严格比它小和严格比它大的数字; [题解] 排个序 ...