「hdu2294」Pendant
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
2 2 1 3 2
Sample Output
2 8
题解
* [题意]
* 有k种珍珠,每种珍珠N个,问长度<=N且有k种珍珠的垂饰有多少个?
* [解题方法]
* dp[i][j]表示长度为i的并且有j种珍珠的垂饰有多少个
* 则有状态转移:dp[i][j] = (k-(j-1))*dp[i-1][j-1] + j*dp[i-1][j];
* 由于N太大,所以把i看成“阶段”,构造矩阵,通过矩阵快速转移
* 设第i阶段的一维数组(dp[i][0~j])状态设为F,转移矩阵为init(k+1阶方阵)
* 则有:init * 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}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include<iostream> #include<cstdio> #include<cstring> #define mod 1234567891 #define ll long long using namespace std; int T,n;ll m; ll c[31],a[31][31],b[31][31]; void mul(ll a[31][31],ll b[31][31],ll s[31][31]) { ll t[31][31]; for(int i=0;i<=m;i++) for(int j=0;j<=m;j++) { t[i][j]=0; for(int k=0;k<=m;k++) t[i][j]=(t[i][j]+a[i][k]*b[k][j])%mod; } for(int i=0;i<=m;i++) for(int j=0;j<=m;j++) s[i][j]=t[i][j]; } void mul2(ll a[31][31],ll b[31],ll s[31]) { ll t[31]; for(int i=0;i<=m;i++) { t[i]=0; for(int k=0;k<=m;k++) t[i]=(t[i]+a[i][k]*b[k])%mod; } for(int i=0;i<=m;i++)s[i]=t[i]; } int main() { scanf("%d",&T); while(T--) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); scanf("%d%lld",&n,&m); a[0][0]=a[0][m]=1; for(int i=1;i<=m;i++) { a[i][i]=i; if(i>1)a[i][i-1]=m-i+1; } for(int i=0;i<=m;i++)b[i][i]=1; while(n) { if(n&1)mul(a,b,b); n>>=1; mul(a,a,a); } c[1]=m; mul2(b,c,c); printf("%d\n",c[0]); } return 0; } |
Subscribe