So Easy!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2022 Accepted Submission(s): 621
Problem Description
A sequence S
n 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 S n.
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 S n.
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 < 2
15, (a-1)
2< b < a
2, 0 < b, n < 2
31.The input will finish with the end of file.
Output
For each the case, output an integer S
n.
Sample Input
2 3 1 2013 2 3 2 2013 2 2 1 2013
Sample Output
4 14 4
Source
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you:
4830
4829
4828
4827
4826
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=4;
ll n,m,a,b;
struct Matrix
{
ll matrix[MAXN][MAXN];
}E;
Matrix matrix_mul(Matrix a,Matrix b)
{
Matrix c;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
{
c.matrix[i][j]=0;
for(int k=1;k<=2;k++)
if(a.matrix[i][k] && b.matrix[k][j])
{
c.matrix[i][j]+=a.matrix[i][k]*b.matrix[k][j];
if(c.matrix[i][j]>=m)
c.matrix[i][j]%=m;
if(c.matrix[i][j]<0)
c.matrix[i][j]=c.matrix[i][j]%m+m;
}
}
return c;
}
Matrix matrix_pow(Matrix a,int k)
{
Matrix c=E;
while(k)
{
if(k&1)
c=matrix_mul(c,a);
a=matrix_mul(a,a);
k>>=1;
}
return c;
}
int main()
{
//freopen("text.txt","r",stdin);
while(~scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&m))
{
if(n==1)
{
printf("%I64d\n",2*a%m);
continue;
}
for(int i=1;i<=2;i++)
E.matrix[i][i]=1;
Matrix A;
A.matrix[1][1]=2*a; A.matrix[1][2]=b-a*a;
A.matrix[2][1]=1; A.matrix[2][2]=0;
A=matrix_pow(A,n-1);
printf("%I64d\n",((A.matrix[1][1]*2*a+A.matrix[1][2]*2)%m+m)%m);
}
}