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.
 

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
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比较小,所以可以利用矩阵进行加速
 
#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
原创粉丝点击