UVA 10870 - Recurrences UVA • Sep 1, 2020 Problem PDFSolution:/****************************************************************** *** Problem : *** *** Author : Shipu Ahamed (Psycho Timekiller) *** *** E-mail : [email protected] *** *** University : BUBT,Dept. of CSE *** *** Team : BUBT_Psycho *** *** My Blog : http://shipuahamed.blogspot.com *** *** Facebook : http://www.facebook.com/DeesheharaShipu *** ******************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define pi 2*acos(0.0) #define all(v) v.begin(),v.end() //input #define si(t) scanf("%d",&t) #define sl(t) scanf("%lld",&t) #define sf(t) scanf("%f",&t) #define sd(t) scanf("%lf",&t) #define sc(c) scanf("%c",&c) #define sii(a,b) scanf("%d%d",&a,&b) #define sll(a,b) scanf("%lld%lld",&a,&b) //Output #define P(a) printf("%d\n",a) #define PL(a) printf("%lld\n",a) #define PF(a) printf("%f\n",a) #define PD(a) printf("%lf\n",a) #define PS(a) printf("%s\n",a) #define PSN(a) printf("%s ",a) #define PN(a) printf("%d ",a) #define PLN(a) printf("%lld ",a) #define PFN(a) printf("%f ",a) #define PDN(a) printf("%lf ",a) #define PP(a,b) printf("%d %d\n",a,b) #define PPN(a,b) printf("%d %d ",a,b) #define PPL(a,b) printf("%lld %lld\n",a,b) #define PPLN(a,b) printf("%lld %lld ",a,b) #define CP(a) cout< vi; typedef vector vll; typedef vector vs; typedef set si; typedef set ss; typedef map mii; typedef map mll; typedef map msi; typedef map mci; template string toString( T Number ){stringstream st;st << Number;return st.str();} template T SOD(T n) {__typeof(n) sum=0;for(__typeof(n) i=1;i*i<=n;i++)sum+=(n%i)?0:((i*i==n)?i:i+n/i);return sum;} //For Define #define forab(i,a,b) for(__typeof(b) i=(a);i<=(b);i++) #define for0(i,n) forab(i,0,(n)-1) #define for1(i,n) forab(i,1,n) #define rforab(i,b,a) for(__typeof(b) i=(b);i>=(a);i--) #define rfor0(i,n) rforab(i,(n)-1,0) #define rfor1(i,n) rforab(i,n,1) #define forstl(i,s) for(__typeof((s).end()) i=(s).begin(); i != (s).end(); i++) //File input/output #define input freopen("in.txt","r",stdin) #define output freopen("out.txt","w",stdout) //upper bound and lower bound #define LB(a,value) (lower_bound(all(a),value)-a.begin()) #define UB(a,value) (upper_bound(all(a),value)-a.begin()) //Test Case & New line #define Case(no) printf("Case %d: ",++no) #define nl puts("") int stringconvert(string s){int p; istringstream st(s); st>>p ; return p;} ll pow(ll a,ll b, ll m) { ll res = 1; while(b) { if(b & 1) { res = ( (res % m) * (a % m) ) %m ; } a= ((a%m) * (a%m)) %m; b >>= 1; } return res; } ll modInverse(ll a, ll m){return pow(a,m-2,m);} ////============ CONSTANT ===============//// #define mx7 10000007 #define mx6 1000006 #define mx5 100005 #define inf 1<<30 //infinity value #define eps 1e-9 #define mx (100010) ////=====================================//// int row; ll mod; struct Matrix { ll mat[20][20]; }; Matrix zero; Matrix Mul(Matrix &aa, Matrix &bb) { Matrix ret=zero; for(int i = 0; i < row; i ++) { for(int j = 0; j < row; j ++) { for(int k = 0; k < row; k ++) { ret.mat[i][j] += (aa.mat[i][k]*bb.mat[k][j]); ret.mat[i][j] %= mod; } } } return ret; } Matrix exp(Matrix a, ll b) { Matrix res=zero; for0(i,row) { res.mat[i][i]=1; } while(b) { if(b&1) res=Mul(res,a); a=Mul(a,a); b>>=1; } return res; } int main() { int d,n; while(sii(d,n)) { sl(mod); row=d; if(d==0 && n==0 && mod==0) break; Matrix base=zero; for0(i,d) { sl(base.mat[0][i]); } Matrix f=zero; int k=d-1; for0(i,d) { sl(f.mat[k][0]); k--; } for1(i,d-1) { base.mat[i][i-1]=1; } // for0(i,d) // { // for0(j,d) // { // PLN(base.mat[i][j]); // } // nl; // } Matrix res =exp(base, n-1); res = Mul(res,f); // for0(i,d) // { // for0(j,d) // { // PLN(res.mat[i][j]); // } // nl; // } PL(res.mat[d-1][0]); } return 0; } https://github.com/Shipu/OnlineJudgeProblemSolutionWithCPlusPlus/tree/master/uva/10870/10870.cpp Tags UVA Competitive Programming Shipu Ahamed Recommended for you UVA UVA 913 - Joana and the Odd Numbers 4 years ago • 1 min read UVA UVA 871 - Counting Cells in a Blob 4 years ago • 3 min read UVA UVA 866 - Intersecting Line Segments 4 years ago • 3 min read