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. Dagger2 探索记1——四大基本组件(一)

    喝很多自主学习的人,我接触Dagger 2 框架的原因是刚进公司的时候导师给安排的学习任务,学习方式是组内培训. 听到这个消息的我,以为是部门的人轮流给我讲课. 后来导师跟我说,组内培训的意思是,我先 ...

  2. 网关高可用之keepavlived全流程(安装/配置/验证/解析)

    1.场景描述 因为要做网关的高可用,用到了keepalived+nginx,来保证nginx的高可用.(微服务时代之网关及注册中心高可用架构设计),如下图: 安装了keepavlived,走了一些弯路 ...

  3. Python3 爬虫之 Scrapy 框架安装配置(一)

    博客地址:http://www.moonxy.com 基于 Python 3.6.2 的 Scrapy 爬虫框架使用,Scrapy 的爬虫实现过程请参照本人的另一篇博客:Python3 爬虫之 Scr ...

  4. [C++]类的设计(2)——拷贝控制(析构和三五法则)

    1.析构函数:释放对象使用的资源,并销毁对象的非static数据成员:析构函数不接受参数,因此不能被重载.对于一个给定类,有且只有一个析构函数.   2.析构函数的组成:一个函数体+一个析构部分(im ...

  5. Linux 修改 ls 时间显示格式

    [root@CentosTe tmp]# export TIME_STYLE='+%Y-%m-%d %H:%M:%S'[root@CentosTe tmp]# ll总计 24-rwx------ 1  ...

  6. RestClient火狐接口测试

    一.RestClient的简单介绍 RESTClient是一款用于测试各种Web服务的插件,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应.二.RESTClient的 ...

  7. SPN扫描

    0x01介绍 Kerberos是一种支持票证身份验证的安全协议.如果客户端计算机身份验证请求包含有效的用户凭据和服务主体名称 (SPN),则 Kerberos 身份验证服务器将授予一个票证以响应该请求 ...

  8. FileDetail

    import org.apache.hadoop.conf.*; import org.apache.hadoop.fs.*; import java.io.IOException; import j ...

  9. 自定义TabLayout的Indicator

    最近项目要使用类似TabLayout的控件,其实我感觉就是TabLayout只是换了一个Indicator,先说一说TabLayout这是Android Support Design的控件要使用的同学 ...

  10. mysql 查找数组格式的字符串中是否包含某个值

    --#{type}表示将判断的单个值 例如 -- arr表示数组格式的字符串,例如 ,,, FIND_IN_SET(#{type},arr) 使用的时候,举例: