hdu 2294 Pendant (dp+矩阵快速幂)

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
题目分析:
首先考虑小数据情况下的动态规划做法
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] );
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值