HDU 3292 No more tricks, Mr Nanguo 矩阵快速幂求佩尔方程第k大的解


No more tricks, Mr Nanguo

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


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

根据题意知X^2-N*Y^2=1.如果N是完全平方数,则无解,否则先求出特解,在结合快速幂求出第k大的解。
//0MS	228K
#include<stdio.h>
#include<math.h>
#define M 8191
int x,y,n,d;//x,y为最小正整数解
struct Matrax
{
    int m[4][4];
}per,a;
void search(int d)
{
    y=1;
    while(1)
    {
        x=(long long)sqrt(d*y*y+1);
        if(x*x-d*y*y==1)break;
        y++;
    }
}
void init()//建立矩阵
{
    a.m[0][0]=x%M;
    a.m[0][1]=d*y%M;
    a.m[1][0]=y%M;
    a.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);
}
Matrax multi(Matrax a,Matrax b)//矩阵相乘
{
    Matrax c;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            c.m[i][j]=0;
            for(int 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 c,p,ans=per;
    p=a;
    while(k)
    {
        if(k&1){ans=multi(ans,p);k--;}
        else {k/=2;p=multi(p,p);}
    }
    return ans;
}
int main()
{
    int k;
    while(scanf("%d%d",&d,&k)!=EOF)
    {
        int aa=sqrt(d);
        if(aa*aa==d){printf("No answers can meet such conditions\n");continue;}
        search(d);
        n=2;
        init();
        a=power(k-1);
        x=(a.m[0][0]*x%M+a.m[0][1]*y%M)%M;
        printf("%d\n",x);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题决能力。此外,在职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值