HDU 4565 So Easy!

来源:互联网 发布:mac上怎么做u盘启动盘 编辑:程序博客网 时间:2024/06/02 07:28

我不是看这里的,你们不要质疑我。

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>typedef __int64 LL;using namespace std;struct matrix{    LL a[2][2];}origin,res;LL A,B,M;matrix multiply(matrix x,matrix y){    matrix temp;    memset(temp.a,0,sizeof(temp.a));    for(int i=0;i<2;i++)    {        for(int j=0;j<2;j++)        {            for(int k=0;k<2;k++)            {                temp.a[i][j]+=x.a[i][k]*y.a[k][j];                temp.a[i][j]=(temp.a[i][j]+M)%M;            }        }    }    return temp;}void matmod(LL n){    while(n)    {        if(n&1)        res=multiply(res,origin);        n>>=1;        origin=multiply(origin,origin);    }    LL C1=2*A;    LL C0=2;    //printf("c1 = %d\n",C1);    printf("%I64d\n",((res.a[0][0]*C1+res.a[0][1]*C0)+M)%M);}int main(){    LL N;    while(scanf("%I64d %I64d %I64d %I64d",&A,&B,&N,&M)!=EOF)    {        origin.a[0][0]=2*A;        origin.a[0][1]=(-(A*A-B));        origin.a[1][0]=1;        origin.a[1][1]=0;        res.a[0][0]=1;        res.a[0][1]=0;        res.a[1][0]=0;        res.a[1][1]=1;        matmod(N-1);    }    return 0;}