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. 

InputThe 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 
OutputOutput the answer on one line for each test case.Sample Input

2
2 1
3 2

Sample Output

2
8

题意:给定N,K。求不超过N的链子的所有染色情况,使得起使用了K种颜色。

思路:

  法1:容斥,用K种颜色,则其方案=1^K+2^K+...N^K,符号为正。 用K-1种颜色,方案=1^(K-1)+2^(K-2)...N^(K-1),符号为负...。用1种颜色...然后累加即可,但是我不会。

法2:不难得到其DP方程,dp[i][j]=dp[i-1][j-1]*(K-j+1)+dp[i-1][j]*j;然后用矩阵优化DP。在第一维加个1用来累加前缀和。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=;
int L;
struct mat
{
int M[maxn][maxn];
mat() { memset(M,,sizeof(M)); }
mat friend operator *(mat a,mat b)
{
mat res;
for(int k=;k<=L;k++)
for(int i=;i<=L;i++)
for(int j=;j<=L;j++)
res.M[i][j]=(res.M[i][j]+(ll)a.M[i][k]*b.M[k][j]%Mod)%Mod;
return res;
}
mat friend operator ^(mat a,ll x)
{
mat res; rep(i,,L) res.M[i][i]=;
while(x){
if(x&1LL) res=res*a; a=a*a; x/=;
} return res;
}
}; int main()
{
int T,N,K;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&K); L=K;
mat base,a;
a.M[][]=K; //单独考虑,因为base的第0行是累加dp的,不能再用了。
base.M[][]=; base.M[][K]=;
rep(i,,K){
if(i!=)//这里单独考虑了。
base.M[i][i-]=K-i+;
base.M[i][i]=i;
}
a=(base^(N))*a;
printf("%d\n",a.M[][]);
}
return ;
}

HDU - 2294: Pendant(矩阵优化DP&前缀和)的更多相关文章

  1. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  2. 矩阵优化dp

    链接:https://www.luogu.org/problemnew/show/P1939 题解: 矩阵优化dp模板题 搞清楚矩阵是怎么乘的构造一下矩阵就很简单了 代码: #include < ...

  3. bzoj 3120 矩阵优化DP

    我的第一道需要程序建矩阵的矩阵优化DP. 题目可以将不同的p分开处理. 对于p==0 || p==1 直接是0或1 对于p>1,就要DP了.这里以p==3为例: 设dp[i][s1][s2][r ...

  4. [六省联考2017]组合数问题 (矩阵优化$dp$)

    题目链接 Solution 矩阵优化 \(dp\). 题中给出的式子的意思就是: 求 nk 个物品中选出 mod k 为 r 的个数的物品的方案数. 考虑朴素 \(dp\) ,定义状态 \(f[i][ ...

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

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

  6. [Sdoi2017]序列计数 矩阵优化dp

    题目 https://www.lydsy.com/JudgeOnline/problem.php?id=4818 思路 先考虑没有质数限制 dp是在同余系下的,所以\(f[i][j]\)表示前i个点, ...

  7. bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...

  8. 矩阵优化DP类问题应用向小结

    前言 本篇强调应用,矩阵的基本知识有所省略(也许会写篇基础向...). 思想及原理 为什么Oier们能够想到用矩阵来加速DP呢?做了一些DP题之后,我们会发现,有时候DP两两状态之间的转移是定向的,也 ...

  9. 洛谷P3193 GT考试 kmp+矩阵优化dp

    题意 求\(N\)位数字序列(可以有前导0)中不出现某\(M\)位子串的个数,模\(K\). \(N<=10^9,M<=20,K<=1000\) 分析 设\(dp[i][j]\)表示 ...

随机推荐

  1. 深度学习1--ubuntu14.04+win10双系统

    相当多的内容参考的百度经验https://jingyan.baidu.com/article/eb9f7b6d79a7b4869364e885.html?qq-pf-to=pcqq.group 还可以 ...

  2. 流量分析系统---flume(测试flume+kafka)

    1.在flume官方网站下载最新的flume     wget http://124.205.69.169/files/A1540000011ED5DB/mirror.bit.edu.cn/apach ...

  3. iOS 视图控制器转场动画/页面切换效果/跳转动画 学习

    一 学习 在 UINavigationController 中 push 和 pop 的转场效果  (基于iOS7 以上的转场方式) 经过学习了解到,重点分三块: (1)pushAnimation:  ...

  4. $MySQL常用操作命令备忘

    1.登录数据库:mysql -uroot -p12345  (12345为密码)2.创建数据库:create database senguocc; (senguocc为数据库名)3.查看有哪些数据库: ...

  5. SpringBoot Redis工具类封装

    1.接口类 package com.sze.redis.util; import java.util.List; import java.util.Set; import java.util.conc ...

  6. 添加code到github上

    第一步:github上新建远程仓库 1. 在 https://github.com/  注册账号 2. new 一个新仓库 (1) 点击加号下的`New repository` (2)在Reposit ...

  7. 前端之CSS样式

    一.CSS 1.什么是CSS 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的 ...

  8. P3437 [POI2006]TET-Tetris 3D

    题目 P3437 [POI2006]TET-Tetris 3D 做法 一眼就是二维线段树,仔细想想,赋值操作怎么办??\(lazy\)标记放在一维,下一次又来放个标记二维就冲突了 正解:永久化标记 怎 ...

  9. mini2440移植uboot 2014.04(六)

    上一篇博文:<mini2440移植uboot 2014.04(五)> 代码已经上传到github上:https://github.com/qiaoyuguo/u-boot-2014.04- ...

  10. Centos6.5使用yum安装软件的时候 Another app is currently holding the yum lock; waiting for it to exit...

    Loaded plugins: fastestmirror, refresh-packagekit, security Existing . Another app is currently hold ...