任意门: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:

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = ;
const int mod = ;
typedef struct
{
int m[MAXN][MAXN];
}Matrix;
Matrix per, d;
int x, y, D; void Find_ans()
{
y = ;
while(){
x = (int)sqrt(D*y*y+1.0);
if(x*x - D*y*y == ) break;
y++;
}
} void init()
{
d.m[][] = x%mod;
d.m[][] = D*y%mod;
d.m[][] = y%mod;
d.m[][] = x%mod;
for(int i = ; i < MAXN; i++)
for(int j = ; j < MAXN; j++)
per.m[i][j] = (i==j);
} Matrix multi(Matrix a, Matrix b)
{
Matrix c;
for(int i = ; i < MAXN; i++)
for(int j = ; j < MAXN; j++){
c.m[i][j] = ;
for(int k = ; k < MAXN; k++)
c.m[i][j] += a.m[i][k] * b.m[k][j];
c.m[i][j]%=mod;
}
return c;
} Matrix qpow(int k)
{
Matrix p = d, ans = per;
while(k){
if(k&){
ans = multi(ans, p);
k--;
}
k>>=;
p = multi(p, p);
}
return ans;
} int main()
{
int K;
while(~scanf("%d %d", &D, &K)){
int ad = (int)sqrt(D+0.0);
if(ad*ad == D){
puts("No answers can meet such conditions");
continue;
}
Find_ans();
init();
d = qpow(K-);
printf("%d\n", (d.m[][]*x%mod+ d.m[][]*y%mod)%mod);
}
return ;
}

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. Android 屏蔽recent task 按钮

    Step 1 Add this permission to the manifest.xml file <uses-permission android:name="android.p ...

  2. Shiro - 自定义filterChainDefinitions和Realm

    在Spring Context中定义shiroFilter(org.apache.shiro.spring.web.ShiroFilterFactoryBean)时需要为其filterChainDef ...

  3. Spring 数据传入

    表单传入 前端代码: <form method="POST" id="user_login_submit"> <div class=" ...

  4. 会话技术Cookie

    1.会话技术 1>什么是会话技术: 从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话. 2>作用: 会话技术就是记录这次会话中客户端的状态与数据的. Cookie:数 ...

  5. spring框架-----轻量级的应用开发框架

    一.bean 1.容器实例化 ApplicationContext ac=             new ClassPathXmlApplicationContext("applicati ...

  6. Java 集合类常用方法

    Collection中的contains()方法和remove()方法. boolean contains(Object o);该方法是用来判断集合中是否包含某个元素,若包含,返回true,不包含返回 ...

  7. Mybaits插入记录返回主键值

    某些情况进行insert时不知道主键值(主键为自增),例如系统新增用户时,有用户序号(主键 自增),用户名,密码.插入时只需插入用户名和密码,之后取得mysql自增的序号. 如下为mysql的usr表 ...

  8. Mybatis执行sql(insert、update、delete)返回值问题

    数据库:Mysql 在使用mybatis的过程中对执行sql的返回值产生疑问,顺手记录一下. 结论: insert:   插入n条记录,返回影响行数n.(n>=1,n为0时实际为插入失败) up ...

  9. phpmyadmin登录报错crypt_random_string requires at least one symmetric cipher be loaded 解决方法

    通过phpmyadmin登陆时提示以下错误: phpmyadmin crypt_random_string requires at least one symmetric cipher be load ...

  10. python中循环删除list和dict类型注意事项

    列表和字典在循环操作(增删)时,其长度会改变 # 删除 li = [11, 22, 33, 44, 'rock']中索引为单数的元素 # 方法一 del li[1::2] print(li) # [1 ...