任意门: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. Azure 上 Linux 虚拟机 Mac 地址的持久化

    有些用户在使用 Azure Linux 虚拟机安装软件时,有些软件的 license 会和当前系统的 mac 地址绑定,那么在 Azure VM 重启,reszie(改变尺寸大小),停止然后再启动的时 ...

  2. 数据结构(二) --- 伸展树(Splay Tree)

    文章图片和代码来自邓俊辉老师课件 概述 伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由丹尼尔·斯立特Daniel Sleator ...

  3. C# Windows服务创建应用

    创建项目 1.创建windows服务项目 2.右键点击Service1.cs,查看代码, 用于编写操作逻辑代码 3.OnStart函数在启动服务时执行,OnStop函数在停止服务时执行.代码中OnSt ...

  4. 在线HTML文档编辑器使用入门之图片上传与图片管理的实现

    在线HTML文档编辑器使用入门之图片上传与图片管理的实现: 官方网址: http://kindeditor.net/demo.php 开发步骤: 1.开发中只需要导入选中的文件(通常在 webapp ...

  5. spring_boot启动报错

    配置好pom文件后,在controller加注解,如下: 运行后报错!!! 发现配置加的是多此一举,修改为下边的,运行OK

  6. 8、springboot之定时任务

    @Configuration @EnableScheduling public class SchedulingConfig { @Scheduled(cron = "0/5 * * * * ...

  7. vsphere client 创建虚拟机 如何关联到本地iso文件

    问题:以前用过vmere 创建虚拟机,都要在虚拟机启动之前配置系统镜像文件,第一次使用vspere client时在创建虚拟机后,我就想着应该先配置,再启动,其实,非也,应该这样,先启动虚拟机,在点按 ...

  8. php查找字符串中第一个非0的位置截取

    $str = '00000000000000000000000000000000000000001234506'; $preg = '/[0]*/'; $result = preg_replace($ ...

  9. 滴滴开源 Vue 组件库— cube-ui

    cube-ui 是滴滴去年底开源的一款基于 Vue.js 2.0 的移动端组件库,主要核心目标是做到体验极致.灵活性强.易扩展以及提供良好的周边生态-后编译. 自 17 年 11 月开源至今已有 5 ...

  10. 洛谷P1600 天天爱跑步(差分 LCA 桶)

    题意 题目链接 Sol 一步一步的来考虑 \(25 \%\):直接\(O(nm)\)的暴力 链的情况:维护两个差分数组,分别表示从左向右和从右向左的贡献, \(S_i = 1\):统计每个点的子树内有 ...