Read the program below carefully then answer the question.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans*2+1)%m;
else ans=ans*2%m;
}
printf("%d\n",ans);
}
return 0;
}
Input
Multi test cases,each line will contain two integers n and m. Process to end of file.
[Technical Specification]
1<=n, m <= 1000000000
Output
For each case,output an integer,represents the output of above program.
Sample Input
1 10 3 100
Sample Output
1 5
f[n]=2*f[n-1]+1=f[n-1]+f[n-1]+1=f[n-1]+2*f[n-2]+1
f[n]=2*f[n-1]=f[n-1]+f[n-1]=f[n-1]+2*f[n-2]+1;
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
ll n,m;
struct matrix
{
ll mat[15][15];
matrix()
{
memset(mat,0,sizeof(mat));
}
};
matrix mul(matrix B,matrix A)
{
int i,j,k;
matrix C;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;k<=3;k++)
C.mat[i][j]=(C.mat[i][j]+B.mat[i][k]*A.mat[k][j])%m;
return C;
}
matrix pow(matrix A,ll p)
{
matrix ans;
for(int i=1;i<=3;i++)
ans.mat[i][i]=1;
while(p)
{
if(p&1)
ans=mul(ans,A);
A=mul(A,A);
p>>=1;
}
return ans;
}
int main()
{
while(~scanf("%lld%lld",&n,&m))
{
matrix a,b;
a.mat[1][1]=1;
a.mat[1][2]=2;
a.mat[1][3]=1;
a.mat[2][1]=1;
a.mat[3][3]=1;
b.mat[1][1]=1;
b.mat[2][1]=0;
b.mat[3][1]=1;
a=pow(a,n-1);
b=mul(a,b);
printf("%lld\n",b.mat[1][1]%m);
}
return 0;
}