[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代表有毒或无毒的标志位:其他是特征,可以 ...
随机推荐
- JNI-java native interface(java本地接口)
什么是JNI java native interface(java本地接口) ABI: application binary interface (应用程序二进制接口) 为什么要使用JNI * 复用很 ...
- Python几行代码实现邮件发送
话不多说直接进入正题 首先我们需要安装一个名为'zmail'的包,终端执行'pip install zmail'即可实现安装. 直接上代码 import zmail mail = { 'subject ...
- JavaScript高级程序设计(第3版) 第四章(变量、作用域和内存问题)
4.1 基本类型和引用类型的值 1.基本类型的值是(简单的数据段),引用类型的值是(保存在内存中的对象). 基本类型的值在内存中占据固定大小的空间,因此被保存在栈中.(lifo ...
- C++ std::isnan等函数的使用
C和C++11标准提供了类似于isnan.isfinite.isinf.isnormal.fpclassify分别用于判断是非数(NaN)值.有限制.无穷值.正常数值等. 今天在使用Modbus读取设 ...
- Manacher(最长镜面回文串)
I - O'My! Gym - 101350I Note: this is a harder version of Mirrored string I. The gorillas have recen ...
- Capture pictures using Jpython
Becuz it is a jpython code, we can use it in Sikuli. from time import strftime, gmtime from java.awt ...
- break和continue能否跳出函数
int func() { printf("In func, before continue.\n"); // continue; break; printf("In fu ...
- 安装weblogic界面安装
如果中间有任何问题请联系作者:291562721 界面安装weblogic首先 需要工具: 链接:https://pan.baidu.com/s/1x3uYxsnycjT2Xi2TOTbDdQ 提取码 ...
- Java-java.lang.NoClassDefFoundError:brave.Span.Kind
今天在升级某个框架时,遇到如标题描述的问题.这个问题应该说还是比较明显的,首先去搜了一下NoClassDefFoundError的问题,参考这篇博客:https://www.cnblogs.com/x ...
- 312-金胜维 P系列2.5寸 480G SATA3 SSD固态硬盘
金胜维 P系列2.5寸 480G SATA3 SSD固态硬盘 一.概述 P系列为KingSpec金胜维针对入门级消费群体推出的一款高性价SSD固态硬盘,采用7mm超薄金属冲压外壳,板载优质TL ...