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 ;
}

最新文章

  1. BZOJ 1984: 月下“毛景树” [树链剖分 边权]
  2. jQuery代码节选(事件)
  3. 教你一招:Win10系统如何正确卸载edge浏览器?
  4. [DB那些事]数据库加密
  5. Odoo report
  6. 多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量
  7. Android ListView内容变化后的动态刷新
  8. openssl生成RSA格式,并转为pkcs8格式
  9. shell中的declare命令
  10. js记录用户行为浏览记录和停留时间(转)
  11. [转载]CodeBlocks+wxWidgets
  12. 基于Visual C++2013拆解世界五百强面试题--题16-进制分析
  13. virtual 关键字
  14. git 配置多个SSH-Key(转)
  15. NHibernate 数据查询之QueryOver&lt;T&gt;
  16. iOS-Core-Animation-Advanced-Techniques(一)
  17. applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.
  18. 微信小程序开发之模板
  19. JS学习笔记Day21
  20. 服务器返回:type&quot;:&quot;Buffer&quot;,&quot;data&quot;:

热门文章

  1. java使用FileSystem上传文件到hadoop文件系统
  2. 为什么StringBuilder是线程不安全的?StringBuffer是线程安全的?
  3. Linux之vim、压缩与解压缩
  4. RabbitMQ的六种工作模式总结
  5. MybatisPlus报错Invalid bound statement (not found)的解决方案
  6. django开发后台接口error 10053/10054
  7. select2 分组后的选项无法被选中
  8. 1,eclipse导入项目jdk版本不一样解决方案 2,java报javax.servlet.jsp cannot be resolved to a type
  9. JavaScript学习记录
  10. Spring Boot 2.x基础教程:构建RESTful API与单元测试