CF--Anniversary--斐波那契數+矩陣快速冪

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

給出m,l,r,k。求l-r中選k個數使得他們斐波那契數的最大公約數最大--轉化成選k個數使得他們最大公約數最大(斐波那契數性質)。

第一步:求出k個數使得它們最大公約數最大。枚舉,x從1-r,枚舉的是這k個數的最大公約數,如果r/x-(l-1)/x>=k,那麼這個x符合條件,否則不行。找出這個x.

第二步:求出斐波那契矩陣的的x次冪,利用矩陣快速冪即可,最後輸出mod m即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn=20+66;
const ll MOD=1e9+7;
ll mod,l,r,k;
bool check(ll x)
{
    //cout<<(r/x-(l-1)/x)<<"----"<<endl;
    return (r/x-(l-1)/x)-k>=0;
}
struct Mat{
    ll m[101][101];
};//結構體存矩陣
Mat a,e;//a是輸入的矩陣,e是單位矩陣
ll n,p;
Mat Mul(Mat x,Mat y) //矩陣乘
{
    Mat c;
    for(int i=1;i<=n;i++)
      for(int j=1;j<=n;j++)
        c.m[i][j]=0;
    for(int i=1;i<=n;i++)
      for(int j=1;j<=n;j++)
        for(int k=1;k<=n;k++)
          c.m[i][j]=c.m[i][j]%mod+x.m[i][k]*y.m[k][j]%mod;
    return c;
}
Mat pow(Mat x,ll y) //矩陣快速冪
{
    Mat ans=e;
    while(y)
    {
        if(y&1)
         ans=Mul(ans,x);
        x=Mul(x,x);
        y>>=1;
    }
    return ans;
}
int main()
{
    while(scanf("%I64d%I64d%I64d%I64d",&mod,&l,&r,&k)!=EOF)
    {
        n=2;
        ll gcd=0;
      // cout<<check(8)<<"+++"<<endl;
        for(ll i=1;i*i<=r;i++)
        {
            if(check(i))
            {
                //cout<<i<<"---"<<endl;
                gcd=max(gcd,i);
            }
            if(check(r/i))
            {
                gcd=max(gcd,r/i);
               //cout<<i<<"---"<<endl;
            }
        }
        for(int i=1;i<=n;i++)
        e.m[i][i]=1;
        a.m[1][1]=a.m[1][2]=a.m[2][1]=1;
        a.m[2][2]=0;
       //cout<<gcd<<"ddd"<<endl;
        Mat ans=pow(a,gcd);
        ll ans1=ans.m[2][1]%mod;
        printf("%I64d\n",ans1);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章