COJ 2124 Day8-例1

Day8-例1
难度级别:B; 运行时间限制:1000ms; 运行空间限制:256000KB; 代码长度限制:2000000B
试题描述
给定n、m的值,求

的值。

答案对10^9+7取模。

输入
一行,两个整数n、m。
输出
一行,一个整数,表示答案mod 10^9+7的值。
输入示例
5 3
输出示例
36363
其他说明
n<=10^9 m<=50

题解:矩阵乘法+变形+二项式定理:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<stack>
 6 #include<queue>
 7 #include<cstring>
 8 #define PAU putchar(' ')
 9 #define ENT putchar('\n')
10 using namespace std;
11 typedef long long LL;
12 const LL mod=1000000007;
13 const int maxn=50+5;
14 struct matx{
15     int h,w;LL x[maxn][maxn];matx(){memset(x,0,sizeof(x));}
16     matx operator*(matx a){
17         matx b;b.h=h;b.w=a.w;
18         for(int k=0;k<w;k++)
19             for(int i=0;i<h;i++)
20                 for(int j=0;j<a.w;j++)b.x[i][j]=(b.x[i][j]+x[i][k]*a.x[k][j])%mod;return b;
21     }
22     void init(int h,int w){this->h=h;this->w=w;return;}
23 }A,B;
24 LL C[maxn][maxn];int n,m;
25 LL powi(LL x,LL y){
26     LL ans=1;x%=mod;for(int i=y;i;i>>=1,x=x*x%mod)if(i&1)ans=ans*x%mod;return ans;
27 }
28 matx powm(matx a,int n){
29     matx ans=a,tmp=a;n--;for(int i=n;i;i>>=1,tmp=tmp*tmp)if(i&1)ans=ans*tmp;return ans;
30 }
31 LL inv(LL a){return powi(a,mod-2);}
32 void initc(){
33     for(int i=0;i<=m;i++)C[i][0]=1,C[i][i]=1;
34     for(int i=1;i<=m;i++)for(int j=1;j<i;j++)C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;return;
35 }
36 void initmatx(){
37     A.init(1,m+2);B.init(m+2,m+2);A.x[0][0]=1;
38     for(int i=0;i<=m;i++)for(int j=0;j<=i;j++)B.x[j][i]=C[i][j];
39     for(int i=0;i<=m;i++)B.x[i][m+1]=C[m][i];
40     B.x[m+1][m+1]=inv(m);return;
41 }
42 inline int read(){
43     int x=0,sig=1;char ch=getchar();
44     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
45     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
46     return sig?x:-x;
47 }
48 inline void write(long long x){
49     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
50     int len=0;long long buf[20];while(x)buf[len++]=x%10,x/=10;
51     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
52 }
53 int main(){
54     n=read();m=read();initc();initmatx();A=powm(B,n);
55     write((A.x[0][m+1]*powi(m,n))%mod);
56     return 0;
57 }

 

posted @ 2015-08-12 15:27  AI_Believer  阅读(130)  评论(0编辑  收藏  举报