Title: Known recursive formula and edge value, to find the last m (0<m<5) bit number of an item.
Topic Analysis: Matrix Two-Power template problem.
The code is as follows:
1# include<iostream>2# include<cstdio>3# include<cstring>4# include<algorithm>5 using namespacestd;6 structMatrix7 {8 intr,c,m[3][3];9Matrixint_r,int_c): R (_r), C (_c) {}Ten }; One inta,b,n,m; A intmod[4]={Ten, -, +,10000}; - matrix Multiply (matrix A,matrix b) - { the Matrix C (A.R,B.C); - for(intI=1; i<=c.r;++i) { - for(intj=1; j<=c.c;++j) { -c.m[i][j]=0; + for(intk=1; k<=a.c;++k) { -c.m[i][j]+= (a.m[i][k]*b.m[k][j]); +c.m[i][j]%=mod[m-1]; A } at } - } - returnC; - } -Matrix Matrix_pow (Matrix A,intk) - { in if(k==0){ - for(intI=1; i<=a.r;++i) to for(intj=1; j<=a.c;++j) +a.m[i][j]= (i==j)?1:0; - returnA; the } * if(k==1) $ returnA;Panax NotoginsengMatrix Res=matrix_pow (a,k/2); -res=multiply (res,res); the if(k&1) +res=multiply (res,a); A returnRes; the } + intMain () - { $ intT; $scanf"%d",&T); - while(t--) - { thescanf"%d%d%d%d",&a,&b,&n,&m); - if(n==0){Wuyiprintf"%d\n", a%mod[m-1]); the Continue; - } WuMatrix Mat (2,2); -mat.m[1][1]=mat.m[1][2]=mat.m[2][1]=1, mat.m[2][2]=0; AboutMat=matrix_pow (mat,n-1); $Matrix ans (2,1); -ans.m[1][1]=b,ans.m[2][1]=A; -ans=multiply (Mat,ans); -printf"%d\n", ans.m[1][1]); A } + return 0; the}
UVA-10689 yet another number Sequence (matrix two-power template)