[HDU2294]Pendant
题目:Pendant
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2294
分析:
1)f[i][j]表示长度为i,有j种珍珠的吊坠的数目。
$f[i][j] = (k - j + 1) * f[i - 1][j - 1] + j * f[i - 1][j] $
2)用矩阵来转移.
转移矩阵:
$\left[ \begin{array}{cccccc} 1 & 0 & 0 & ... & 0 & 0 \\ 0 & 1 & k-(2-1) & ... & 0 & 0 \\ 0 & 0 & 2 & ... & 0 & 0 \\ ... & ... & ... & ... & ... & ... \\ 0 & 0 & 0 & ... & k-1 & k-(k-1) \\ 0 & 0 & 0 & ... & 0 & k \end{array} \right] $
状态矩阵:
$\left[ \begin{array}{ccccc} sum[i-1] & f[i][1] & f[i][2] & ... & f[i][k] \end{array} \right] $
初始矩阵:
$\left[ \begin{array}{ccccc} 0 & k(f[i][1]的值) & 0 & ... & 0 \end{array} \right] $
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int MOD=;
struct Matrix{
LL n,a[][];
void init(int _n,int f){
n=_n;
memset(a,,sizeof a);
if(f==-)return;
for(int i=;i<=n;++i)a[i][i]=;
}
};
Matrix operator*(Matrix& A,Matrix& B){
Matrix C;C.init(A.n,-);
for(int i=;i<=C.n;++i)
for(int j=;j<=C.n;++j)
for(int k=;k<=C.n;++k){
C.a[i][j]+=A.a[i][k]*B.a[k][j];
C.a[i][j]%=MOD;
}
return C;
}
Matrix operator^(Matrix A,int n){
Matrix Rt;Rt.init(A.n,);
for(;n;n>>=){
if(n&)Rt=Rt*A;
A=A*A;
}
return Rt;
}
int main(){
int Case;scanf("%d",&Case);
Matrix A,T;
for(int n,k;Case--;){
scanf("%d%d",&n,&k);
memset(T.a,,sizeof T.a);
T.n=k;
T.a[][]=;T.a[k][]=;
T.a[][]=;
for(int i=;i<=k;++i){
T.a[i][i]=i;
T.a[i-][i]=k-i+;
}
A=T^n;
printf("%lld\n",A.a[][]*k%MOD);
}
return ;
}
[HDU2294]Pendant的更多相关文章
- [HDU2294] Pendant - 矩阵加速递推
Pendant Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu2294:Pendant
T<=10组数据问K<=30种珠子每种n<=1e9串成1~n长度的序列共有多少种,mod1234567891. 方程没想到.矩阵不会推.很好. f[i][j]--长度i,j种珠子方案 ...
- Pendant
Pendant Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU - 2294: Pendant(矩阵优化DP&前缀和)
On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...
- HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)
Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend ...
- 2016.03.31,英语,《Vocabulary Builder》Unit 08
tend/tent: from the Latin tendere, meaning 'to stretch, extend, or spread'. tent: [tent] n. 帐篷 vt.&a ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 无废话网页重构系列——(6)HTML主干结构:站点(site)、页面(page)
本文作者:大象本文地址:http://www.cnblogs.com/daxiang/p/4653546.html 在分析和切出设计稿,以及部署项目目录文件后,开始写HTML Demo. 首先,弄出H ...
- OpenCV码源笔记——Decision Tree决策树
来自OpenCV2.3.1 sample/c/mushroom.cpp 1.首先读入agaricus-lepiota.data的训练样本. 样本中第一项是e或p代表有毒或无毒的标志位:其他是特征,可以 ...
随机推荐
- socketpair
与pipe的区别 pipe产生的文件描述符是半双工的,需要pipe两次才能实现全双工,产生的两个描述符是一个读,一个写 socketpair直接就可以全双工,产生的两个文件描述符的任何一个都可读可写 ...
- is_numeric漏洞总结
将16进制数据判断为数据,这样在存入数据库后,数据库会把16进制解析成字符串,可能造成二次注入 转载: https://www.jianshu.com/p/e7cf997d6ccb
- Python 根据入栈顺利判定出栈顺序
1.读取入栈,出栈数据: 2.把数据分别转化成整数列表: 3.新建栈列表,用入栈数据进行压栈:如果栈列表不为空,并且栈顶层数据为出栈的元素:删除栈列表的顶层数据: 4.如果栈列表不为空,说明栈列表里面 ...
- Excel 技巧
<!-- Excel跳转到指定行指定列 --> =HYPERLINK("#"&ADDRESS(要跳转到的行数,要跳转到的列数),"跳转")
- stl应用(map)或字典树(有点东西)
M - Violet Snow Gym - 101350M Every year, an elephant qualifies to the Arab Collegiate Programming C ...
- SR-IOV
SR-IOV 来源 http://blog.csdn.net/liushen0916/article/details/52423507 摘要: 介绍SR-IOV 的概念.使用场景.VMware 和 K ...
- docker镜像源设置
由于docker默认镜像源为国外官方源,下载速度较慢.设置国内镜像源可加速 修改文件 /etc/docker/daemon.json vi /etc/docker/daemon.json 添加以下内容 ...
- php之ob_start()缓冲区
ob_get_contents()函数及与其相关几个函数的用法 ob_start() ob_get_contents(); 获取缓冲区内容,如果是纯 html内容或标签,则都会放于浏览器的缓冲区中. ...
- golang指针函数
func main() { a := models.SmsVerify{} a.Id = 100 fmt.Println(a.Id) // 100 test111(a) fmt.Println(a.I ...
- Oracle:同步两张表的相同字段
有一个需求需要同步两张表的相同字段,比如表A和表B,这两张表是不同的用户下的表,表结构是一样的. 一开始我简单写了一个sql语句,如下: update ord_log1 A set (A.pid, A ...