http://acm.hdu.edu.cn/showproblem.php?pid=2281
又是一道Pell方程
化简构造以后的Pell方程为
求出其前15个解,但这些解不一定满足等式,判断后只有5个满足的情况,直接判断即可
求法可以参照上一篇日志: http://www.cnblogs.com/Felix-F/p/3223323.html
struct matrix { LL ma[2][2]; }; int n = 2; LL nn[20],xx[20]; matrix operator * (matrix a,matrix b) { matrix temp; memset(temp.ma,0,sizeof(temp.ma)); for(int i = 0; i < n ; i++) for(int j = 0; j < n ; j++) for(int k = 0 ; k < n ; k++) temp.ma[i][j] = temp.ma[i][j] + (a.ma[k][i] * b.ma[j][n-k-1]); return temp; } matrix operator + (matrix a,matrix b) { for(int i = 0; i < n ; i++) for(int j = 0; j < n ; j++) a.ma[i][j] = (a.ma[i][j] + b.ma[i][j]) ; return a; } void init() { matrix a; a.ma[0][0] = 1; a.ma[0][1] = 7; a.ma[1][0] = 7; a.ma[1][1] = 48; matrix temp = a; nn[0] = 1; xx[0] = 1; int cnt = 1; for(int i = 1 ;i <= 18 ; i++) { if(i%2 == 0) { LL s1 = temp.ma[0][1] * 7 + temp.ma[1][1] * 1; nn[cnt] = (s1 - 3) / 4; LL s2 = temp.ma[0][0] * 7 + temp.ma[1][0] * 1; xx[cnt++] = s2; } temp = temp*a; } } int main() { init(); LL N; while(cin>>N && N) { for(int i = 7 ; i >= 0 ; i--) { if(N >= nn[i]) { cout<<nn[i]<<" "<<xx[i]<<endl; break; } } } return 0; }