题目: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的更多相关文章

  1. [HDU2294] Pendant - 矩阵加速递推

    Pendant Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  2. hdu2294:Pendant

    T<=10组数据问K<=30种珠子每种n<=1e9串成1~n长度的序列共有多少种,mod1234567891. 方程没想到.矩阵不会推.很好. f[i][j]--长度i,j种珠子方案 ...

  3. Pendant

    Pendant Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. HDU - 2294: Pendant(矩阵优化DP&前缀和)

    On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...

  5. HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)

    Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend ...

  6. 2016.03.31,英语,《Vocabulary Builder》Unit 08

    tend/tent: from the Latin tendere, meaning 'to stretch, extend, or spread'. tent: [tent] n. 帐篷 vt.&a ...

  7. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  8. 无废话网页重构系列——(6)HTML主干结构:站点(site)、页面(page)

    本文作者:大象本文地址:http://www.cnblogs.com/daxiang/p/4653546.html 在分析和切出设计稿,以及部署项目目录文件后,开始写HTML Demo. 首先,弄出H ...

  9. OpenCV码源笔记——Decision Tree决策树

    来自OpenCV2.3.1 sample/c/mushroom.cpp 1.首先读入agaricus-lepiota.data的训练样本. 样本中第一项是e或p代表有毒或无毒的标志位:其他是特征,可以 ...

随机推荐

  1. 使用多线程开启OCR

    需求:经过opencv 或者其他算法对一张图片里面的文字内容进行切割,获取到切割内容的坐标信息,再使用ocr进行识别.一张一张识别太慢了,我们可以开启多线程识别.代码如下 threads = [] f ...

  2. TCP协议-流量控制

    流量控制是通过滑动窗口来实现控制的.使用了坚持定时器,防止发送失败导致死锁.

  3. js实现千位符分隔

    前几天面试做保险项目的公司,被问到了一道实现千位符分割方法的题,乍一看挺简单,但做起来最后却没给出来一个合适的解决方法.回来自己琢磨了一个还行的答案. var num = 3899000001, ar ...

  4. 2019 最新 Java 核心技术教程,都在这了!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 以下是Java技术栈微信公众号发布的所有关于 Java 的技术干货,会从以下几个方面汇总,本文会长期更新. Java 基础篇 ...

  5. 2019 Multi-University Training Contest 1 - 1011 - Function - 数论

    http://acm.hdu.edu.cn/showproblem.php?pid=6588 新学到了一个求n以内与m的gcd的和的快速求法.也就是下面的S1. ①求: $ \sum\limits_{ ...

  6. hdu4352 XHXJ's LIS(数位dp)

    题目传送门 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. mysql (mariadb)表结构添加修改删除方法总结

    1,添加表字段 alter table table1 add ptel varchar(100) not Null; alter table table1 add id int unsigned no ...

  8. P5459 [BJOI2016]回转寿司

    传送门 暴力怎么搞,维护前缀和 $s[i]$ ,对于每一个 $s[i]$,枚举所有 $j\in[0,i-1]$,看看 $s[i]-s[j]$ 是否属于 $[L,R]$ 如果属于就加入答案 $s[i]- ...

  9. 并行开发 8.用VS性能向导解剖你的程序

    原文:8天玩转并行开发——第八天 用VS性能向导解剖你的程序 最后一篇,我们来说说vs的“性能向导",通常我们调试程序的性能一般会使用Stopwatch,如果希望更加系统的了解程序,我们就需 ...

  10. JS 逻辑非!简单总结

    !""                  true!"aaa"          false""==false          true ...