#include <cstdio>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
#define ull unsigned long long
struct Matrix {
ull v[2][2];
};
Matrix A, B={1L,0L,0L,1L};//E Matrix
ull M;
Matrix mul(Matrix m1,Matrix m2,ll M) {
int i,j,k;
Matrix c;
for (i=0; i<2;i++)
for (j=0; j<2;j++) {
c.v[i][j]=0;
for (k=0; k<2;k++) {
c.v[i][j]+=(m1.v[i][k]*m2.v[k][j]) %M;
c.v[i][j] %=M;
}
c.v[i][j] %=M;
}
return c;
}
Matrix Mpow(Matrix A, Matrix B, ll n, ll M) {
Matrix x= A, c = B;
// great idea
while (n>=1) {
if (n&1L) c = mul(c,x,M);
n>>=1;
x=mul(x,x,M);
}
return c;
}
int main()
{
freopen("1.in","r",stdin);
ll a, b, n, t;
while (cin>>a>>b>>n>>M) {
if (n==1) {
cout<<(2*(a%M))%M<<endl;
continue;
}
if (n==2) {
cout<<((2*((a%M)*(a%M))%M)%M+(2*b)%M)%M<<endl;
continue;
}
A.v[0][0] = (2*(a%M)) %M;
A.v[0][1] = 1;
t=b-a*a;
while (t<0) t+=M;
A.v[1][0] = t;
A.v[1][1] = 0;
Matrix p = Mpow(A,B,n-2,M), q={((2*((a%M)*(a%M))%M)%M+(2*b)%M)%M,
(2*(a%M))%M,0L,0L};
p = mul(q,p,M);//zuo cheng
cout<<p.v[0][0]<<endl;
}
return 0;
}
cout 和 printf 在输出longlong时有区别?
用cout就对,printf就错了。。
神奇的题目。