题目: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. js-判断当前页面是否在移动端打开显示的

    if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)==false) { //该页面不是在移动端打开的, }

  2. css圣杯布局

    HTML <div class="container"> <div class="content">content</div> ...

  3. servlet--三大域

    requset  \ session   servletContext  application

  4. redis可以做什么?

    redis可以做什么? 1.缓存,毫无疑问这是Redis当今最为人熟知的使用场景.在提升服务器性能方面非常有效: 2.排行榜,如果使用传统的关系型数据库来做这个事儿,非常的麻烦,而利用Redis的So ...

  5. html5_websql

    var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);  var msg;  db.transaction(function ...

  6. golang的数据类型之浮点类型

    [root@node3 shangxuetang]# cat float.go package main import "fmt" //演示golang中小数类型使用func ma ...

  7. zookeeper+dubbo+demo

    zookeeper下载地址 https://archive.apache.org/dist/zookeeper/ zookeeper安装和使用 windows环境 https://blog.csdn. ...

  8. activity知识点

    一:activity的理解 1.活动:四大应用组件之一 2.作用:提供能让用户操作并与之交互的界面 3.组件的特点: 它的类必须实现特定接口或继承特定类 需要在配置文件中配置全类名 它的对象不是通过n ...

  9. python 列表总结大全

    1定义 names=[] names=[1,2,1,1,1,] names=[1.'10'.[1,1]] 2添加元素 names.append() names.insert(0,10) names.e ...

  10. sublime text3 安装插件

    遇到的问题1: 软件中无此属性 打开Packages目录,Preferences > Browse Packages 就可以进入这个目录.$ cd Packages/$ git clone ht ...