『算法-ACM竞赛-CodeForces』227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质

『算法-ACM 竞赛-CodeForces』227E 矩阵快速幂求斐波那契+N 个连续数求最大公约数+斐波那契数列的性质

E. Anniversary
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathematician Leonardo Fibonacci. Of course, such important anniversary needs much preparations.

Dima is sure that it’ll be great to learn to solve the following problem by the Big Day: You’re given a set A, consisting of numbers l, l + 1, l + 2, …, r; let’s consider all its k-element subsets; for each such subset let’s find the largest common divisor of Fibonacci numbers with indexes, determined by the subset elements. Among all found common divisors, Dima is interested in the largest one.

Dima asked to remind you that Fibonacci numbers are elements of a numeric sequence, where F1 = 1, F2 = 1, Fn = Fn - 1 + Fn - 2 for n ≥ 3.

Dima has more than half a century ahead to solve the given task, but you only have two hours. Count the residue from dividing the sought largest common divisor by m.

Input
The first line contains four space-separated integers m, l, r and k (1 ≤ m ≤ 109; 1 ≤ l < r ≤ 1012; 2 ≤ k ≤ r - l + 1).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Output
Print a single integer — the residue from dividing the sought greatest common divisor by m.

Examples
inputCopy
10 1 8 2
outputCopy
3
inputCopy
10 1 8 3
outputCopy
1
题意很简单,就是给你第 L 到第 R 个斐波那契额数列,让你选 K 个求 K 个数的最大公约数模 MOD;
在这里首先要明确性质,斐波那契数列第 K 个数与第 S 个数的最大公约数是,第 N 个斐波那契数,N 为 S 与 K 的最大公约数。
所以这个题转化为先求 N 选 K 的最大公约数+矩阵快速幂求斐波那契,N 选 K 的数的最大公约数,因为 K 是连续的,所有有这个性质,每 N 个数一定有一个 N 的倍数,这是后应该判断 K 与区间长度的关系,再判断 L 与 R,与 N 的关系,选取最大值即为 K 组的最大公约数。
带入最大公约数到矩阵快速幂即可。
矩阵快速幂 https://blog.csdn.net/weixin_43627118/article/details/97394804

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
#include<bits/stdc++.h>
using namespace std;
int MOD=1e8+5;
const int maxn=2; //定义方阵的阶数
struct JZ{ long long m[maxn][maxn]; };//定义maxn阶方阵
JZ muti(JZ a,JZ b,int mod);
JZ quick_mod(JZ a,long long k,int mod);
bool chk(long long u, long long L, long long R, long long K) {
if(u == 0) {
return 0;
}
return (R / u) - ((L - 1) / u) >= K;
}
int main()
{
long long L,R,K;
cin >> MOD >> L >> R >> K;
long long te = 0;
for(long long i = 1; i * i <= R; i++) {
if(chk(i, L, R, K)) {
te = max(te, i);
}
}
for(long long bs = 1; bs * bs <= R; bs++) {
if(chk(R / bs, L, R, K)) {
te = max(te, R / bs);
}
}
for(long long bs = 1; bs * bs <= L - 1; bs++) {
if((chk(((L - 1) / bs) - 1, L, R, K))) {
te = max(te, ((L - 1) / bs) - 1);
}
}
JZ demo,ans;
demo.m[0][0]=0; demo.m[0][1]=1; demo.m[1][0]=1; demo.m[1][1]=1;
ans=quick_mod(demo,te,MOD);
cout<<ans.m[1][0]<<endl;
}
JZ muti(JZ a,JZ b,int mod)
{
JZ temp;
memset(temp.m,0,sizeof(temp.m));
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++){
for(int k=0;k<maxn;k++)
{
temp.m[i][j]+=(long long) a.m[i][k]*b.m[k][j]%mod;
}
temp.m[i][j]%=mod;
}
return temp;
}
JZ quick_mod(JZ a,long long k,int mod)
{
JZ ans;
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
ans.m[i][j]=(i==j);
while(k) {
if(k &1) ans =muti(ans,a,mod);
a = muti(a,a,mod);
k >>=1;
}
return ans;
}


『算法-ACM竞赛-CodeForces』227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质
https://chiamzhang.github.io/2024/06/29/『算法-ACM竞赛-CodeForces』227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质/
Author
Chiam
Posted on
June 29, 2024
Licensed under