HDU 3292 No more tricks, Mr Nanguo

版权声明:本文为博主原创文章,未经博主允许不得转载。若有误,欢迎私信指教~ https://blog.csdn.net/little_starfish/article/details/82022703

HDU 3292 No more tricks, Mr Nanguo

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 999888
3 1000001
4 8373

Sample Output

7181
600 
No answers can meet such conditions

Author

B.A.C

Source

2010 “HDU-Sailormoon” Programming Contest

题意

给出一个整数N,问其佩尔方程中第K小的正整数X的解(取模8191)

思路

佩尔方程,形如 X^2 - N*Y^2=1
若N为完全平方数,显然方程只有特解(1,0)、(-1,0)
若N不是完全平方数,则有多个解
若佩尔方程的最小特解为(X1,Y1),则:
Xn=Xn-1 * X1 + N * Yn-1 * Y1
Yn=Xn-1 * Y1 + Yn-1 * X1
故,只要求出最小特解后可以通过矩阵快速幂来求第K小的解(Xk,Yk)

如图

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=8191;
typedef struct Matrix{
    int m[4][4];
};
Matrix pre,d;
int x,y,n,k;
void init()  //初始化
{
    y=1;
    while(true){  //求最小X的解
        x=(int)sqrt(n*y*y*1.00+1.00);
        if(x*x-n*y*y==1)  //找到最小的X
            break;
        y++;
    }
    int i,j;
    for(i=0;i<2;i++){  //求单位矩阵
        for(j=0;j<2;j++){
            if(i==j){
                pre.m[i][j]=1;
            }
            else pre.m[i][j]=0;
        }
    }
    //初始矩阵赋值
    d.m[0][0]=x%mod;
    d.m[0][1]=n*y%mod;
    d.m[1][0]=y%mod;
    d.m[1][1]=x%mod;
}
Matrix mul(Matrix a, Matrix b)  //矩阵a与b相乘
{
    Matrix c;
    int i,j,ij;
    for(i=0;i<2;i++){
        for(j=0;j<2;j++){
            c.m[i][j]=0;
            for(ij=0;ij<2;ij++){
                c.m[i][j]+=a.m[i][ij]*b.m[ij][j];
            }
            c.m[i][j]%=mod;
        }
    }
    return c;
}
Matrix quick()  //矩阵快速幂
{
    Matrix ans=pre,a=d;
    while(k){
        if(k&1){
            ans=mul(ans,a);
            k--;
        }
        k>>=1;
        a=mul(a,a);
    }
    return ans;
}
int main()
{
    while(scanf("%d %d",&n,&k)!=EOF){
        int temp=(int)sqrt(n*1.00);
        if(temp*temp==n){  //如果N为完全平方数
            printf("No answers can meet such conditions\n");
            continue;
        }
        init();
        k--;
        Matrix a=quick();
        int ans=(a.m[0][0]*x%mod + a.m[0][1]*y%mod+mod)%mod;
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/little_starfish/article/details/82022703
MR