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减去 ...
随机推荐
- Python数据分析环境和工具
一.数据分析工作环境 Anaconda: Anaconda(水蟒)是一个科学计算软件发行版,集成了大量常用扩展包的环境,包含了 Python 解释器,conda 包管理工具,以及 NumPy.Pand ...
- [Now] Deploy a Node project with Zeit’s Now
Use Zeit’s now to deploy a node application from your local machine to a remote cloud service in mom ...
- WordPress通过插件发送邮件
原文发表自我的个人站点,欢迎大家訪问~转载请保留本段,或注明原文链接:http://www.hainter.com/wordpress-send-email-via-plugins WordPress ...
- C#生成6位随机验证码
private string VerifyCode() { Random random = new Random(); , ).ToString(); }
- 清楚arp
转载:http://wscyza.blog.51cto.com/898495/728717/ linux系统下清空arp 缓存(清空arp表)方法 命令红色字体标记 系统初始arp环境 [ro ...
- nginx源代码分析--ngx_http_optimize_servers()函数
这个函数做了连部分工作:1)以port为入口点 将实用的信息存放到hash表内 2)调用ngx_http_init_listening()函数 对port进行监听 1. 在ngx_http_core_ ...
- struts2基本的配置代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- AngularJS之基本指令(init、repeat)
<h3>ng-init初始化变量</h3> <div ng-init="name='aurora';age='18'"> <p ng-bi ...
- ajax跳转到新的jsp页面(局部刷新)
ajax可以实现局部刷新页面,即在不刷新整个页面的情况下更新页面的局部信息. 项目中遇到一个问题:在用户列表也,当点击某个按钮时需要去查询用户的信息,查询成功跳转到用户详情界面:查询失败,则在原页面弹 ...
- Android BlueDroid(一):BlueDroid概述
关键词:bluedroid bluez作者:xubin341719(欢迎转载,请注明作者,请尊重版权,谢谢!)欢迎指正错误.共同学习.共同进步!! 一.名词解释:(实用信息添加中--) BTI F: ...