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. android 对话框中的进度条 (ProgressDialog)

    from:http://byandby.iteye.com/blog/817214 显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里 ...

  2. 推荐一款轻量级PHP数据库框架–Medoo

    引用官网的简介: 可以加快开发速度的最轻量级的PHP数据库框架 为什么选择Medoo及其主要功能: 轻量级–单个文件,只有20KB 易用–非常容易学习和使用 功能强大–支持各种常见和复杂的SQL查询 ...

  3. $《利用Python进行数据分析》学习笔记系列——IPython

    本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...

  4. usb2.0 协议分析

    转:https://blog.csdn.net/u011594613/article/details/48291307 一.USB硬件介绍1.1.概述 一条USB传输线分别由地线.电源线.D+和D-四 ...

  5. 获取CPU利用率

    #define MB (1024 * 1024) MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); GlobalMemoryStatu ...

  6. INSPIRED启示录 读书笔记 - 第34章 恐惧、贪婪、欲望

    消费者购买产品大多源于情感需求 企业级消费者出于恐惧和贪婪购买产品:如果不买这款产品,竞争对手会超过我,黑客会攻破我的防火墙,客户将弃我而去:如果买了,会赚得更多,省得更多 大众消费者购买产品的原因更 ...

  7. 为多个文件夹下的C源代码编写Makefile文件

    上一篇文章写了如何为在同一个文件夹下的C源代码,本篇文章为多个文件夹下的C源代码编写Makefile文件. 建立两个文件夹,分别为abs与src.其最终目录结构如下: 1 $ ls * 2 jun.c ...

  8. Kafaka高可用集群环境搭建

    zk集群环境搭建:https://www.cnblogs.com/toov5/p/9897868.html 三台主机每台的Java版本1.8 下面kafka集群的搭建:  3台虚拟机均进行以下操作:  ...

  9. Python根据内嵌的数字将字符串排序(sort by numbers embedded in strings)

    import re  re_digits = re.compile(r'(\d+)')  def embedded_numbers(s):       pieces = re_digits.split ...

  10. EF Code-First 学习之旅 Code First Conventions

    协定是一系列的默认规则用来自动配置领域中的概念模型 1:类型发现 Code-First对包含DBSet属性的类型创建表(包括这些类型的所有引用类型) public class Student { pu ...