day8-Example 1 |
Difficulty level: B; run time limit: 1000ms; operating space limit: 256000KB; code length limit: 2000000B |
Question Description |
Given the value of N, M, ask The value. The answer to the 10^9+7 modulo. |
Input |
One line, two integers n, M. |
Output |
A line, an integer, that represents the value of the answer mod 10^9+7. |
Input example |
5 3 |
Output example |
36363 |
Other Notes |
N<=10^9 m<=50 |
Key: Matrix multiplication + deformation + two-term theorem:
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <stack>6#include <queue>7#include <cstring>8 #definePAU Putchar (")9 #defineENT Putchar (' \ n ')Ten using namespacestd; OnetypedefLong LongLL; A ConstLL mod=1000000007; - Const intmaxn= -+5; - structmatx{ the intH,w; LL X[maxn][maxn];matx () {memset (x,0,sizeof(x));} -mATXoperator*(mATX a) { -mATX b;b.h=h;b.w=A.W; - for(intk=0; k<w;k++) + for(intI=0; i) - for(intj=0; j<a.w;j++) b.x[i][j]= (b.x[i][j]+x[i][k]*a.x[k][j])%mod;returnb; + } A voidInitintHintW) { This->h=h; This->w=w;return;} at }a,b; -LL C[MAXN][MAXN];intn,m; - ll Powi (ll X,ll y) { -LL ans=1; x%=mod; for(inti=y;i;i>>=1, X=x*x%mod)if(i&1) Ans=ans*x%mod;returnans; - } -mATX POWM (mATX A,intN) { inmATX ans=a,tmp=a;n--; for(inti=n;i;i>>=1, tmp=tmp*tmp)if(i&1) ans=ans*tmp;returnans; - } toLL INV (ll a) {returnPowi (a,mod-2);} + voidINITC () { - for(intI=0; i<=m;i++) c[i][0]=1, c[i][i]=1; the for(intI=1; i<=m;i++) for(intj=1; j<i;j++) c[i][j]= (c[i-1][j]+c[i-1][j-1])%mod;return; * } $ voidInitmatx () {Panax NotoginsengA.init (1, m+2); B.init (m+2, m+2); a.x[0][0]=1; - for(intI=0; i<=m;i++) for(intj=0; j<=i;j++) b.x[j][i]=C[i][j]; the for(intI=0; i<=m;i++) b.x[i][m+1]=C[m][i]; +b.x[m+1][m+1]=INV (m);return; A } theInlineintRead () { + intx=0, sig=1;CharCh=GetChar (); - for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=0; $ for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0'; $ returnsig?x:-x; - } -InlinevoidWriteLong Longx) { the if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; - intlen=0;Long Longbuf[ -]; while(x) buf[len++]=x%Ten, x/=Ten;Wuyi for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; the } - intMain () { WuN=read (); M=read (); INITC (); Initmatx (); A=powm (b,n); -Write ((a.x[0][m+1]*powi (m,n))%MoD); About return 0; $}
Coj 2124 day8-Example 1