hdu 3292 No more tricks, Mr Nanguo pell方程+矩阵快速幂

来源:互联网 发布:回收站数据恢复破解版 编辑:程序博客网 时间:2024/06/08 18:28

No more tricks, Mr Nanguo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 201    Accepted Submission(s): 113


Problem Description
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.
 

Input
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).
 

Output
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”.
 

Sample Input
2 9998883 10000014 8373
 

Sample Output
7181600 No answers can meet such conditions
 

Author
B.A.C
 

Source
2010 “HDU-Sailormoon” Programming Contest
 

Recommend
lcy

题意:这个题说的是从前有个国王,也是文艺青年,喜欢竽。有一个为自己演奏的乐队。里面有一个叫南郭的人。他不会演奏。但是国王听不出来。后来国王去世了。国王的儿子要把这个乐队分成n个部分演奏。南郭害怕自己被发现所以就跑了。但是,南郭逃跑了之后,小国王把这个乐队分成n个小的乐队之后正好是n个正方形。
分析:根据题目的意思,刚开始是x个人的大正方形的演奏乐队。也就是x^2后来南郭走了,分成n个y^2的小乐队。不难得出方程为x^2-n*y^2=1。。等于的那个1就是南郭啦~~题目输入n和让求的第k个大的数。如果n要是一个完全平方数的话,则没有接。本身y就是完全平方数,n也是。那么方程变为。x^2=1+n*y^2。。加1之后肯定x就不是完全平方数了。

#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;const int mod = 8191;const int maxn = 3;int N,K;struct Matrix{    int data[maxn][maxn];} E,Mul;Matrix mul(Matrix a,Matrix b){    Matrix ans;    int i,j,k;    for(i=0; i<2; i++)    {        for(j=0; j<2; j++)        {            ans.data[i][j]=0;            for(k=0; k<2; k++)            {                ans.data[i][j]+=a.data[i][k]*b.data[k][j];                if(ans.data[i][j]>=mod)                    ans.data[i][j]%=mod;            }        }    }    return ans;}Matrix quick_mod(int n){    Matrix ans = E,hjf=Mul;    for(; n>0; n>>=1)    {        if(n&1)        {            ans = mul(ans,hjf);        }        hjf = mul(hjf,hjf);    }    return ans;}void solve(int x,int y){    Matrix A;    memset(A.data,0,sizeof(A.data));    A.data[0][0]=x;    A.data[1][0]=y;    Mul.data[0][0]=x,Mul.data[0][1]=N*y;    Mul.data[1][0]=y,Mul.data[1][1]=x;    Matrix ans = quick_mod(K-1);    ans = mul(ans,A);    printf("%d\n",ans.data[0][0]);    return ;}int main(){    E.data[0][0]=1,E.data[0][1]=0;    E.data[0][1]=0,E.data[1][1]=0;    while(cin>>N>>K)    {        int x, y, t;        t = sqrt(N+0.5);        if(t*t==N)        {            printf("No answers can meet such conditions\n");            continue;        }        y = 1;        while(1)        {            x = sqrt(N*y*y+1);            if(x*x == N*y*y+1)                break;            y++;        }        //  cout<<"x--->"<<x<<"y--->"<<y<<endl;        solve(x,y);    }    return 0;}


 
原创粉丝点击