P1962 斐波那契数列
题目背景
大家都知道,斐波那契数列是满足如下性质的一个数列:
• f(1) = 1
• f(2) = 1
• f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数)
题目描述
请你求出 f(n) mod 1000000007 的值。
输入输出格式
输入格式:
·第 1 行:一个整数 n
输出格式:
第 1 行: f(n) mod 1000000007 的值
题解
/*
落谷:p1962
题意:求斐波那契数列f(n)mod1e9+7 n在long long范围内
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
LL n,p;
struct node
{
LL m[3][3];
node(){
memset(m,0,sizeof(m));
}
friend node operator *(node x,node y){
node t;
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
t.m[i][j]=0;
}
}
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
for(int k=1;k<=2;k++){
//尽量这样mod规范些
t.m[i][j]+=((x.m[i][k]%mod)*(y.m[k][j]%mod))%mod;
}
}
}
return t;
}
}a,ans;
node binary(node x,LL y)
{
while(y){
if(y&1) ans=ans*x;
x=x*x;
y>>=1;
}
return ans;
}
void Init()
{
a.m[1][1]=1;
a.m[1][2]=1;
a.m[2][1]=1;
a.m[2][2]=0;
ans.m[1][1]=1;
ans.m[1][2]=1;
}
int main()
{
Init();
LL n;scanf("%lld",&n);
if(n<=2) printf("1\n");
else{
node t=binary(a,n-2);
printf("%lld\n",t.m[1][1]%mod);
}
return 0;
}
P1349 广义斐波那契数列
题目描述
广义的斐波那契数列是指形如an=p\times a_{n-1}+q\times a_{n-2}an=p×an−1+q×an−2的数列。今给定数列的两系数pp和qq,以及数列的最前两项a_1a1和a_2a2,另给出两个整数nn和mm,试求数列的第nn项a_nan除以mm的余数。
输入输出格式
输入格式:
输入包含一行6个整数。依次是pp,qq,a_1a1,a_2a2,nn,mm,其中在pp,qq,a_1a1,a_2a2整数范围内,nn和mm在长整数范围内。
输出格式:
输出包含一行一个整数,即a_nan除以mm的余数。
输入输出样例
输入样例#1: 复制
1 1 1 1 10 7
输出样例#1: 复制
6
说明
数列第10项是55,除以7的余数为6。
和第一个的区别是要改下基本矩阵 想办法相乘为第n项就可以
p 1
1 0 这样的一个矩阵
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL mod;
LL p,q,a1,a2,n,m;
struct node
{
LL m[3][3];
node(){
memset(m,0,sizeof(m));
}
friend node operator *(node x,node y){
node t;
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
t.m[i][j]=0;
}
}
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
for(int k=1;k<=2;k++){
t.m[i][j]+=(x.m[i][k]%mod*y.m[k][j]%mod)%mod;
}
}
}
return t;
}
}a,e,b,ans;
node binary(node x,LL y)
{
;
while(y){
if(y&1) ans=ans*x;
x=x*x;
y>>=1;
}
return ans;
}
void Init()
{
a.m[1][1]=p;
a.m[1][2]=1;
a.m[2][1]=q;
a.m[2][2]=0;
ans.m[1][1]=a2;
ans.m[1][2]=a1;
mod=m;
}
int main()
{
scanf("%lld %lld %lld %lld %lld %lld",&p,&q,&a1,&a2,&n,&m);
Init();
if(n==1) printf("%lld\n",a1);
else if(n==2) printf("%lld\n",a2);
else{
node t=binary(a,n-2);
printf("%lld\n",t.m[1][1]%mod);
}
return 0;
}
P1939 【模板】矩阵加速(数列)
题目描述
a[1]=a[2]=a[3]=1
a[x]=a[x-3]+a[x-1] (x>3)
求a数列的第n项对1000000007(10^9+7)取余的值。
输入输出格式
输入格式:
第一行一个整数T,表示询问个数。
以下T行,每行一个正整数n。
输出格式:
每行输出一个非负整数表示答案。
输入输出样例
输入样例#1: 复制
3
6
8
10
输出样例#1: 复制
4
9
19
说明
对于30%的数据 n<=100;
对于60%的数据 n<=2*10^7;
对于100%的数据 T<=100,n<=2*10^9;
推导过程
/*
落谷:p3390
给定n*n的矩阵A,求A^k %p
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
LL p,q,a1,a2,n,m;
struct node
{
LL m[4][4];
node(){
memset(m,0,sizeof(m));
}
friend node operator *(node x,node y){
node t;
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
t.m[i][j]=0;
}
}
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
for(int k=1;k<=3;k++){
t.m[i][j]+=(x.m[i][k]%mod*y.m[k][j]%mod)%mod;
}
}
}
return t;
}
}a,ans;
node binary(node x,LL y)
{
while(y){
if(y&1) ans=ans*x;
x=x*x;
y>>=1;
}
return ans;
}
void Init()
{
memset(a.m,0,sizeof(a.m));
memset(ans.m,0,sizeof(ans.m));
a.m[1][1]=1;
a.m[1][2]=1;
a.m[2][3]=1;
a.m[3][1]=1;
ans.m[1][1]=1;
ans.m[1][2]=1;
ans.m[1][3]=1;
}
int main()
{
int T;scanf("%d",&T);
while(T--){
Init();
LL n;
scanf("%lld",&n);
if(n<=3) printf("1\n");
else{
node t=binary(a,n-3);
printf("%lld\n",t.m[1][1]%mod);
}
}
return 0;
}
例题4:和P1349 广义斐波那契数列一样但是n的范围为:
十进制拆分不过就是b是10的倍数的话,我们让a每次变为原来的10次幂,b变为原来的1/10
他要是给n是一个16进制串,那咱们就搞个16进制拆分快速幂
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+100;
LL mod;
LL p,q,a1,a2,len;
char s[N];
struct node
{
LL m[3][3];
node(){
memset(m,0,sizeof(m));
}
friend node operator *(node x,node y){
node t;
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
t.m[i][j]=0;
}
}
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
for(int k=1;k<=2;k++){
t.m[i][j]+=(x.m[i][k]%mod*y.m[k][j]%mod)%mod;
}
}
}
return t;
}
}a,e,b,ans;
node binary(node x,LL y)
{
node ret;
for(int i=1;i<=2;i++) ret.m[i][i]=1;
while(y){
if(y&1) ret=ret*x;
x=x*x;
y>>=1;
}
return ret;
}
node solve(node A)
{
node ret;
for(int i=1;i<=2;i++) ret.m[i][i]=1;
for(int i=len;i>=1;i--){
int x=s[i]-'0';
ret=ret*binary(A,x);
A=binary(A,10);
}
return ret;
}
void Init()
{
a.m[1][1]=p;
a.m[1][2]=1;
a.m[2][1]=q;
a.m[2][2]=0;
ans.m[1][1]=a2;
ans.m[1][2]=a1;
}
int main()
{
scanf("%lld %lld %lld %lld",&a1,&a2,&p,&q);
scanf("%s",s+1);
scanf("%lld",&mod);
len=strlen(s+1);
Init();
node t=ans*solve(a);
printf("%lld\n",t.m[1][2]%mod);
return 0;
}