题目

题目描述
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 AA , consisting of numbers ll , l+1l+1 , l+2l+2 , … , rr ; let’s consider all its kk -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 F_{1}=1F
1

=1 , F_{2}=1F
2

=1 , F_{n}=F_{n-1}+F_{n-2}F
n

=F
n−1

+F
n−2

for n>=3n>=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 mm .

输入格式
The first line contains four space-separated integers mm , ll , rr and kk (1<=m<=10^{9}; 1<=l<r<=10^{12}; 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.

输出格式
Print a single integer — the residue from dividing the sought greatest common divisor by mm .

题意翻译
设FF是斐波那契数列,在[l,r][l,r]中选kk个数a_{1}…a_{k}a
1

…a
k

,使得gcd(F_{a_{1}},F_{a_{2}},…,F_{a_{k}})gcd(F
a
1


,F
a
2


,…,F
a
k


)尽可能大。输出对mm取模后的结果。

输入输出样例
输入 #1复制
10 1 8 2
输出 #1复制
3
输入 #2复制
10 1 8 3
输出 #2复制
1

思路

有一个性质gcd(f(a),f(b))=f(gcd(a,b))
那么问题就转化成在区间 [l,r][l,r] 中找 kk 个不同的数字使得这些数字的最大公约数最大。

那么在一个区间 [l,r] 中,有因子 x 的数的数量是 r/x-(l-1)/x个,我们在sqrt® 中寻找 i 和 r/i 的有因子 x 的数的数量是否大于等于 k,记录下最大的一个因子 id,那么最后的答案就是 Fid

代码

#include <bits/stdc++.h>
using namespace std;
char ch,B[1 << 20],*S = B,*T = B;
ll mod,l,r,k;
struct M 
{
	ll a[3][3];
	M() { memset(a,0,sizeof a); }
	M operator*(const M &b) const 
	{
		M res;
		for(int i = 1; i <= 2; ++i)
			for(int j = 1; j <= 2; ++j)
				for(int k = 1; k <= 2; ++k)
					res.a[i][j] = (res.a[i][j] + 1ll * a[i][k] * b.a[k][j] % mod) % mod;
		return res;
	}
} ans,base;
void init() {
	base.a[1][1] = base.a[1][2] = base.a[2][1] = 1;
	ans.a[1][1] = ans.a[1][2] = 1;
}
void power(int b) {
	while (b) {
		if (b & 1) ans = ans * base;
		base = base * base;
		b >>= 1;
	}
}
void work() {
	mod = rdll();
	l = rdll();
	r = rdll();
	k = rdll();
	int id = 0;
	for(ll i = 1; i * i <= r; ++i)
	{
		if(r / i - (l - 1) / i >= k)
		{
			checkmax(id,i);
		}
		if(r / (r / i) - (l - 1) / (r / i) >= k)
		{
			checkmax(id,r / i);
		}
	}
	if(id <= 2) {
		id = 1 % mod;
		printf("%lld\n",id);
	} else {
		init();
		power(id - 2);
		printf("%lld\n",ans.a[1][1] % mod);
	}
}
int main() 
{
	work();
	return 0;
}