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 ...
随机推荐
- [转]How can I install the VS2017 version of msbuild on a build server without installing the IDE?
本文转自:http://stackoverflow.com/questions/42696948/how-can-i-install-the-vs2017-version-of-msbuild-on- ...
- Azure 认知服务--计算机视觉 API - 分析图像
在本节中,笔者将详细介绍 Azure 认知服务中的一种:计算机视觉 (Computer Vision) API. 我的一个客户有需求,他们需要消费者与自己的产品合照,然后上传到服务器并转发到朋友圈. ...
- ASP.NET Visual Studio2010 发布Web网站问题详解
今天研究了一下如何发布web网站,之前总是没耐心,遇到点问题就没心情搞了,今天总算有点耐心搞明白了.其实遇到的问题还是挺多的,网上也没有太全的解释,所以结合自己还有别人的方法进行一下总结. 环境:Wi ...
- JavaScript(JS)计算某年某月的天数(月末)
方法1 /** * 获取某年月的天数 * @param year 年 * @param month 月(0-11) * @returns {number} 天数 */ var getDaysOfMon ...
- JBPM学习第1篇:入门与安装
1.相关关键词 BPM:全称Business Process Management,即业务流程管理 BPEL:全称为Business Process Execution Language,即业务流程执 ...
- cookie实现记住登录名和密码
在最近学习的session作用域中,顺便了解了一下cookie, session是存放在服务器中,而cookie是存放在客户端中. 本篇文章主要是使用cookie来实现记住密码的功能. 简单的logi ...
- csharp: datagridview Convert csv file
/// <summary> /// 保存文件 /// 涂聚文 /// 2014-08-29 /// Geovin Du /// </summary> /// <param ...
- 牛顿迭代,多项式求逆,除法,开方,exp,ln,求幂
牛顿迭代 若 \[G(F_0(x))\equiv 0(mod\ x^{2^t})\] 牛顿迭代 \[F(x)\equiv F_0(x)-\frac{G(F_0(x))}{G'(F_0(x))}(mod ...
- roadflow asp.net core版工作流引擎更新发布
ROADFLOW CORE (.NET CORE工作流引擎)更新说明 1.RoadFlow全新工作流平台采用.NET CORE 2.1重构,结构更简单,逻辑梳理更清析,性能有了很大的提升. 2.表单设 ...
- 二十三、css如何实现锯齿形---border-image
css如何实现这样的样式: 解决方案: 这里需要用到的技术是border-image的灵活运用,首先需要一张图片,这里我选中的是这样子的,此后 的图片可以拿这个更改圆形的颜色以更改锯齿颜色: 底部透明 ...