hdu 3292 佩尔方程一系列操作
来源:互联网 发布:淘宝买家信用 编辑:程序博客网 时间:2024/05/24 07:17
No more tricks, Mr Nanguo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 445 Accepted Submission(s): 294
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
#include <cstdio> // 当 x^2-D*y^2=1 D 很大时(D>1000)需要用大数#include <cstring>#include <cmath>#include <cstdlib>#include <vector>#include <iostream>using namespace std;typedef long long ll;const int mod = 8191;ll a[20000];vector<ll> xx,yy;struct matrix{ int m[2][2];}ans,bs;matrix multi(matrix a,matrix b){ matrix c; for(int i = 0; i < 2; i++){ for(int j = 0; j< 2; j++){ c.m[i][j] = 0; for(int k = 0; k< 2; k++){ c.m[i][j] = (c.m[i][j]+a.m[i][k]*b.m[k][j])%mod; } } } return c;}int fast_mod(ll x,ll y,ll d,ll n){ bs.m[0][0]=x; bs.m[0][1]=d*y; ans.m[0][0]=ans.m[1][1]=1; bs.m[1][0]=y; bs.m[1][1]=x; ans.m[0][1]=ans.m[1][0]=0; while(n){ if(n&1) ans=multi(ans,bs); bs=multi(bs,bs); n>>=1; } printf("%lld\n",(ans.m[0][0]*x+ans.m[0][1]*y)%mod); return ans.m[0][1];}bool pell_minimum_solution(ll n,ll &x0,ll &y0){ ll m=(ll)sqrt((double)n); double sq=sqrt(n); int i=0; if(m*m==n)return false; a[i++]=m; ll b=m,c=1; double tmp; do{ c=(n-b*b)/c; tmp=(sq+b)/c; a[i++]=(ll)(floor(tmp)); b=a[i-1]*c-b; }while(a[i-1]!=2*a[0]); ll p=1,q=0; for(int j=i-2;j>=0;j--){ ll t=p; p=q+p*a[j]; q=t; } if((i-1)%2==0){x0=p;y0=q;} else{x0=2*p*p+1;y0=2*p*q;} return true;}void get(ll x,ll y,ll d,int n) //利用特解求n个通解;{ xx.push_back(x);yy.push_back(y); for (int i=1;i<=n;i++) { xx.push_back(xx[i-1]*x+d*yy[i-1]*y); yy.push_back(xx[i-1]*y+yy[i-1]*x); }}void getk(ll x,ll y, ll d,ll k) //矩阵快速幂求出第k大的解.{ fast_mod(x,y,d,k-1);}int main(){ ll n,x,y,k; while(~scanf("%lld %lld",&n,&k)){ if(pell_minimum_solution(n,x,y)){ getk(x,y,n,k); }else printf("No answers can meet such conditions\n"); }}
- hdu 3292 佩尔方程一系列操作
- 毕达哥拉斯三元组:poj 1305+佩尔方程:poj 1320,hdu 3292(特殊不定方程)
- HDU 3292 No more tricks, Mr Nanguo(佩尔方程)
- hdu——3292(数论之佩尔方程)
- HDU 3292 No more tricks, Mr Nanguo(佩尔方程)
- HDU 3292 佩尔方程+矩阵快速幂
- HDU 3292 (佩尔方程 矩阵快速幂)
- HDU 3292 pell方程
- HDU 3292 No more tricks, Mr Nanguo(佩尔方程,矩阵快速幂)
- HDU 3292(No more tricks, Mr Nanguo 佩尔方程矩阵快速幂求解)
- HDU 3292 No more tricks, Mr Nanguo [佩尔方程+矩阵]【数论】
- sql函数一系列操作
- 一系列的IO操作
- 单链表的一系列操作
- 双链表一系列操作
- 顺序表一系列操作
- 任务栏的一系列操作
- 关于mysql 一系列操作
- opencv创建滑动条以及滑动条的位置
- 买单侠微服务的API网关演化之路
- Android 7.0 ActivityManagerService(1) AMS的启动过程
- HDU 6070 Dirt Ratio【分数规划】【线段树】
- Apache Torque连接多个数据库及其使用
- hdu 3292 佩尔方程一系列操作
- 聚簇索引与非聚簇索引的区别
- Bootstrap框架 模态框插件 禁止点击空白处关闭模态框事件
- iOS上传应用到蒲公英分发测试
- 初学JAVA02
- 【phantomjs系列】Phantomjs Api介绍
- 在 Ubuntu16.04 上安装 OpenCV3.2.0
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(下)
- 全排列的实现