Codeforces Round #140 (Div. 1) C. Anniversary (两个定理的应用(数论+矩阵快速幂)

52 篇文章 0 订阅
16 篇文章 0 订阅

题目链接:

http://codeforces.com/problemset/problem/226/C

C. Anniversary

 

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

10 1 8 2

output

3

input

10 1 8 3

output

1

 首先知道两个定理,算是规律吧

1. F_{GCD(n,m)}=GCD(F_n , F_m)

2   \left \lfloor \frac{x}{i} \right \rfloor = k   表是1-x中能够整除i的数有k个i

题意:

给你四个数m,l,r,k (1 ≤ m ≤ 1e9; 1 ≤ l < r ≤ 1e12; 2 ≤ k ≤ r - l + 1),让你从斐波那契数列的第l项,l+1项,...,第r项中挑k项,使这k项的gcd最大(设最大为x)。输出x%m。

F_{GCD(n,m)}=GCD(F_n , F_m)

因此我们只需要从l~r中挑gcd最大的k个数即可。设最大gcd为x,那么答案就是Fx,并且有

\left \lfloor \frac{r}{x} \right \rfloor - \left \lfloor \frac{l-1}{x} \right \rfloor >= k  找最大的x

对于上式,我们只需要枚举x=1~sqrt(r),然后直接判断x和r/x是否合法就行了。

x可能高达1e12,因此我们用矩阵快速幂计算出答案。不要忘了对m取模!!

This is the  code:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
LL l,r,k;
LL mod;
bool Find(LL mid)
{
    if(r/mid-(l-1)/mid >= k)
        return true;
    return false;
}
struct node
{
    LL n,m,a[7][7];
    node()
    {
        memset(a,0,sizeof(a));
    }
};
node mul(node aa, node bb, LL n, LL m, LL k)
{
    node cc;
    cc.n=n;
    cc.m=k;
    for(int i=0;i<n;i++)
        for(int j=0;j<k;j++)
            for(int k=0;k<m;k++)
                cc.a[i][j]=(cc.a[i][j]+aa.a[i][k]*bb.a[k][j]%mod)%mod;
    return cc;
}
node power(node a, LL m)
{
    node d;
    d.n=d.m=a.n;
    for(int i=0;i<d.n;++i)
        d.a[i][i]=1;
    while(m)
    {
        if(m&1)
            d=mul(d,a,d.n,d.m,a.m);
        m>>=1;
        a=mul(a,a,a.n,a.n,a.n);
    }
    return d;
}
int main()
{
    scanf("%lld%lld%lld%lld",&mod,&l,&r,&k);
    LL maxx=1;
    for(LL i=1; i*i<=r;++i)
    {
        if(Find(i))
            maxx=max(maxx,i);
        if(Find(r/i))
            maxx=max(maxx,r/i);
    }
    if(maxx<=2)//表示斐波那契的前两项
        printf("%lld\n",1LL%mod);
    else
    {
        node x;//系数矩阵
        x.n=2;
        x.m=2;
        x.a[0][0]=1;
        x.a[0][1]=1;
        x.a[1][0]=1;
        x.a[1][1]=0;

        node tmp;//初始值矩阵
        tmp.n=2;
        tmp.m=1;
        tmp.a[0][0]=1;
        tmp.a[1][0]=1;

        node ans=power(x,maxx-2);//计算系数
        ans=mul(ans,tmp,2,2,1);

        printf("%lld\n",ans.a[0][0]);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值