hdu3292
佩尔方程,详见《数论概论》(原书第三版)
网上其他解题方法大都是用矩阵,其实快速幂也可以。
参考:http://blog.csdn.net/z690933166/article/details/9746843
(今天中午作为“娘家人”去见了队友的男朋友,尴尬,不知道该怎么办,希望没有给队友丢人)
(我们都是小怪兽,有一天会被正义的奥特曼杀死。——江南 《龙族》 喜欢绘梨衣,萌萌哒)
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> using namespace std; #define MOD 8191 void km(int *ansx,int *ansy,int x,int y,int n,int k){ *ansx=1; *ansy=0; x=x%MOD; y=y%MOD; while(k){ if(k%2){ //*ansx=((*ansx)*x%MOD+(*ansy)*y%MOD*n%MOD)%MOD; //*ansy=((*ansx)*y%MOD+x*(*ansy)%MOD)%MOD;//ansx的值已经变了,所以这样算不对 int tempx=((*ansx)*x%MOD+(*ansy)*y%MOD*n%MOD)%MOD; int tempy=((*ansx)*y%MOD+x*(*ansy)%MOD)%MOD; *ansx=tempx; *ansy=tempy; } k=k/2; /*x=(x*x%MOD+y*y%MOD*n*(这里应该是%MOD,结果写成了*,下面直接复制粘贴过去的,所以也一直错。)MOD)%MOD; y=(x*y%MOD+x*y%MOD)%MOD;//x的值已经改变了,所以这样算不对 */ int tempx=(x*x%MOD+y*y%MOD*n%MOD)%MOD; int tempy=(x*y%MOD+x*y%MOD)%MOD; x=tempx; y=tempy; //printf("%d %d\n",x,y); } return; } int main(){ int n,k; while(scanf("%d%d",&n,&k)!=EOF){//没有写!=EOF,结果Output Limit Exceeded int tempn=sqrt(n); if(tempn*tempn==n){ printf("No answers can meet such conditions\n"); } else{ int x,y; int ansx,ansy; for(int i=2;;i++){ int temp=i*i-1; if(temp%n){ continue; } else{ temp=temp/n; int temptemp=sqrt(temp); if(temptemp*temptemp==temp&&temptemp>0){ x=i; y=temptemp; break; } } } //printf("%d %d\n",x,y); km(&ansx,&ansy,x,y,n,k); printf("%d\n",ansx); } } return 0; }