码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-17 23:48:07      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:+=   decided   orm   real   .com   contains   title   down   个人   

任意门: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 = 2;
 6 const int mod = 8191;
 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 = 1;
17     while(1){
18         x = (int)sqrt(D*y*y+1.0);
19         if(x*x - D*y*y == 1) break;
20         y++;
21     }
22 }
23 
24 void init()
25 {
26     d.m[0][0] = x%mod;
27     d.m[0][1] = D*y%mod;
28     d.m[1][0] = y%mod;
29     d.m[1][1] = x%mod;
30     for(int i = 0; i < MAXN; i++)
31         for(int j = 0; 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 = 0; i < MAXN; i++)
39     for(int j = 0; j < MAXN; j++){
40         c.m[i][j] = 0;
41         for(int k = 0; 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&1){
53             ans = multi(ans, p);
54             k--;
55         }
56         k>>=1;
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-1);
74         printf("%d\n", (d.m[0][0]*x%mod+ d.m[0][1]*y%mod)%mod);
75     }
76     return 0;
77 }
View Code

 

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

标签:+=   decided   orm   real   .com   contains   title   down   个人   

原文地址:https://www.cnblogs.com/ymzjj/p/10549369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!