Pendant

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1032    Accepted Submission(s): 535

Problem Description
On
Saint Valentine's Day, Alex imagined to present a special pendant to
his girl friend made by K kind of pearls. The pendant is actually a
string of pearls, and its length is defined as the number of pearls in
it. As is known to all, Alex is very rich, and he has N pearls of each
kind. Pendant can be told apart according to permutation of its pearls.
Now he wants to know how many kind of pendant can he made, with length
between 1 and N. Of course, to show his wealth, every kind of pendant
must be made of K pearls.
Output the answer taken modulo 1234567891.
 
Input
The
input consists of multiple test cases. The first line contains an
integer T indicating the number of test cases. Each case is on one line,
consisting of two integers N and K, separated by one space.
Technical Specification

1 ≤ T ≤ 10
1 ≤ N ≤ 1,000,000,000
1 ≤ K ≤ 30

 
Output
Output the answer on one line for each test case.
 
Sample Input
2
2 1
3 2
 
Sample Output
2
8
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1588 3117 2971 2256 1757
 

 
题解:
设f[i][j] 表示长度为i,用了j种珍珠的方案个数;
我们考虑加一个位置,我们可以让它是之前出现过的珍珠,也可以是没出现过的珍珠;
f[i][j] = (k-(i-1))*f[i-1][j-1] + j*f[i-1][j];
我们发现这个dp是O(nk)的,n变态的大显然炸掉;
看到n自然而然的会想到矩阵加速;
我们设一个转移矩阵是G,G[k+1][k+1], 为什么是k+1?
我们要算总的方案个数,要把所有的f[i][k]加起来,所以我们多开一维,用来转移f[i][k]的和;
G的矩阵长这样
1 0 0 0 0 1      sum    sum'  
0 1 0 0 0 1      f1     f1'
0 k-1 2 0 0 0    *     f2  ->   f2'
...          ...    ...
0 0 0 0 1 k      fk     fk'
 
就这样
 

 
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define mod 1234567891
#define N 32
int T;
ll n, k; struct Mat
{
ll a[N][N];
Mat() {memset(a, , sizeof a);}
inline void clear() {memset(a, , sizeof a);}
inline void ini() {for(int i=;i<=k;i++)a[i][i]=;}
friend Mat operator * (Mat x, Mat y)
{
Mat z;
for (register int p = ; p <= k ; p ++)
{
for (register int i = ; i <= k ; i ++)
{
for (register int j = ; j <= k ; j ++)
{
z.a[i][j] = (z.a[i][j] + x.a[i][p] * y.a[p][j]) % mod;
}
}
}
return z;
}
friend Mat operator ^ (Mat x, ll y)
{
Mat z;z.ini();
while (y)
{
if (y & ) z = z * x;
x = x * x;
y >>= ;
}
return z;
}
}G, B, C;
inline void init() {G.clear(), B.clear(), C.clear();} int main()
{
scanf("%d", &T);
while (T--)
{
init();
scanf("%lld%lld", &n, &k);
G.a[][] = , G.a[][k] = ;
G.a[][] = ;
for (register int i = ; i <= k ; i ++)
{
G.a[i][i] = i;
G.a[i][i-] = k - i + ;
}
// for (int i=0;i<=k;i++,puts(""))for(int j=0;j<=k;j++) printf("%d ",G.a[i][j]) ;
B.a[][] = k;
C = G ^ n;
C = C * B;
cout<<C.a[][]<<endl;
}
return ;
}

[HDU2294] Pendant - 矩阵加速递推的更多相关文章

  1. luogu题解 P1707 【刷题比赛】矩阵加速递推

    题目链接: https://www.luogu.org/problemnew/show/P1707 分析: 洛谷的一道原创题,对于练习矩阵加速递推非常不错. 首先我们看一下递推式: \(a[k+2]= ...

  2. bzoj2004公交线路——DP+矩阵加速递推

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 求方案数,想到DP: 因为两个站间距离<=p,所以每p个站中所有车一定都会停靠至 ...

  3. 矩阵经典题目七:Warcraft III 守望者的烦恼(矩阵加速递推)

    https://www.vijos.org/p/1067 非常easy推出递推式f[n] = f[n-1]+f[n-2]+......+f[n-k]. 构造矩阵的方法:构造一个k*k的矩阵.当中右上角 ...

  4. [模板][题解][Luogu1939]矩阵乘法加速递推(详解)

    题目传送门 题目大意:计算数列a的第n项,其中: \[a[1] = a[2] = a[3] = 1\] \[a[i] = a[i-3] + a[i - 1]\] \[(n ≤ 2 \times 10^ ...

  5. 【csp模拟赛3】bridge.cpp--矩阵加速递推

    题目描述 穿越了森林,前方有一座独木桥,连接着过往和未来(连接着上一题和下一题...). 这座桥无限长. 小 Q 在独木桥上彷徨了.他知道,他只剩下了 N 秒的时间,每一秒的时间里,他会向 左或向右移 ...

  6. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  7. CH 3401 - 石头游戏 - [矩阵快速幂加速递推]

    题目链接:传送门 描述石头游戏在一个 $n$ 行 $m$ 列 ($1 \le n,m \le 8$) 的网格上进行,每个格子对应一种操作序列,操作序列至多有 $10$ 种,分别用 $0 \sim 9$ ...

  8. POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】

    题目链接:传送门 题目大意: 求斐波那契数列第n项F(n). (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路: 用矩阵乘法加速递推. 算法竞赛进阶指南的模板: #includ ...

  9. HDU 1757 矩阵快速幂加速递推

    题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...

随机推荐

  1. 从MySQL到Hive,数据迁移就这么简单

    使用Sqoop能够极大简化MySQL数据迁移至Hive之流程,并降低Hadoop处理分析任务时的难度. 先决条件:安装并运行有Sqoop与Hive的Hadoop环境.为了加快处理速度,我们还将使用Cl ...

  2. centos7 下安装mysql5.7 数据库并使用nevicat连接数据库

    安装mysql5.7的教程: https://www.cnblogs.com/yybrhr/p/9810375.html 遇到的问题: 无法连接,到阿里云服务器安全组设置3306端口

  3. Linux环境基于CentOS7 搭建部署Docker容器

    1.Docker容器概述 区分Docker容器技术和VM虚拟机技术: evernotecid://394EFE90-9CE0-4D65-A8CD-DFEC0DC8061E/appyinxiangcom ...

  4. 01 (OC)* @property 后面可以有哪些修饰符?

    一:@property 后面可以有哪些修饰符? 1:线程安全的: atomic,nonatomic 2:访问权限的 readonly,readwrite 3:内存管理(ARC) assign, cop ...

  5. 深入理解Three.js中正交摄像机OrthographicCamera

    前言 在深入理解Three.js中透视投影照相机PerspectiveCamera那篇文章中讲解了透视投影摄像机的工作原理以及对应一些参数的解答,那篇文章中也说了会单独讲解Three.js中另一种常用 ...

  6. [Advanced Python] 16 - Google style guide for programming

    Ref: Python 风格指南 - 内容目录 这里主要记录一下值得注意的地方. Python语言规范 Lint:vsCode自带 导入:完整路径 异常 Ref: [Python] 07 - Stat ...

  7. 1,eclipse导入项目jdk版本不一样解决方案 2,java报javax.servlet.jsp cannot be resolved to a type

    一:eclipse导入项目jdk版本不一样解决方案 参考博文: https://www.cnblogs.com/chenmingjun/p/8472885.html 选中项目右键 --> Pro ...

  8. 代码审计之XSS及修复

    xss在平时的测试中,还是比较重要的,如果存在储存型xss,就可以做很多事情了,打cookie,添加管理员等等很多操作. 以下所有代码都是我自己写的,可能有不美观,代码错误等等问题,希望大家可以指正. ...

  9. 对象实例Vue

    var vm = new Vue({ el:'#app', data:{}, //数据 methods:{}, //方法调用 filters:{}, //私有过滤器 directives:{}, // ...

  10. 基于操作系统原理的Linux 的基本操作和常用命令的使用

    一.实验目的 1.学会不同Linux用户登录的方法. 2.掌握常用Linux命令的使用方法. 3.了解Linux命令中参数选项的用法和作用. 二.实验内容 1. 文件操作命令 (1) 查看文件与目录 ...