hdu 2294 Pendant (dp+矩阵快速幂)
2015-04-21 11:02
218 查看
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.
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
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比较小,所以可以利用矩阵进行加速
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define MAX 37 #define MOD 1234567891 using 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] ); } }
相关文章推荐
- HDU 2294 Pendant (DP+矩阵快速幂降维)
- HDOJ 2294 - Pendant(DP+矩阵快速幂)
- HDU 2294 Pendant
- hdu 2294 Pendant
- HDU 2294 Pendant(DP+矩阵)
- HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)
- HDU 2294 Pendant
- hdu 2294 Pendant(矩阵乘法)
- HDU 2294 Pendant
- hdu 2294 pendant
- hdu 2294 Pendant (动态规划+矩阵) 解题报告
- HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)
- HDU 5607 graph(概率DP+矩阵快速幂)
- HDOJ 2294 - Pendant
- HDU 5318 (dp+矩阵快速幂优化)
- HDU 2294【DP+快速幂】
- hdu 2294
- HDU 6185 Covering(dp+矩阵快速幂)
- hdu 2294 Pendant dp+矩阵
- Hdu 6030 Happy Necklace【Dp+矩阵快速幂】