No more tricks, Mr Nanguo

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

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
依题可得x^2-ny^2=1,所以此题解法为佩尔方程+矩阵快速幂
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <queue>
  7. #include <stack>
  8. #include <cstdlib>
  9. #include <iomanip>
  10. #include <cmath>
  11. #include <cassert>
  12. #include <ctime>
  13. #include <map>
  14. #include <set>
  15. using namespace std;
  16. #pragma comment(linker, "/stck:1024000000,1024000000")
  17. #define lowbit(x) (x&(-x))
  18. #define max(x,y) (x>=y?x:y)
  19. #define min(x,y) (x<=y?x:y)
  20. #define MAX 100000000000000000
  21. #define MOD 1000000007
  22. #define pi acos(-1.0)
  23. #define ei exp(1)
  24. #define PI 3.1415926535897932384626433832
  25. #define ios() ios::sync_with_stdio(true)
  26. #define INF 0x3f3f3f3f
  27. #define mem(a) (memset(a,0,sizeof(a)))
  28. typedef long long ll;
  29. ll n,k,x,y;
  30. const ll maxn=;
  31. struct matrix
  32. {
  33. ll a[][];
  34. };
  35. void serach(ll n,ll &x,ll &y)
  36. {
  37. y=;
  38. while()
  39. {
  40. x=(1ll)*sqrt(n*y*y+);
  41. if(x*x-n*y*y==) break;
  42. y++;
  43. }
  44. }
  45. matrix mulitply(matrix ans,matrix pos)
  46. {
  47. matrix res;
  48. memset(res.a,,sizeof(res.a));
  49. for(int i=;i<;i++)
  50. {
  51. for(int j=;j<;j++)
  52. {
  53. for(int k=;k<;k++)
  54. {
  55. res.a[i][j]+=(ans.a[i][k]*pos.a[k][j])%maxn;
  56. res.a[i][j]%=maxn;
  57. }
  58. }
  59. }
  60. return res;
  61. }
  62. matrix quick_pow(ll m)
  63. {
  64. matrix ans,pos;
  65. for(int i=;i<;i++)
  66. for(int j=;j<;j++)
  67. ans.a[i][j]=(i==j);
  68. pos.a[][]=x%maxn;
  69. pos.a[][]=n*y%maxn;
  70. pos.a[][]=y%maxn;
  71. pos.a[][]=x%maxn;
  72. while(m)
  73. {
  74. if(m&) ans=mulitply(ans,pos);
  75. pos=mulitply(pos,pos);
  76. m>>=;
  77. }
  78. return ans;
  79. }
  80. int main()
  81. {
  82. while(scanf("%lld%lld",&n,&k)!=EOF)
  83. {
  84. ll m=sqrt(n);
  85. if(m*m==n) {printf("No answers can meet such conditions\n");continue;}
  86. serach(n,x,y);
  87. matrix ans=quick_pow(k);
  88. printf("%lld\n",ans.a[][]);
  89. }
  90. return ;
  91. }

hdu 3292 No more tricks, Mr Nanguo的更多相关文章

  1. No more tricks, Mr Nanguo HDU - 3292(pell + 矩阵快速幂)

    No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Jav ...

  2. HDU 3292 【佩尔方程求解 && 矩阵快速幂】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...

  3. HDU 3292

    快速幂模+佩尔方程 #include <iostream> #include <cstdio> #include <algorithm> #include < ...

  4. hdu3293(pell方程+快速幂)

    裸的pell方程. 然后加个快速幂. No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: ...

  5. 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏

    RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

  6. HDU 3634 City Planning (离散化)

    City Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. hdu 3624 City Planning(暴力,也可扫描线)

    City Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  8. 多校赛3- Painter 分类: 比赛 2015-07-29 19:58 3人阅读 评论(0) 收藏

    D - Painter Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status P ...

  9. 每日英语:Best Ways to Ramp Up to A Marathon

    For the record number of American runners who completed an official race event last year, the questi ...

随机推荐

  1. redis的javaclientJedis简单封装

    经过我们团队的一番讨论,终于决定使用redis来进行我们的业务缓存.redis会将数据缓存到内存中,执行效率会非常快.同一时候异步将数据写入到磁盘中.进行持久化. 且redis支持主从同步,支持分布式 ...

  2. ThinkPHP5.0框架开发--第4章 TP5.0路由

    ThinkPHP5.0框架开发--第4章 TP5.0路由 第4章 TP5.0 路由 ================================================== 上次复习 1. ...

  3. Eclipse下载安装教程

    Eclipse下载安装 Eclipse是一款开源软件,免费,实用,也应该是大多数同学接触的第一款java集成开发环境(IDE),简单介绍下下载流程 1.进入官网 百度,Bing,或谷歌搜索Eclips ...

  4. atom安装插件失败 latex

    用atom写latex 链接 http://www.cnblogs.com/schaepher/p/5934184.html 但在gui下安装插件失败 按照以下步骤解决了 安装gitbash cd . ...

  5. HDU 1087 Super Jumping! Jumping! Jumping!【DP】

    解题思路:题目的大意是给出一列数,求这列数里面最长递增数列的和 dp[i]表示到达地点i的最大值,那么是如何达到i的呢,则我们可以考虑没有限制条件时候的跳跃,即可以从第1,2,3,---,i-1个地点 ...

  6. shell-3.bash的基本功能:多命令顺序执行与管道符

    1. 2.

  7. 马云谈AI:未来10-15年传统制造业的痛苦将远超今天的想象

    马云谈AI:未来10-15年传统制造业的痛苦将远超今天的想象 “未来10到15年,传统制造业面临的痛苦将会远远超过今天的想象,企业如果不能从规模化.标准化向个性化和智慧化转型,将很难生存下去.” 9月 ...

  8. [arc086e]snuke line

    题意: 有n个区间,询问对于$1\leq i\leq m$的每个i,有多少个区间至少包含一个i的倍数? $1\leq N\leq 3\times 10^5$ $1\leq M\leq 10^5$ 题解 ...

  9. HDU 6315 Naive Operations(线段树+复杂度均摊)

    发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...

  10. 紫书 习题 11-1 UVa 821 (Floyd)

    水题, Floyd一遍就完了. #include<cstdio> #include<algorithm> #define REP(i, a, b) for(int i = (a ...