斐波那契

题意

$f(x)$为斐波那契数列,求$\large\left(\sum\limits_{i=1}^nf(i)\right) mod f(m) mod p$

$0<n,m,p<1e18$

题解

  1. m 为偶数

    • $\frac n m$为偶数,$f(n) mod f(m)=f(n\%m)$
    • $\frac n m$为奇数,$f(n) mod f(m)=f(m-1)f(n\%m) mod f(m)$
  2. m 为奇数

    • $\frac n m$为偶数,$\frac n {2m}$为偶数,$f(n) mod f(m)=f(n\%m)$
    • $\frac n m$为偶数,$\frac n {2m}$为奇数,$f(n) mod f(m)=f(m)-f(n\%m)$
    • $\frac n m$为奇数,$\frac n {2m}$为偶数,$f(n) mod f(m)=f(m-1)f(n\%m) mod f(m)$
    • $\frac n m$为奇数,$\frac n {2m}$为奇数,$f(n) mod f(m)=f(m)-f(m-1)f(n\%m) mod f(m)$
  3. 简化$f(m-1)f(n\%m) mod f(m)$

    性质:若$n\ge1,r\ge2$,则$f(n)f(n+r-1)-f(n+1)f(n+r-2)=(-1)^{n+1}f(r-2)$

    令$k=n\%m,k=n+1,m-1=n+r-2$,则$f(n)f(k-1)-f(m-1)f(k)=(-1)^kf(m-k)$

    所以$f(m-1)f(k) mod f(m)=(-1)^{k+1}f(m-k) mod f(m)$

    • 当$k$为奇时,$f(m-1)f(n\%m) mod f(m)=f(m-k)$
    • 当$k$为偶时,$f(m-1)f(n\%m) mod f(m)=f(m)-f(m-k)$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define inlld(lld) scanf("%lld", &lld)
#define inlf(f) scanf("%lf", &f)
#define ind(d) scanf("%d", &d)
#define ins(s) scanf("%s", s)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef std::pair<long long, long long> pll;
typedef std::vector<long long> vll;
typedef std::pair<int, int> pii;
typedef unsigned long long ull;
typedef std::vector<int> vii;
typedef long double db;
typedef long long ll;
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
ll mod = 1e9+7;
const int N = 1.1e5;
const db eps = 1e-8;
using namespace std;
int sign(db a) { return a < -eps ? -1 : a > eps; }
int db_cmp(db a, db b){ return sign(a-b); }

struct matrix {
ll a[2][2];
matrix(){
mem0(a);
}
};
ll qMulti(ll x,ll y,ll mod){ //O(1)快速乘
return (x*y-(ll)((long double)x/mod*y)*mod+mod)%mod;
}
matrix mat_mul(matrix x, matrix y) {
matrix res;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
res.a[i][j] = (res.a[i][j] + qMulti(x.a[i][k] , y.a[k][j],mod) ) % mod;
return res;
}
ll mat_pow(ll n) {
if (n <= 2) return 1;
n -= 2;
matrix c, res;
c.a[0][0] = c.a[0][1] = c.a[1][0] = 1;
for (int i = 0; i < 2; i++) res.a[i][i] = 1;
while (n) {
if (n % 2) res = mat_mul(res, c);
c = mat_mul(c, c);
n /= 2;
}
return (res.a[0][0] + res.a[0][1]) % mod;
}
int main() {
#ifdef PerpEternal
// freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin);
// freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);
#endif
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n,m,p;
cin>>n>>m>>p;
mod=p;
n+=2;
ll k=n/m;
if(n%m==0){
cout<<((mat_pow(m)-1)%p+p)%p<<endl;
}else{
int Sign;
if(m%2)Sign=-1;
else Sign=1;
if(k%2){
k/=2;
if(k%2==0)Sign=1;
if(n%m%2==0)Sign=-Sign;
if(Sign==-1){
cout<<((mat_pow(m)-mat_pow(m-n%m)-1)%p+p)%p<<endl;
}else cout<<((mat_pow(m-n%m)-1)%p+p)%p<<endl;
}else{
k/=2;
if(k%2&&Sign==-1){
cout<<((mat_pow(m)-mat_pow(n%m)-1)%p+p)%p<<endl;
}else cout<<((mat_pow(n%m)-1)%p+p)%p<<endl;
}
}
return 0;
}

引用

https://blog.csdn.net/acdreamers/article/details/21822165