So Easy!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2026 Accepted Submission(s): 624
Problem Description
A sequence Sn is defined as:
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
You, a top coder, say: So easy!
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
You, a top coder, say: So easy!
Input
There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
Output
For each the case, output an integer Sn.
Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
Sample Output
4
14
4
Source
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 typedef __int64 LL; 7 8 LL p ; 9 struct Matrix 10 { 11 LL mat[3][3]; 12 void init() 13 { 14 mat[1][1]=1;mat[1][2]=0; 15 mat[2][1]=0;mat[2][2]=1; 16 } 17 void mem(LL a,LL b) 18 { 19 mat[1][1]=(2*a)%p; mat[1][2]=(b-a*a)%p; 20 mat[2][1]=1; mat[2][2]=0; 21 } 22 }; 23 Matrix multiply(Matrix cur,Matrix ans) 24 { 25 Matrix now; 26 memset(now.mat,0,sizeof(now.mat)); 27 int i,j,k; 28 for(i=1;i<=2;i++) 29 { 30 for(k=1;k<=2;k++) 31 { 32 for(j=1;j<=2;j++) 33 { 34 now.mat[i][j]+=cur.mat[i][k]*ans.mat[k][j]; 35 now.mat[i][j]%=p; 36 while(now.mat[i][j]<0) now.mat[i][j]+=p; 37 } 38 } 39 } 40 return now; 41 } 42 void pow_mod(Matrix cur,LL n,LL a,LL b) 43 { 44 Matrix ans; 45 ans.init(); 46 while(n) 47 { 48 if(n&1) ans=multiply(ans,cur); 49 n=n>>1; 50 cur=multiply(cur,cur); 51 } 52 LL sum=(ans.mat[1][1]*2*a+ans.mat[1][2]*2)%p; 53 printf("%I64d ",sum); 54 } 55 int main() 56 { 57 LL a,b,n; 58 while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&p)>0) 59 { 60 Matrix hxl; 61 hxl.mem(a,b); 62 if(n>1) 63 pow_mod(hxl,n-1,a,b); 64 else printf("%I64d ",(2*a)%p); 65 } 66 return 0; 67 }