C. Anniversary

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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 ll + 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 mlr 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 %I64dspecifier.

Output

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

Examples

input

Copy


10 1 8 2


output

Copy


3


input

Copy


10 1 8 3


output

Copy


1


 

题意:

给出l,r,k,在区间[l,r]中找k个不同数字使得以这些数字为下标的斐波那契数的最大公约数最大.。输出最大值模m的值.

分析:

这里斐波那契数列有一个性质

gcd(f[m],f[n])=f[gcd(m,n)]

题意转化为:在区间[l,r]中找k个不同数字使得这些数字的最大公约数最大。

求最大的最大公约数可以见

即枚举一下他们的所有因子即可,找到因子个数>=k的最大因子即可,但这题数据范围太大,会T

洛谷上的题解:

CodeForces - 226C Anniversary  斐波那契性质+矩阵快速幂_#include

即对于一段区间[l,r]来说,  num=r/x-(l-1)/x,      num为具有因子x的数的个数,

我们求出来满足个数k的最大因子x来,最后答案就是F[k],矩阵快速幂求

 

//矩阵快速幂注意特判
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define N 10005
using namespace std;
const int MAXN= 35;
int p;
typedef long long LL;
int len=35;//构造矩阵的长度
struct Matrix//注意定义的是方阵,*法也是
{
LL M[MAXN][MAXN];
Matrix(const bool I = 0) ///初始化对角矩阵
{
memset(M, 0, sizeof(M));
if (I)
for(int i = 0; i < len; i++)
M[i][i] = 1;
}
Matrix operator *(const Matrix &y) ///矩阵乘法,对乘法重新定义,z=x*y
{
Matrix z;
for (int i = 0; i < len; i++)//x矩阵的n
for (int j = 0; j < len; j++)//y矩阵的n
for (int k = 0; k < len; k++)//xy矩阵的m
z.M[i][j] = (z.M[i][j]+M[i][k]*y.M[k][j])%p;
return z;
}
};

Matrix Pow(Matrix A, long long b)///矩阵的快速幂
{
Matrix ans = Matrix(1);
for (; b; b>>=1)
{
if (b&1)
ans = ans*A;
A = A*A;
}
return ans;
}
Matrix Add(Matrix a,Matrix b) //a+b
{
Matrix c;
for(int i = 0; i < len; ++i)
{
for(int j = 0; j < len; ++j)
{
c.M[i][j] = (a.M[i][j] + b.M[i][j]) % p;
}
}
return c;
}
Matrix MatrixSum(Matrix a,int k)//a + a^2 + a^3 + … + a^k
//原理:等比数列二分求和
{
if(k == 1)
return a;

if(k&1)
{
return Add(MatrixSum(a,k-1),Pow(a,k)); //当n是奇数,f[n]=f[n-1]+A^(n);
}
else
{
Matrix E(1);
return MatrixSum(a,k>>1)*Add(Pow(a,k>>1),E); //当n是偶数,f[n]=f[n/2]*(A^(n/2)+E);
}

}
LL l,r,k;
bool judge(LL x)
{
return r/x-(l-1)/x>=k;
}
int main()
{
LL ans=0;

scanf("%I64d%I64d%I64d%I64d",&p,&l,&r,&k);
for(LL i=1; i*i<=r; i++)
{
if(judge(i))
ans=max(ans,i);
if(judge(r/i))
ans=max(ans,r/i);
}
//cout<<ans<<endl;


if(ans==1)
{
ans=1%p;
printf("%I64d",ans);
return 0;
}
else if(ans==2)
{
ans=1%p;
printf("%I64d",ans);
return 0;
}

Matrix A,B;
A.M[0][0]=1;
A.M[0][1]=1;
A.M[1][0]=1;
A.M[1][1]=0;

B=Pow(A,ans-2);
printf("%I64d",(B.M[0][0]+B.M[0][1])%p);

return 0;
}