题目链接<http://acm.hdu.edu.cn/showproblem.php?pid=4565>
题意:
求出:
题解:
首先从入手,很容易发现它的构成是的形式。
假设,那么
所以可以构造出矩阵:.
还有一个向上取整的问题,这题利用共轭来构造出答案,否则精度会有问题。
因为,所以,所以
利用二项式展开可以得出:,所以就是答案。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
ll s[2][2],p[2][2],q[2][2],m;
void mul(ll a[2][2],ll b[2][2]){
ll c[2][2];
memset(c,0,sizeof(c));
for(ll i=0;i<2;i++)
for(ll j=0;j<2;j++)
for(ll k=0;k<2;k++)
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;
memcpy(b,c,sizeof(c));
}
void mpow(ll t){
for(ll i=t;i;i>>=1,mul(p,p)){
if(i&1) mul(p,s);
}
}
int main(){
ll n,a,b;
while(scanf("%lld%lld%lld%lld",&a,&b,&n,&m)!=EOF){
p[0][0]=a;p[0][1]=b;
p[1][0]=1;p[1][1]=a;
s[0][0]=a;s[1][0]=1;
mpow(n-1);
printf("%lld\n",s[0][0]*2%m);
}
}