您的位置:首页 > 其它

hdu3292(解pell方程,求第K个解)

2013-08-06 10:27 148 查看

No more tricks, Mr Nanguo

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 198 Accepted Submission(s): 112



[align=left]Problem Description[/align]
Now Sailormoon girls want to tell you a ancient idiom story named “be there just to make up the number”. The story can be described by the following words.

In the period of the Warring States (475-221 BC), there was a state called Qi. The king of Qi was so fond of the yu, a wind instrument, that he had a band of many musicians play for him every afternoon. The number of musicians is just a square number.Beacuse
a square formation is very good-looking.Each row and each column have X musicians.

The king was most satisfied with the band and the harmonies they performed. Little did the king know that a member of the band, Nan Guo, was not even a musician. In fact, Nan Guo knew nothing about the yu. But he somehow managed to pass himself off as a yu
player by sitting right at the back, pretending to play the instrument. The king was none the wiser. But Nan Guo's charade came to an end when the king's son succeeded him. The new king, unlike his father, he decided to divide the musicians of band into some
equal small parts. He also wants the number of each part is square number. Of course, Nan Guo soon realized his foolish would expose, and he found himself without a band to hide in anymore.So he run away soon.

After he leave,the number of band is Satisfactory. Because the number of band now would be divided into some equal parts,and the number of each part is also a square number.Each row and each column all have Y musicians.

[align=left]Input[/align]
There are multiple test cases. Each case contains a positive integer N ( 2 <= N < 29). It means the band was divided into N equal parts. The folloing number is also a positive integer K ( K < 10^9).

[align=left]Output[/align]
There may have many positive integers X,Y can meet such conditions.But you should calculate the Kth smaller answer of X. The Kth smaller answer means there are K – 1 answers are smaller than them. Beacuse the answer may be very large.So
print the value of X % 8191.If there is no answers can meet such conditions,print “No answers can meet such conditions”.

[align=left]Sample Input[/align]

2 999888
3 1000001
4 8373


[align=left]Sample Output[/align]

7181
600
No answers can meet such conditions


分析题目得:X^X-N*Y*Y=1(标准的pell方程) X^X-d*Y*Y=1

1:若N为完全平方数(如4,25,36,49等等),则方程无解

2:否则解为

X
=X[]n-1]*X[1]+d*Y[n-1]*Y[1];

Y
=X[n-1]*Y[1]+Y[n-1]*X[1];

本题求第K个解:

使用快速矩阵求幂的方法,求矩阵的k-1次幂

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAXN 4
#define M 8191
using namespace std;
typedef struct
{
int m[MAXN][MAXN];
}Matrax;
Matrax per,d;
int n;
int x,y,D;
void Search()
{
y=1;
while(1)
{
x=(long long)sqrt(D*y*y+1);
if(x*x-D*y*y==1)
break;
y++;
}
}
void initial()
{
d.m[0][0]=x%M;
d.m[0][1]=D*y%M;
d.m[1][0]=y%M;
d.m[1][1]=x%M;
for(int i=0;i<n;i++) //构造单位矩阵
for(int j=0;j<n;j++)
per.m[i][j]=(i==j); //单位矩阵,主对角元为1
}
Matrax multi(Matrax a,Matrax b)
{
Matrax c;
int k,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
c.m[i][j]=0;
for(k=0;k<n;k++)
c.m[i][j]+=a.m[i][k]*b.m[k][j];
c.m[i][j]%=M;
}
return c;
}
Matrax power(int k)
{
Matrax p,ans=per; //此时ans为单位矩阵
p=d;
while(k)
{
if(k&1)
{
ans=multi(ans,p);
k--;
}
k/=2;
p=multi(p,p);
}
return ans;
}
int main()
{
int K;
while(scanf("%d%d",&D,&K)==2)
{
int ad=sqrt(D+0.0);
if(ad*ad==D)
{
printf("No answers can meet such conditions\n");
continue;
}
Search();
n=2;
initial();
d=power(K-1);
x=(d.m[0][0]*x%M+d.m[0][1]*y%M)%M;
printf("%d\n",x);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: