JZOJ 1240. Fibonacci sequence

博客围绕一道题目展开,包含题目描述、输入输出等信息。分析部分提及刚学矩阵乘法,考场不知如何求前n项,给出了s[n]=2(fn + 2)-1的公式,代码部分为转载内容。

题目

Description
 
Input
 
Output
 
 
Sample Input
 
Sample Output
 
 
Data Constraint

 

分析

 

  • 前几天刚刚学矩阵乘法啊
  • 然后考场不知道怎么求前n项
  • s[n]=2(fn+2)-1

代码

 1 #pragma GCC optimize(3)
 2 #include<iostream>
 3 #include<cstring> 
 4 #include<cstdio>
 5 #define MOD 10000
 6 #define ll long long
 7 using namespace std;
 8 struct sb
 9 {
10     ll a[2][2];
11 };
12 sb mul(sb x,sb y)
13 {
14     sb res;
15     memset(res.a,0,sizeof(res.a));
16     for (int i=0;i<2;i++)
17       for (int j=0;j<2;j++)
18         for (int k=0;k<2;k++)
19           res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[j][k])%MOD;
20     return res;
21 }
22 //ll anss[8000001];
23 ll mat_pow(int n)
24 {
25 //    int wz=n;
26 //    if (anss[wz]!=0) return anss[wz];
27     sb res,c;
28     c.a[0][0]=c.a[0][1]=c.a[1][0]=1;
29     c.a[1][1]=0;
30     memset(res.a,0,sizeof(res.a));
31     for (int i=0;i<2;i++) res.a[i][i]=1;
32     while (n)
33     {
34         if (n&1!=0) res=mul(res,c);
35         c=mul(c,c);
36         n>>=1;
37     }
38 //    anss[wz]=res.a[0][1]%MOD;
39     return res.a[0][1]%MOD;
40 }
41 int main ()
42 {
43     int T;
44     cin>>T;
45     ll ans=0,a,aa,b,bb;
46     for (int i=1,x,y;i<=T;i++)
47     {
48         cin>>x>>y;
49         a=mat_pow(x-1);
50         if (x-2<0) aa=0;
51         else 
52           aa=mat_pow(x-2);
53         b=mat_pow(y);
54         bb=mat_pow(y-1);
55         if (x==1) 
56         cout<<((2*b)%MOD+bb-1)%MOD<<endl;
57         else
58         cout<<(((2*b)%MOD+bb-1-((2*a)%MOD+aa-1))%MOD+MOD)%MOD<<endl;
59     }
60 }

 

 

转载于:https://www.cnblogs.com/zjzjzj/p/11166697.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值