HDU2294 Pendant (齐次线性方程的矩阵解法 + 矩阵快速幂)

Pendant

Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 115 Accepted Submission(s): 70
 
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
 

题意 : 有K种珍珠,每种 N 个,做一条项链,每种珍珠都要用上。 问 1 到 N的长度能组成几种。
首先想dp,很明显可以想到: 
dp[i][j] = (k-(j-1))*dp[i-1][j-1] + j*dp[i-1][j] (dp[i][j]表示长度为i的并且有j种珍珠的垂饰有多少个)
g[n] = g[n-1] + dp[n][K],    g[n] 为最后求解的答案(表示  1 到 n 的 长度可以组成几种
但是 数据量太大 1e9 数组开不了那么大,观察这个式子是一个齐次线性方程,可以通过矩阵来求解。

设第i阶段的一维数组(dp[i][0~j])状态设为F,转移矩阵为A(k+1阶方阵) 则有:A * F = F';(F'为新状态) 
设答案 = gn = dp[1][k] + dp[2][k] + ... + dp[n][k] 所以有矩阵:

| 1 0...............0 1 |       |g0|        |g1'| 
| 0 1 0...............0 |       |f1|        |f1'| 
| 0 k-1 2.............0 |       |f2|        |f2'| 
| ..................... |   *   |..|    =   |..'| 
| 0...0 k-(j-1) j 0...0 |       |fj|        |fj'| 
| ..................... |       |..|        |..'| 
| 0...............0 1 k |       |fk|        |fk'| 

这个代码的初始化:[g0, f1, f2, ..., fk] = {0, k, 0, ..., 0}

g[n] = A^n *  [g0, f1, f2, ..., fk];
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <string.h>
#include <typeinfo>
using namespace std;
const int mod = 1234567891;
int n, k;
struct Matrix {
    long long a[35][35];
};

Matrix operator * (const Matrix& a, const Matrix& b) {
    Matrix c;
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            c.a[i][j] = 0;
            for (int k = 0; k <=n; ++k) {
                c.a[i][j] += (a.a[i][k] * b.a[k][j]) % mod;
            }
            c.a[i][j] %= mod;
        }
    }
    return c;
}

Matrix operator ^ (Matrix& a, int k) {
    Matrix c;
    for (int i = 0; i <= n; ++i)
        for (int j = 0; j <= n; ++j) {
            if (i == j)
                c.a[i][j] = 1;
            else
                c.a[i][j] = 0;
        }
    while (k) {
        if (k&1)
            c = a * c;
        k >>= 1;
        a = a * a;
    }
    return c;
}
Matrix aa, cc;
void init() {
    memset(aa.a, 0, sizeof(aa.a));
    memset(cc.a, 0, sizeof(cc.a));
    aa.a[0][0] = aa.a[0][n] = 1;
    for (int i = 1; i <= n; ++i) {
        if (i > 1)
            aa.a[i][i-1] = n - i + 1;
        aa.a[i][i] = i;
    }
    cc.a[1][0] = n;
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        cin >> k >> n;
        init();
        aa = aa ^ k;
        cc = aa * cc;
        cout << cc.a[0][0] << endl;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值