HDU 4565 So Easy(矩阵解公式)
So Easy
【题目链接】So Easy
【题目类型】矩阵解公式
&题解:
感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的.


所以最后2Xn就是答案
【时间复杂度】\(O(logn)\)
&代码:
#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int si=2;
int M,a,b,n;
struct mat
{
int m[si][si];
}A;
mat Mul(mat a,mat b)
{
mat c;
for(int i=0;i<si;i++)
for(int j=0;j<si;j++){
c.m[i][j]=0;
for(int k=0;k<si;k++){
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
}
}
return c;
}
mat bPow(mat a,ll z)
{
mat b;
for(int i=0;i<si;i++)for(int j=0;j<si;j++)
b.m[i][j]=(i==j);
while(z){
if(z&1)
b=Mul(b,a);
a=Mul(a,a);
z>>=1;
}
return b;
}
int tb[si];
void Init()
{
tb[0]=a,tb[1]=1;
A.m[0][0]=a,A.m[0][1]=b;
A.m[1][0]=1,A.m[1][1]=a;
}
void DF(mat A)
{
for(int i=0;i<si;i++){
for(int j=0;j<si;j++)
cout<<A.m[i][j]<<" ";
cout<<endl;
}
}
int main()
{
freopen("E:1.txt","r",stdin);
while(cin>>a>>b>>n>>M){
//输入必须要取模,在这wa了1次
a%=M,b%=M;
Init();
A=bPow(A,n-1);
// DF(A);
ll ans=0;
for(int i=0;i<si;i++)
ans=(ans+A.m[0][i]*tb[i])%M;
cout<<2*ans%M<<endl;
}
return 0;
}
HDU 4565 So Easy(矩阵解公式)的更多相关文章
- 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 ...
- HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简
http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...
- hdu 4565 So Easy! (共轭构造+矩阵快速幂)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
- 【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
[解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下来就是矩阵的快速幂了. 神图: 给个大神的链接:构造类斐波那契数列的矩阵快速幂 ...
- 数学(矩阵乘法):HDU 4565 So Easy!
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 4565 So Easy!(公式化简+矩阵)
转载:http://www.klogk.com/posts/hdu4565/ 这里写的非常好,看看就知道了啊. 题意很easy.a,b,n都是正整数.求 Sn=⌈(a+b√)n⌉%m,(a−1)2&l ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- hdu 4565 So Easy!(矩阵+快速幂)
题目大意:就是给出a,b,n,m:让你求s(n); 解题思路:因为n很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...
随机推荐
- [No0000119]什么是柳比歇夫的时间事件记录法
上图是我过去一年来做的时间事件记录中的某几天的记录文字.从接触到这种方法以来,也就是2009年的7月31日到今天,我已经作了一年多时间的记录.那么什么是时间事件记录?很简单,就像那两幅图片上所展示的, ...
- idea 乱码问题
1. db browser查询结果为乱码: 找到idea的安装目录 如C:\..\Roaming\JetBrains\IntelliJ IDEA Community Edition 2018.1.3\ ...
- DOM节点的基础操作
1.寻找节点 //寻找节点 id方法 document.getElementById(); //标准 //寻找节点层次方法 parentNode().firstChild()和lastChild(): ...
- Usage of git
目录 Git 配置 查看配置信息 基本概念 Git 创建仓库 git init git clone 撤销操作 从暂存区恢复文件 从仓库恢复某个文件 版本退回 版本前进 分支操作 删除未跟踪的文件 连 ...
- OC中如何优化代理是否响应某个方法
看以下示例代码: if([_delegate respondsToSelector: @selector(someClassDidSomething:)){ [_delegate someClassD ...
- Flink - allowedLateness
WindowOperator processElement final Collection<W> elementWindows = windowAssigner.assignWindow ...
- airflow docker
https://github.com/puckel/docker-airflow 镜像介绍:https://hub.docker.com/r/puckel/docker-airflow/ docker ...
- xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at[转载]
今天在添加友盟统计的podfile pod install报错了: bogon:Children songximing$ pod install /Library/Ruby/Gems//gems/co ...
- ipv6的校验格式
ipv6的校验格式: ^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
- zabbix监控托管主机遇到问题
昨天监控公司的托管主机时发现监控不上,回想起来其实就是个小问题,分分钟能解决的事,排错的过程才是真正耗心费神的. 监控环境: A zabbix server: 192.168.17.110 serve ...