hdu4565---So Easy!(矩阵)
Problem Description
A sequence Sn is defined as:
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
You, a top coder, say: So easy!
Input
There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
Output
For each the case, output an integer Sn.
Sample Input
2 3 1 2013 2 3 2 2013 2 2 1 2013
Sample Output
4 14 4
Source
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you: 5185 5184 5181 5180 5177
(a+sqrt(b))^n最后的形式一定形如
X+Y * sqrt(b)
n范围非常大,须要用矩阵来加速
推出转移矩阵为
a 1
b a
(a+sqrt(b))^n = X+Y * sqrt(b)
(a-sqrt(b))^n = X-Y * sqrt(b)
a - 1< sqrt(b) < a
所以z = (a-sqrt(b))^n 大于0 小于1
设ans = (a+sqrt(b))^n
ans+z = 2 * X
2 * X - 1 < ans = 2 * X - z < 2 * X
那么向上取整就是2 * X
/*************************************************************************
> File Name: hdu4565.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年03月14日 星期六 11时01分21秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
LL mod;
class MARTIX
{
public:
LL mat[5][5];
MARTIX();
MARTIX operator * (const MARTIX &b)const;
MARTIX& operator = (const MARTIX &b);
};
MARTIX::MARTIX()
{
memset (mat, 0, sizeof(mat));
}
MARTIX MARTIX :: operator * (const MARTIX &b)const
{
MARTIX ret;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < 2; ++k)
{
ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
ret.mat[i][j] %= mod;
}
}
}
return ret;
}
MARTIX& MARTIX :: operator = (const MARTIX &b)
{
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
this -> mat[i][j] = b.mat[i][j];
}
}
return *this;
}
MARTIX fastpow(MARTIX ret, LL n)
{
MARTIX ans;
for (int i = 0; i < 2; ++i)
{
ans.mat[i][i] = 1;
}
while (n)
{
if (n & 1)
{
ans = ans * ret;
}
ret = ret * ret;
n >>= 1;
}
return ans;
}
void Debug(MARTIX A)
{
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
printf("%lld ", A.mat[i][j]);
}
printf("\n");
}
}
int main ()
{
LL a, b, n;
while (~scanf("%lld%lld%lld%lld", &a, &b, &n, &mod))
{
MARTIX A;
A.mat[0][0] = a;
A.mat[0][1] = 1;
A.mat[1][0] = b;
A.mat[1][1] = a;
A = fastpow(A, n - 1);
MARTIX F;
F.mat[0][0] = a;
F.mat[0][1] = 1;
F = F * A;
printf("%lld\n", (2 * F.mat[0][0]) % mod);
}
return 0;
}
hdu4565---So Easy!(矩阵)的更多相关文章
- hdu4565 So Easy! 矩阵快速幂
A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example ...
- HDU4565 So Easy! 矩阵高速幂外加数学
easy 个屁啊,一点都不easy,题目就是要求公式的值,但是要求公式在最后的取模前的值向上取整.再取模,无脑的先试了高速幂 double fmod来做,结果发现是有问题的.这题要做肯定得凑整数,凑 ...
- HDU4565 So Easy! —— 共轭构造、二阶递推数列、矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4565 So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- HDU4565 So Easy!
/* HDU4565 So Easy! http://acm.hdu.edu.cn/showproblem.php?pid=4565 数论 快速幂 矩阵快速幂 题意:求[(a+sqrt(b))^n ] ...
- hdu4565 So Easy!(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题解:(a+√b)^n=xn+yn*√b,(a-√b)^n=xn-yn*√b, (a+√b)^n ...
- 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 4565 So Easy(矩阵解公式)
So Easy [题目链接]So Easy [题目类型]矩阵解公式 &题解: 感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的. 所以最后2Xn就是答案 [时间复杂度]\(O ...
- HDU 4565 So Easy!(矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题意: 题意: #include <iostream>#include <cs ...
- HDU 4565 So Easy! 矩阵快速幂
题意: 求\(S_n=\left \lceil (a+\sqrt{b})^n \right \rceil mod \, m\)的值. 分析: 设\((a+\sqrt{b})^n=A_n+B_n \sq ...
随机推荐
- new,malloc,GlobalAlloc具体解释
WINDOWS下最好的方式是用VirtualAlloc分配内存,他不是在堆,也不是栈,而是直接在进程的地址空间中保留一快内存.尽管用起来最不方便. 可是速度快,也最灵活 new,malloc,Glob ...
- 可靠的Windows版Redis
副标题: 评论更精彩,教你怎么解决64位Windows版Redis狂占C盘的问题. MS Open Tech 技术团队近期花了非常多时间来測试最新构建的Windows版Redis(可在 MS Open ...
- bzoj2438: [中山市选2011]杀人游戏(强联通+特判)
2438: [中山市选2011]杀人游戏 题目:传送门 简要题意: 给出n个点,m条有向边,进行最少的访问并且可以便利(n-1)个点,求这个方案成功的概率 题解: 一道非常好的题目! 题目要知道最大的 ...
- 使用OpenCV把二进制mnist数据集转换为图片
mnist数据集是以二进制形式保存的,这里借助OpenCV把mnist数据集转换成图片格式.转换程序如下: #include <iostream> #include <fstream ...
- 6. Intellij Idea 2017创建web项目及tomcat部署实战
转自:https://www.cnblogs.com/shindo/p/7272646.html 相关软件:Intellij Idea2017.jdk16.tomcat7 Intellij Idea直 ...
- 转】关于cgi、FastCGI、php-fpm、php-cgi
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- 5.array
#include <iostream> #include <array> #include <algorithm> using namespace std; //a ...
- 解决VMware Pro 14安装Linux镜像时黑屏问题
软件及版本: VMware-workstation-full-14.0.0-6661328 CentOS-6.8-x86_64-bin-DVD1 系统: win10 问题: 启动虚拟机,配置完cent ...
- SQL--通过身份证号得到年龄的
/* =======================================创 建 人:CuiYaChao创建日期:2017-08-16功能描述:通过身份证号来计算年龄单元名称: Fun_Ge ...
- 运维派 企业面试题1 监控MySQL主从同步是否异常
Linux运维必会的实战编程笔试题(19题) 企业面试题1:(生产实战案例):监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员.提示:如果没主从同步环境,可以用下面文本放到文件里读 ...