题目: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. Jenkins+Git+Gitlab+Ansible实现持续集成自动化部署静态网站

    环境准备 三台主机: 一台主机部署jenkins和作为ansible的管理主机 一台主机部署gitlab和ansible的节点1 一台主机为ansible的节点2 防火墙和apache服务关闭 第一步 ...

  2. Centos7.2命令安装图形化界面

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/liang_operations/arti ...

  3. oracle两表中的两列进行模糊匹配的方法

    SELECT T2.列名,T1.列名  FROM 主表 T1, 匹配表 T2    WHERE  T1.匹配列  LIKE CONCAT('%',concat(T2.匹配列,'%')); 注意:  a ...

  4. Git006--管理修改

    Git--管理修改 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...

  5. JavaScript实现的发布/订阅(Pub/Sub)模式

    JavaScript实现的发布/订阅(Pub/Sub)模式 时间 2016-05-02 18:47:58  GiantMing's blog 原文  http://giantming.net/java ...

  6. Spring Security 02

    权限管理 配置不过滤的资源 方法1 <http pattern="/login.jsp" security="none"></http> ...

  7. android概念-android学习第二天

    一:1G到4G(generation) 1G 大哥大 -- 电话 2G 小灵通 gsm标准 发短信 wap.baidu.com -- 电话和短信 3G 沃 www.baidu.com 7.2M/s - ...

  8. python——datetime模块

    一.datetime模块介绍 (一).datetime模块中包含如下类: 类名 功能说明 date 日期对象,常用的属性有year, month, day time 时间对象 datetime 日期时 ...

  9. python学习第二天标准输入输出和注释用法

    任何编程语言都有输入输出和用打交道,python也不例外,输入input(),输出print() 玖乐网络(http://www.96net.com.cn/)分享自己的心得 1,input()用法实例 ...

  10. NodeJs的CommonJS模块规范

    前言 本人记忆力一般,为了让自己理解<深入浅出Node.js-朴灵>一书,会在博客里记录一些关键知识,以后忘了也可以在这里找到,快速回想起来 Node通过require.exports.m ...