首页 技术 正文
技术 2022年11月18日
0 收藏 944 点赞 4,575 浏览 2260 个字

思路:找规律发现这个数是斐波那契第2*k+3项-1,数据较大矩阵快速幂搞定. 快速幂入门第一题QAQ

#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <ctype.h>
//******************************************************
#define lrt (rt*2)
#define rrt (rt*2+1)
#define LL long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define exp 1e-8
//***************************************************
#define eps 1e-8
#define inf 0x3f3f3f3f
#define INF 2e18
#define LL long long
#define ULL unsigned long long
#define PI acos(-1.0)
#define pb push_back
#define mk make_pair#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define SQR(a) ((a)*(a))
#define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define Iterator(a) __typeof__(a.begin())
#define rIterator(a) __typeof__(a.rbegin())
//#define FastRead ios_base::sync_with_stdio(0);cin.tie(0)
#define FastRead ios::sync_with_stdio(false);
#define CasePrint pc('C'); pc('a'); pc('s'); pc('e'); pc(' '); write(qq++,false); pc(':'); pc(' ')
#define vi vector <int>
#define vL vector <LL>
#define For(I,A,B) for(int I = (A); I < (B); ++I)
#define FOR(I,A,B) for(int I = (A); I <= (B); ++I)
#define rFor(I,A,B) for(int I = (A); I >= (B); --I)
#define Rep(I,N) For(I,0,N)
#define REP(I,N) FOR(I,1,N)
using namespace std;
const int maxn=1e5+10;
#define mod 998244353struct Matirx {
int h,w;
LL a[5][5];
}ori,res,it;
LL f[5]={0,1,1};
void iint(){
it.w=2;it.h=1;it.a[1][1]=1;it.a[1][2]=0;
res.w=res.h=2;
memset(res.a,0,sizeof(res.a));
res.a[1][1]=res.a[2][2]=1;
ori.w=ori.h=2;
memset(ori.a,0,sizeof(ori.a));
ori.a[1][1]= 1;ori.a[1][2]= 1;
ori.a[2][1]= 1;ori.a[2][2]= 0;
}
Matirx multy(Matirx x,Matirx y){
Matirx z;z.w=y.w;z.h=x.h;
memset(z.a,0,sizeof(z.a));
for(int i=1;i<=x.h;i++){
for(int k=1;k<=x.w;k++){
if(x.a[i][k]==0) continue;
for(int j=1;j<=y.w;j++)
z.a[i][j]=(z.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;
}
}
return z;
}LL Matirx_mod(LL n){
if(n<2) return f[n];
else n-=1;
while(n){
if(n&1) res=multy(ori,res);
ori=multy(ori,ori);
n>>=1;
}
res=multy(it,res);
return res.a[1][1]%mod;
}int main()
{
LL n;
while(scanf("%I64d",&n)!=EOF)
{
iint();
printf("%I64d\n",Matirx_mod(2*n+3)-1);
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848