hdu 2294 Pendant (dp+矩阵快速幂)
来源:互联网 发布:电脑连不上网多重网络 编辑:程序博客网 时间:2024/05/29 02:23
Pendant
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 673 Accepted Submission(s): 339
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.
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
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
22 13 2
Sample Output
28
Source
The 4th Baidu Cup final
题目分析:
首先考虑小数据情况下的动态规划做法
dp[i][j]表示到第i个,已经选了j种的情况,容易推出
dp[i][j] = dp[i-1][j] * j + dp[i-1][j-1]*k-j+1;
发现n过于大,单考虑到k比较小,所以可以利用矩阵进行加速
首先考虑小数据情况下的动态规划做法
dp[i][j]表示到第i个,已经选了j种的情况,容易推出
dp[i][j] = dp[i-1][j] * j + dp[i-1][j-1]*k-j+1;
发现n过于大,单考虑到k比较小,所以可以利用矩阵进行加速
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define MAX 37#define MOD 1234567891using namespace std;typedef long long LL;int t;LL n,k;struct Matrix { LL a[MAX][MAX]; Matrix ( ) { memset ( a , 0 , sizeof ( a ) ); }};Matrix multi ( Matrix m1 , Matrix m2 ){ Matrix ret; for ( int i = 1 ; i <= k ; i++ ) for ( int j = 1 ; j <= k ; j++ ) if( m1.a[i][j] ) for ( int t = 1 ; t <= k ; t++ ) ret.a[i][t] = ( ret.a[i][t] + m1.a[i][j]*m2.a[j][t]%MOD)%MOD; return ret; }Matrix quick ( Matrix m , int n ){ Matrix ret; for ( int i = 1 ; i <= k ; i++ ) ret.a[i][i] = 1; while ( n ) { if ( n&1 ) ret = multi ( ret , m ); m = multi ( m , m ); n >>= 1; } return ret;}void print ( Matrix m ){ for ( int i = 1 ; i <= k ; i++ ) { for ( int j = 1 ; j <= k ; j++ ) printf ( "%lld " , m.a[i][j] ); puts (""); }}int main ( ){ scanf ( "%d" , &t ); while ( t-- ) { scanf ( "%lld%lld" , &n , &k ); Matrix ans; k+=2; ans.a[1][1] = 0 , ans.a[1][2] = 1; Matrix vary; vary.a[k][1] = 1; vary.a[1][1] = 1; for ( int i = 3 ; i <= k ; i++ ) { vary.a[i-1][i] = k-i+1; vary.a[i][i] = i-2; } //print ( vary ); //cout <<"---------------------------" << endl; vary = quick ( vary , n+1 ); //print ( vary ); ans = multi ( ans , vary ); printf ( "%lld\n" , ans.a[1][1] ); }}
0 0
- HDU 2294 Pendant DP+矩阵快速幂
- hdu 2294 Pendant (dp+矩阵快速幂)
- HDOJ 2294 - Pendant(DP+矩阵快速幂)
- hdu 2294 Pendant dp+矩阵
- HDU 2294 Pendant (DP+矩阵快速幂降维)
- HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)
- hdu 2294 Pendant DP+矩阵优化
- hdu 2294 Pendant(矩阵乘法)
- Aoj 92 Pendant (DP+矩阵)
- 【HDU2294】Pendant-DP矩阵优化
- hdu 2294 Pendant (动态规划+矩阵) 解题报告
- HDU 2294 Pendant
- hdu 2294 pendant
- hdu 2294 Pendant
- HDU 2294 Pendant
- HDU 2604 Queuing DP + 矩阵快速幂
- HDU 5318 (dp+矩阵快速幂优化)
- hdu 5564 快速矩阵幂+数位dp
- 使用时间插件时出现的问题
- MongoDB集群上副本集(replica set)建立
- virtio驱动学习笔记
- Gson的使用
- Mongodb 问题汇总
- hdu 2294 Pendant (dp+矩阵快速幂)
- 2105阿里c c++研发面试(一面跪)
- Eclipse去掉注释
- 2013腾讯面经(技术类-后台开发)
- Servlet简单登陆验证实例
- 一键查询-查看联系人资料
- UVA 10494 解题报告
- java开发过程中如何使用API?
- JSON小结【json-lib】