版本比较
题解:P2106 Sam 数
正文
增加 11 行删除 11 行
25 行未改动2627282930303132333435353637383738394041414243444535 行未改动818283848585868788896 行未改动969798991001001011021031043 行未改动10810911011111211211311411511611711811811912012112012112212312412511 行未改动25 行未改动int dp[N][N][N][N],num[N],pow10[N];void init(){ pow10[0]=1; for(int i=1;i<N;i++) pow10[i]=pow10[i-1]*10; for(int i=1;i<N;i++) pow10[i]=pow10[i-1]*10;}int dfs(int pos,int last,int llast,bool state,bool head,bool limit){ if(!pos) return state; if(!limit && !head && dp[pos][last][llast][state]!=-1) if(!limit && !head && dp[pos][last][llast][state]!=-1) return dp[pos][last][llast][state]; int top=limit?num[pos]:9,ans=0; for(int i=0;i<=9;i++) int top=limit?num[pos]:9,ans=0; for(int i=0;i<=9;i++) (ans+=dfs(pos-1, (head&&!i)?-1:i, head==1?-1:last, abs(llast-last)<=2, head&&!i, limit&&i==top))%=mod; if(!limit) dp[pos][last][llast][state]=ans; return ans; return ans;}int solve(int x){ int idx=0;35 行未改动{ int mat[N][N]; matrix() { memset(mat,0,sizeof(mat)); } //构造函数 matrix operator * (const matrix &B)const matrix operator * (const matrix &B)const {//重载运算符 matrix ans; for(int i=0;i<N;i++) for(int j=0;j<N;j++)6 行未改动matrix mpow(matrix A,int p){//快速幂部分 matrix temp; for(int i=0;i<N;i++) temp.mat[i][i]=1; while(p>0) while(p>0) { if(p&1) temp=A*temp; A=A*A; p>>=1;3 行未改动
int solve(int k){ if(k==1) return 10;//0-9,一位数就是十个数
matrix temp;//构造转移矩阵 for(int i=0;i<N;i++) for(int j=0;j<N;j++) if(abs(i-j)<=2) temp.mat[i][j]=1;
matrix ans=mpow(temp,k-1); int tot=0;
int tot=0; for(int i=1;i<N;i++)//首位不能为0 for(int j=0;j<N;j++) (tot+=ans.mat[i][j])%=mod; return tot;11 行未改动