首页 > 代码库 > HDU 3292

HDU 3292

快速幂模+佩尔方程

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>const int Mod=8191;struct Matrax {	int m[3][3];};Matrax a,per;void slove(int d,int &x1,int &y1){	y1=1;	while(true){		x1=(int)sqrt(1.0*d*y1*y1+1);		if(x1*x1-d*y1*y1==1)		break;		y1++;	}}Matrax multi(Matrax a,Matrax b){	Matrax c;	for(int i=0;i<2;i++){		for(int j=0;j<2;j++){			c.m[i][j]=0;			for(int k=0;k<2;k++)			c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%Mod;		}	}	return c;}Matrax Power(int k){	Matrax ans=per,p=a;	while(k){		if(k&1){			ans=multi(ans,p);		}		k>>=1;		p=multi(p,p);	}	return ans;}int main(){	int n,k,x1,y1,xt,yt,xn,yn;	while(scanf("%d%d",&n,&k)!=EOF){		int sq=(int)sqrt(n*1.0);		if(sq*sq==n){			printf("No answers can meet such conditions\n");			continue;		}		slove(n,x1,y1);		per.m[0][0]=per.m[1][1]=1;		per.m[0][1]=per.m[1][0]=0;		a.m[0][0]=x1; a.m[0][1]=n*y1;		a.m[1][0]=y1; a.m[1][1]=x1;		Matrax ans=Power(k-1);		int ansx=0;		ansx=(ans.m[0][0]*x1+ans.m[0][1]*y1)%Mod;		printf("%d\n",ansx);	}	return 0;}

  

HDU 3292