Anniversary(CF-226C)

Problem Description

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

Input

10 1 8 2

Output
3

Input

10 1 8 3

Output
1

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

思路:

对于斐波那契数列的 GCD,存在性质:gcd(f[m],f[n])=f[gcd(m,n)]

因此,问题就转换为:在区间 [l,r] 中找 k 个不同的数字使得这些数字的最大公约数最大

假设存在一个数字 x,那么对于 x-1 来说,其在 [l,r] 的倍数的个数变化了,这样的 x 存在 √r 个

根据数据范围,使用 √n 的枚举,由于要求的是最大值,因此只需要对 √r 之内的每一个数 i 判断 i 与 r/i 的倍数在 [l,r] 中是否大于等于 k 个即可

简单来说,对于一个区间 [l,r],具有因子为 x 的数的个数 num=r/x-(l-1)/x,因此求出满足个数 k 的最大因子 x,最后的答案就是 F[x]

此外,由于数据范围,直接推导斐波那契数列会 TLE,因此需要利用矩阵快速幂来推导斐波那契数列

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 50+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int n=35;
LL mod,l,r,k;
struct Matrix{
    LL s[N][N];
    Matrix(){
        memset(s,0,sizeof(s));
    }
}E;
Matrix mul(Matrix A,Matrix B){
    Matrix temp;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            for (int k=0;k<n;k++)
                temp.s[i][j]=(temp.s[i][j]+(A.s[i][k]*B.s[k][j])%mod)%mod;
    return temp;
}
Matrix quickPow(Matrix A,LL k){
    Matrix res=E;
    while(k){
        if(k&1)
            res=mul(res,A);
        A=mul(A,A);
        k>>=1;
    }
    return res;
}
void init(){
    for(int i=0;i<n;i++)
        E.s[i][i]=1;
}
int main() {
    init();
    scanf("%lld%lld%lld%lld",&mod,&l,&r,&k);

    LL res=0;
    for(LL i=1;i*i<=r;i++) {
        if(r/i-(l-1)/i>=k)
            res=max(res,i);
        if(r/(r/i)-(l-1)/(r/i)>=k)
            res=max(res,r/i);
    }

    if(res==1) {
        res=1%mod;
        printf("%lld",res);
    }
    else if(res==2) {
        res=1%mod;
        printf("%lld",res);
    }
    else {
        Matrix A;
        A.s[0][0]=1; A.s[0][1]=1;
        A.s[1][0]=1; A.s[1][1]=0;

        Matrix B=quickPow(A,res-2);
        printf("%lld",(B.s[0][0]+B.s[0][1])%mod);
    }

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值