首页 技术 正文
技术 2022年11月21日
0 收藏 994 点赞 3,374 浏览 1026 个字

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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,468
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,095
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765