CodeForces -226C - 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 (1 ≤ m ≤ 1e9; 1 ≤ l < r ≤ 1e12; 2 ≤ k ≤ r - l + 1),让你从斐波那契数列的第l项,l+1项,...,第r项中挑k项,使这k项的gcd最大(设最大为x)。输出x%m。

斐波那契数列有一个性质:gcd(F_{n},F_{m})=F_{gcd(n,m)}

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

\left \lfloor \frac{r}{x} \right \rfloor-\left \lfloor \frac{l-1}{x} \right \rfloor\geq k

\left \lfloor \frac{n}{i} \right \rfloor 这种形式还记得吗?取值只有O(\sqrt{n})种,对于上式,我们只需要枚举x=1~sqrt(r),然后直接判断x和r/x是否合法就行了。

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

代码:

#include
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=1e6+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
ll mo;
ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
ll l,m,r,k,n;
ll ans,tmp;
ll find(ll mid)
{
    if((r/mid)-((l-1)/mid)>=k) return 1;
    return 0;
}
struct node
{
    ll n,m,a[7][7];
    void init()
    {
        memset(a,0,sizeof(a));
        for(int i=0;i>=1;
        a=mul(a,a,a.n,a.n,a.n);
    }
    return d;
}
ll poww(ll a,ll n)
{
    ll sum=1;
    while(n)
    {
        if(n&1) sum=sum*a%mo;
        n>>=1;
        a=a*a%mo;
    }
    return sum;
}
int main()
{
    int T,cas=1;
    scanf("%lld%lld%lld%lld",&m,&l,&r,&k);
    ll as=1;
    mo=m;
    for(ll i=1;i*i<=r;i++)
    {
        if(find(i)) as=max(as,i);
        if(find((r/i))) as=max(as,(r/i));
    }
    if(as<=2) printf("%lld\n",1LL%mo);
    else
    {
        n=as;
        node x;
        x.n=2;x.m=2;
        memset(x.a,0,sizeof(x.a));
        x.a[0][0]=1;
        x.a[0][1]=1;
        x.a[1][0]=1;
        x.a[1][1]=0;
        node ans=power(x,n-2);
 
        node tmp;
        tmp.n=2;
        tmp.m=1;
        memset(tmp.a,0,sizeof(tmp.a));
        tmp.a[0][0]=1;
        tmp.a[1][0]=1;
 
        ans=mul(ans,tmp,2,2,1);
        ll sum=(ans.a[0][0]+mo)%mo;
        printf("%lld\n",sum);
    }
 
    return 0;
}

 

你可能感兴趣的:(思维,数论,矩阵快速幂)