任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292

No more tricks, Mr Nanguo

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

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

题意概括:

滥竽充数的故事,一开始所有人可以排成一个 X*X 的方阵, 去掉一个人后 所有人可以排成 N 个 Y*Y 的方阵,

求满足上述条件的第K大的总人数。

解题思路:

佩尔方程模板题

可根据关系列出方程: x*x - D*( y*y) = 1;

暴力求出特解;

解的递推式为:

Xn  = Xn-1  × X1 + d × Yn-1 ×Y1

Yn  = Xn-1  × Y1 + Yn-1  × X1

矩阵快速幂递推:

AC code:

  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. #define LL long long
  4. using namespace std;
  5. const int MAXN = ;
  6. const int mod = ;
  7. typedef struct
  8. {
  9. int m[MAXN][MAXN];
  10. }Matrix;
  11. Matrix per, d;
  12. int x, y, D;
  13.  
  14. void Find_ans()
  15. {
  16. y = ;
  17. while(){
  18. x = (int)sqrt(D*y*y+1.0);
  19. if(x*x - D*y*y == ) break;
  20. y++;
  21. }
  22. }
  23.  
  24. void init()
  25. {
  26. d.m[][] = x%mod;
  27. d.m[][] = D*y%mod;
  28. d.m[][] = y%mod;
  29. d.m[][] = x%mod;
  30. for(int i = ; i < MAXN; i++)
  31. for(int j = ; j < MAXN; j++)
  32. per.m[i][j] = (i==j);
  33. }
  34.  
  35. Matrix multi(Matrix a, Matrix b)
  36. {
  37. Matrix c;
  38. for(int i = ; i < MAXN; i++)
  39. for(int j = ; j < MAXN; j++){
  40. c.m[i][j] = ;
  41. for(int k = ; k < MAXN; k++)
  42. c.m[i][j] += a.m[i][k] * b.m[k][j];
  43. c.m[i][j]%=mod;
  44. }
  45. return c;
  46. }
  47.  
  48. Matrix qpow(int k)
  49. {
  50. Matrix p = d, ans = per;
  51. while(k){
  52. if(k&){
  53. ans = multi(ans, p);
  54. k--;
  55. }
  56. k>>=;
  57. p = multi(p, p);
  58. }
  59. return ans;
  60. }
  61.  
  62. int main()
  63. {
  64. int K;
  65. while(~scanf("%d %d", &D, &K)){
  66. int ad = (int)sqrt(D+0.0);
  67. if(ad*ad == D){
  68. puts("No answers can meet such conditions");
  69. continue;
  70. }
  71. Find_ans();
  72. init();
  73. d = qpow(K-);
  74. printf("%d\n", (d.m[][]*x%mod+ d.m[][]*y%mod)%mod);
  75. }
  76. return ;
  77. }

HDU 3292 【佩尔方程求解 && 矩阵快速幂】的更多相关文章

  1. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  2. hdu 6185 递推+【矩阵快速幂】

    <题目链接> <转载于 >>> > 题目大意: 让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...

  3. hdu 4686 Arc of Dream(矩阵快速幂)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...

  4. HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...

  5. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  6. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  7. HDU 1005 Number Sequence:矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意: 数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n ...

  8. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  9. HDU 6470:Count(矩阵快速幂)

    Count Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

随机推荐

  1. windows下查看 mysql二进制日志文件

    有时候需要将linux中的mysql从线上linux种down到windows查看,但是这种binlog日志是二进制的,应该怎么查看呢? 使用window上的mysqlbinlog.exe将其转码到另 ...

  2. angular 与 layer 集成过程

    layer 的提示框和弹层确实也好用,在使用angular的前提下,使用layer遇到诸多麻烦,记录下来. 在类型是1页面层,主要问题在遮罩方面,造成无法编辑. 开始:引入layer 样式,angul ...

  3. 前端小结(5)---- iframe

    iframe对应的div: <div id="iframezone"> <iframe id="iframe" frameborder='0' ...

  4. hdu 1712 (分组背包)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17676 这个问题让我对分组背包更清晰了一点,主要是这个问题: 使用一维数组的 ...

  5. Python-模块3-re

    今天我们就说一个模块,那就是re,不过想要了解re模块,我们得先了解一下什么是正则表达式,有助于我们更好的学习re模块 一.正则表达式 首先, 我们在网页上进行注册或者登陆的时候经常能看到一些格式上的 ...

  6. 三重for循环实现对二维数组的按列排序(JavaScript)

    由C语言联想到的:三重for循环实现对二维数组的按列排序,并且牵扯到数据结构. 自己写的,水平有限,本文属于原创,可能存在错误,忘指正~ function circle() { var a = [ [ ...

  7. Java中避免空指针的几个方法

    equals Object类中的equals 方法在非空对象引用上实现相等关系,具有对称性 x.equals(y) 和 y.equals(x) 结果是一样的,但当x == null时会抛出空指针异常 ...

  8. C# 使用Guid类生成不重复的随机数

    什么是Guid GUID(全局唯一标识符)         全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID ...

  9. 【转】C# GDAL 配置

    共生成9个dll,如下图: 1.在程序中添加*_csharp.dll四个文件的引用: 2.将剩余的五个文件复制到程序的Debug文件夹中:(如果不复制这五个文件就会出现类似“OSGeo.GDAL.Gd ...

  10. EM(期望最大化)算法初步认识

    不多说,直接上干货! 机器学习十大算法之一:EM算法(即期望最大化算法).能评得上十大之一,让人听起来觉得挺NB的.什么是NB啊,我们一般说某个人很NB,是因为他能解决一些别人解决不了的问题.神为什么 ...