Pendant |
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 58 Accepted Submission(s): 41 |
|
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 |
思路:此题好题!!!先列出DP方程,再用矩阵加速,网上有很多资料
改天把我的矩阵(照片)发上来~~
1 #include <cmath> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <string> 6 #include <cstdlib> 7 using namespace std; 8 9 typedef long long ll; 10 typedef double dd; 11 const int maxn=35;//change!! 12 const ll mod=1234567891; 13 struct matrix 14 { 15 ll m[maxn][maxn]; 16 } c,base,ans,a; 17 int n,k,T; 18 ll r; 19 20 void close() 21 { 22 exit(0); 23 } 24 25 void print(matrix a) 26 { 27 for (int i=1;i<=k+1;i++) 28 { 29 for (int j=1;j<=k+1;j++) 30 printf("%I64d ",a.m[i][j]); 31 printf(" "); 32 } 33 } 34 35 matrix mul(matrix a,matrix b) 36 { 37 memset(c.m,0,sizeof(c.m)); 38 for (int i=1;i<=k+1;i++) 39 for (int j=1;j<=k+1;j++) 40 for (int l=1;l<=k+1;l++) 41 { 42 c.m[i][j]+=(a.m[i][l]*b.m[l][j]); 43 c.m[i][j] %= mod; 44 } 45 return c; 46 } 47 48 void work() 49 { 50 memset(base.m,0,sizeof(base.m)); 51 memset(ans.m,0,sizeof(ans.m)); 52 for (int i=1;i<=k+1;i++) 53 ans.m[i][i]=1; 54 for (int i=1;i<=k;i++) 55 { 56 base.m[i][i]=i; 57 base.m[i][i-1]=k-(i-1); 58 } 59 base.m[k+1][k+1]=base.m[k+1][k]=1; 60 //n++; 61 while (n!=0) 62 { 63 if (n & 1) 64 { 65 ans=mul(base,ans); 66 } 67 n/=2; 68 base=mul(base,base); 69 } 70 r=(k * ans.m[k+1][1]) % mod; 71 printf("%I64d ",r); 72 } 73 74 void init() 75 { 76 scanf("%d",&T); 77 while (T--) 78 { 79 scanf("%d %d",&n,&k); 80 work(); 81 } 82 } 83 84 int main () 85 { 86 init(); 87 close(); 88 return 0; 89 }