HDU 3292 【佩尔方程求解 && 矩阵快速幂】
任意门: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
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.
题意概括:
滥竽充数的故事,一开始所有人可以排成一个 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 【佩尔方程求解 && 矩阵快速幂】的更多相关文章
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- hdu 6185 递推+【矩阵快速幂】
<题目链接> <转载于 >>> > 题目大意: 让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...
- 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 ...
- HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...
- [hdu 2604] Queuing 递推 矩阵快速幂
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- HDU - 4990 Reading comprehension 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...
- 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 ...
- HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...
- HDU 6470:Count(矩阵快速幂)
Count Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
随机推荐
- [转]【Oracle Database 12c新特性】32k varchar2 max_string_size
本文转自:https://blogs.oracle.com/askmaclean/entry/oracle_database_12c%E6%96%B0%E7%89%B9%E6%80%A7_32k_va ...
- 创建WPF用户控件
wpf用户自定义控件和winform创建方法类似,这里先纠正一个误区,就是有很多人也是添加,然后新建,然后是新建用户控件库,但是为什么编译好生成后Debug目录下还是只有exe文件而没有dll文件呢? ...
- Nodejs 8.0 踩坑经验汇总
.Linq:Linq to sql 类 高度集成化的数据库访问技术 使用Linq是应该注意的问题: 1.创建Linq连接后生成的dbml文件不要变动,生成的表不要碰,拖动表也会造成数据库连接发生变动, ...
- mac隐藏和显示隐藏文件
显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults write com.apple.finder Ap ...
- 一:ActiveMQ知识整理
一:JMS概念 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消 ...
- 三:Mybatis知识整理(3)
一:mybatis中模糊查询的方法: 1.直接传参法:在java传参时进行拼接 -- %keyword% 2.mysql内置函数:concart('%',#{keyword},'%') -- 拼接sq ...
- Emmet使用方法
本文摘自:http://www.iteye.com/news/27580 Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语 ...
- Javascript获取页面表格中的数据
var main=mygrid.gettable("11"); //表示获取非固定列的表格 var main1=mygrid.gettable("01");// ...
- git 命令备忘
git 常用命令备忘 仅作为日常使用备忘,并非常用命名整理 删除源端分支 git push orgin --delete branch_a a分支某次提交应用到b分支 切换到branch_b 分支: ...
- mac phpstrom 环境配置
因为mac下自带php,但是没有环境(ini文件)所有需要自己重新安装一下: curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5 # 5.5 ...