I - No more tricks, Mr Nanguo 佩尔方程的求解。

题意:滥竽充数的故事,本来是x^2的方形矩阵有一个人滥竽充数,但是换了国王之后他要把这些人分成d份,每一份也组成一个正方形(此时那个滥竽充数的就只能走掉了),然后输入d,让你求解原来的满足条件的第k个数就多少(就是正方形的边长);x^2=d*y^2+1 

 

毕竟是刚学的新知识,虽然是入门数论,还是单独写一个吧,佩尔方程:

x^2-dy^2=1。 这个可以求解很多有趣的不定方差。先看一下推导吧(图片在下面)。可能不是很清晰。其实就是用两个实根去试方程,然后可以得出佩尔方程的各种解来。

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

还有有用带分数求第一个解的代码,暂时不看了先,,还有就是sqrt会卡精度,当数过大的时候不行,debug了半天。

 

 

暴力求解完矩阵快速幂跑就可以了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll d,m,t;
const int mod=8191;
struct node
{
	ll a[3][3];	
}A;
ll qiujie()// 求第一项的解,暴力。 
{
	for(ll i=1;;i++)
	{
		ll x=1+i*i*d;
		ll p=(int)(sqrt(x));
		if(p*p==x)
		{
			return i;
		}
	}
} 
node pow1(node a,node b)
{
	node c={0};
	for(int i=1;i<=2;i++)
	{
		for(int j=1;j<=2;j++)
		{
			c.a[i][j]=0;
			for(int k=1;k<=2;k++)
			{
				c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
			}
		}
	}
	return c;
}
node pow2(node a,ll b)
{
	node c;
	 for(int i=1;i<=2;i++)
        for(int j=1;j<=2;j++)
            c.a[i][j]=(i==j);
	while(b)
	{
		if(b&1) 
		{
			c=pow1(c,a);
		}
		a=pow1(a,a);
		b>>=1;
	}
	return c;
}
int main()
{
	ll n;
	while(scanf("%lld%lld",&d,&n)!=EOF)
	{
		
		//if(sqrt(d)*sqrt(d)==d) 气死了,调了半天竟然是这里的问题,精度不行直接这样用。 
		int p=sqrt(d);
		if(p*p==d)
		{
			printf("No answers can meet such conditions\n"); continue;
		} 
		ll y=qiujie();
		ll x=sqrt(1+y*y*d);
	//	printf("%lld  %lld\n",x,y);
		A.a[1][1]=x;
		A.a[1][2]=d*y;
		A.a[2][1]=y;
		A.a[2][2]=x;
		node ans=pow2(A,n);
		ll res=ans.a[1][1]%mod;
		printf("%lld\n",res);
	}
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值