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代表有毒或无毒的标志位:其他是特征,可以 ...
随机推荐
- FastDFS安装全过程记录(V5.05)
FastDFS安装全过程记录 1.安装准备 HA虚拟IP:192.168.1.208 HA软件:Keepalived 操作系统:CentOS 7 用户:root 数据目录:/data/fastdfs ...
- python基础之五大标准数据类型
学习一门语言,往往都是从Hello World开始. 但是笔者认为,在一个黑框框中输出一个"你好,世界"并没有什么了不起,要看透事物的本质,熟悉一门语言,就要了解其底层,就是我们常 ...
- Hive任务优化(2)
JOIN优化 1.大多数情况下,Hive会对每对Join连接对象启动一个MapReduce任务. 2.多表关联时,如果每个ON子句都使用相同的连接键的话,那么只会产生一个MapReduce Job. ...
- HDU 1754 I Hate It(线段树区间求最值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- HTML5基础知识及相关笔记
HTML5基础 1.1HTML文件的基本结构和W3C标准 1.1.1HTML简介 HTML是一种描述网页的语言,一种超文本标记的语言! 1.1.2HTML文件的基本结构 头部(head) 头部是网页的 ...
- docker镜像文件导入与导出
工作中经常需要拉取一些国外的镜像,但是网络限制等原因在公司拉取很慢,所以我习惯用亚马逊服务器拉取镜像,导出后下载到本地再导入开发环境 1. 查看镜像id sudo docker images REPO ...
- PHP合并两张图片(水印)
$dst_im = "http://img6.cache.netease.com/photo/0001/2016-04-15/BKMTUO8900AP0001.jpg"; $src ...
- asp.net微软图表控件使用示例
<configuration> <system.webServer> <handlers> <remove name="ChartImageHand ...
- IBatis.Net 老技术新研究
我们现在用的数据访问组件封装了IBatis.Net框架,提供了标准的数据访问接口和服务.正好总结一下老技术IBatis.Net在进行实际的数据访问开发之前,我们先了解一下:IBatis.Net中几个重 ...
- DevOps之主机
唠叨话 关于德语噢屁事的知识点,仅提供专业性的精华汇总,具体知识点细节,参考教程网址,如需帮助,请留言. 主机(Host) 关于主机,知识与技能的层次(知道.理解.运用),理论与实践的方面(原理.技术 ...