hdu 4565 So Easy! (共轭构造+矩阵快速幂)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4565
题目大意:
给出a,b,n,m,求出
的值,
解题思路:
因为题目中出现了开根号,和向上取整后求余,所以用矩阵快速幂加速求解过程的时候,会产生误差,就很自然地想到了凑数,因为(a-1)^2<b<a^2,得出0<a-sqrt(b)<1,则无论n取多大,(a-sqrt(b))^n都是小于1的,(a-sqrt(b))^n 与 (a+sqrt(b))^n共轭,两者展开后会相互抵销,所以((a-sqrt(b))^n + (a+sqrt(b))^n)为整数,假设((a-sqrt(b))^n + (a+sqrt(b))^n)用sn表示,则sn*(a+sqrt(b))+(a-sqrt(b)) = Sn+1 - (a^2-b)*Sn-1,进一步得出 Sn+1 = 2*a*Sn - (a*a - b) * Sn-1,
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
#define LL __int64
LL a, b, n, m;
struct mat
{
LL p[][];
}; mat mul (mat x, mat y);
mat pow (mat x, mat y, LL z); int main ()
{
mat x, y;
while (scanf ("%I64d %I64d %I64d %I64d", &a, &b, &n, &m) != EOF)
{
memset (x.p, , sizeof(x.p));
memset (y.p, , sizeof(y.p));
x.p[][] = (*(a*a+b)%m+m)%m;//要用long long,int相乘的时候会溢出
x.p[][] = (*a) % m;
y.p[][] = (*a) % m;
y.p[][] = ;
y.p[][] = ((b-a*a)%m+m)%m;
//y.p[1][0] = ((b-a*a)+m)%m;//这样取余是错误的,因为还有可能是负数,害wa了好几次
x = pow (x, y, n-);
printf ("%I64d\n", x.p[][]);
}
return ;
} mat mul (mat x, mat y)
{
int i, j, k;
mat z;
memset (z.p, , sizeof(z.p));
for (i=; i<; i++)
for (j=; j<; j++)
{
for (k=; k<; k++)
z.p[i][j] += x.p[i][k] * y.p[k][j];
z.p[i][j] = (z.p[i][j] + m )% m;
}
return z;
}
mat pow (mat x, mat y, LL z)
{
while (z)
{
if (z % )
x = mul(x,y);
y = mul (y, y);
z /= ;
}
return x;
}
hdu 4565 So Easy! (共轭构造+矩阵快速幂)的更多相关文章
- HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)
Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- hdu4686 Arc of Dream ——构造矩阵+快速幂
link: http://acm.hdu.edu.cn/showproblem.php?pid=4686 构造出来的矩阵是这样的:根据题目的ai * bi = ……,可以发现 矩阵1 * 矩阵3 = ...
- HDU 5950:Recursive sequence(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...
- HDU 2855 斐波那契+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 3292 【佩尔方程求解 && 矩阵快速幂】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...
- HDU 2256 Problem of Precision (矩阵快速幂)(推算)
Problem of Precision Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)
M斐波那契数列 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Statu ...
随机推荐
- FDMemTable的详细使用方法
unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Var ...
- Dropbox电面面经
他家电面有2轮,等待onsite.. . 电面1: 国人MM面的.这点感觉非常难得. 统计近期5分钟的点击量,实现hit和getHit两个函数.这题是他家高频题,我用deque实现的,hit的均摊时间 ...
- 微信小程序之 Swiper(轮播图)
1.逻辑层 mine.js // pages/mine/mine.js Page({ /** * 页面的初始数据 */ data: { /*轮播图 配置*/ imgUrls: [ 'http://im ...
- srand rand 随机函数
srand函数是随机数发生器的初始化函数.原型:voidsrand(unsigned int seed); srand和rand()配合使用产生伪随机数序列.rand函数在产生随机数前,需要系统提供的 ...
- 鸟哥的Linux私房菜-----12、学习使用Shell scripts
- Qt学习--初学注意事项
过程.心得: 1)Qt Creator与相关的安装包的安装 我在选择去学习Qt之后,第一件事就是Qt SDK下载安装与配置.最初,在网上发现Qt使用的IDE环境 在Windows上可以选 ...
- HTML的高富帅
1,前端的内容(组成部分有以下三部分) HTML CSS JS 裸体的人 穿上好看的衣服 ...
- ubuntu字符界面下显示中文和调整分辨率
1.sudo apt-get install zhcon 2.vi /etc/zhcon.conf 修改下面两行 x_resolution 1024 y_resolution 768 完成这两步后在 ...
- 解决Linux环境Oracle显示乱码
首先查看当前的编码格式 select userenv('language') from dual; 解决方法: 一.临时解决方法 切换到Oracle用户,执行 export NLS_LAN ...
- leetcode 656. Coin Path
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...