Pendant
Pendant |
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 104 Accepted Submission(s): 66 |
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 |
Output
Output the answer on one line for each test case.
|
Sample Input
2 |
Sample Output
2 |
Source
The 4th Baidu Cup final
|
Recommend
lcy
|
/*
题意:有k种珍珠,每种n个,现在问你能组成多少长度为1~n的珍珠项链,要求每条项链必须有k中珍珠 初步思路:......没思路算是思路么? #补充:看了一下题解,dp搞,dp[i][j]表示长度为i,用j种珍珠的方案,状态转移方程为:
dp[i][j]=dp[i-1][j]*j+dp[i-1][j-1]*(k-j+1); 长度为i 用j种珍珠的方案,用长度为i-1 用j种
珍珠的项链再从j种珍珠中再拿一个组成长度为i,用j种珍珠的项链,在加上长度为i,用j-1种珍珠
的项链再选另外的k-(j-1)种珍珠组成长度为i,用j种珍珠的项链。
答案为,dp[1][k]+dp[2][k]+...+dp[n][k] 写的时候是不容易实现的,因为dp数组开不到3e10,所以只能用矩阵来求递推公式。
*/
#include<bits/stdc++.h>
#define mod 1234567891
#define ll long long
using namespace std;
/********************************矩阵模板**********************************/
class Matrix {
public:
ll a[][];
int n; void init(int x) {
memset(a,,sizeof(a));
if (x)
for (int i = ; i < ; i++)
a[i][i] = ;
} Matrix operator +(Matrix b) {
Matrix c;
c.n = n;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
return c;
} Matrix operator +(int x) {
Matrix c = *this;
for (int i = ; i < n; i++)
c.a[i][i] += x;
return c;
} Matrix operator *(Matrix b)
{
Matrix p;
p.n = b.n;
p.init();
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
for (int k = ; k < n; k++)
p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
return p;
} Matrix power(ll t) {
Matrix ans,p = *this;
ans.n = p.n;
ans.init();
while (t) {
if (t & )
ans=ans*p;
p = p*p;
t >>= ;
}
return ans;
}
};
Matrix unit;
int t;
ll n;
int k;
/********************************矩阵模板**********************************/
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d%lld",&n,&k);
k++;
unit.init();
unit.n=k;
for(int i=;i<k-;i++){
unit.a[i][i]=i+;
unit.a[i][i+]=k-i-;
}
unit.a[k-][k-]=unit.a[k-][k-]=;
unit=unit.power(n);
// for(int i=0;i<k;i++){
// for(int j=0;j<k;j++){
// cout<<unit.a[i][j]<<" ";
// }
// cout<<endl;
// }
printf("%lld\n",(k-)*unit.a[][k-]%mod);
}
return ;
}
Pendant的更多相关文章
- HDU - 2294: Pendant(矩阵优化DP&前缀和)
On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...
- HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)
Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend ...
- [HDU2294] Pendant - 矩阵加速递推
Pendant Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- [HDU2294]Pendant
题目:Pendant 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2294 分析: 1)f[i][j]表示长度为i,有j种珍珠的吊坠的数目. $f[i][ ...
- hdu2294:Pendant
T<=10组数据问K<=30种珠子每种n<=1e9串成1~n长度的序列共有多少种,mod1234567891. 方程没想到.矩阵不会推.很好. f[i][j]--长度i,j种珠子方案 ...
- 2016.03.31,英语,《Vocabulary Builder》Unit 08
tend/tent: from the Latin tendere, meaning 'to stretch, extend, or spread'. tent: [tent] n. 帐篷 vt.&a ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 无废话网页重构系列——(6)HTML主干结构:站点(site)、页面(page)
本文作者:大象本文地址:http://www.cnblogs.com/daxiang/p/4653546.html 在分析和切出设计稿,以及部署项目目录文件后,开始写HTML Demo. 首先,弄出H ...
- OpenCV码源笔记——Decision Tree决策树
来自OpenCV2.3.1 sample/c/mushroom.cpp 1.首先读入agaricus-lepiota.data的训练样本. 样本中第一项是e或p代表有毒或无毒的标志位:其他是特征,可以 ...
随机推荐
- oracle pl/sql 基础
一.pl/sql developer开发工具pl/sql developer是用于开发pl/sql块的集成开发环境(ide),它是一个独立的产品,而不是oracle的一个附带品. 二.pl/sql介绍 ...
- C++代码使用 CppUnit 进行单元检测
CppUnit是一个很方便的对C++代码进行单元检测的工具. 如何编译CppUnit参照博客:http://blog.csdn.net/x_iya/article/details/8433716 该博 ...
- C++移动构造函数以及move语句简单介绍
C++移动构造函数以及move语句简单介绍 首先看一个小例子: #include <iostream> #include <cstring> #include <cstd ...
- django之快速分页
本文介绍djanog两种分页,第一是普通分页,第二是使用haystack全文检索的分页. 1.django自带分页功能,这个功能非常好用.基本知识点:Django提供了数据分页的类,这些类被定义在dj ...
- 1289 大鱼吃小鱼 1305 Pairwise Sum and Divide 1344 走格子 1347 旋转字符串 1381 硬币游戏
1289 大鱼吃小鱼 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右 ...
- Ionic3学习笔记(三)禁止横屏
本文为原创文章,转载请标明出处 目录 安装 使用 参数 1. 安装 命令行输入 ionic cordova plugin add cordova-plugin-screen-orientation n ...
- 网时|ipone8爆冷,我的服务器空欢喜一场
上周ipone可谓是占尽了风头,从face ID到全面屏,从人脸识别到逆天价格,小编都能预想到上市之后的一片火热了,苹果后台的服务器恐怕都早已做好了准备,加大带宽,稳定运行,确保万无一失. 9月22日 ...
- Linux-jdk1.7-tomcat7 简易安装
一.jdk 安装 安装包:jdk-7u80-linux-x64.tar.gz 2 解压 [root@localhost package]# tar -zxvf jdk-7u80-linux-x64.t ...
- Hello World -- 第一篇博客
今年注定是不寻常的一年,因为技术,接触了许多大牛.通过一篇篇博文,看到了大牛们勤奋好学.孜孜不倦的精神,于是决定也开个博客,向大牛学习. 博客开了,写点什么呢?奈何肚子里墨水不多,吐出来也多是白沫,不 ...
- win10 uwp 截图 获取屏幕显示界面保存图片
本文主要讲如何保存我们的屏幕显示的,保存为图片,也就是截图,截我们应用显示的. UWP有一个功能,可以截图,RenderTargetBitmap 我们首先写一个Grid,我们需要给他名字,我这里给他S ...