codeforces 227E. Anniversary矩阵快速幂

题目

E. 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 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
Copy
10 1 8 2
Output
Copy
3
Input
Copy
10 1 8 3
Output
Copy
1

给你一个区间 和一个整数 k 求这个区间中k个元素的子集 求以这些元素为下标的斐波那契数列的最大公约数最大。
gcd(f(n),f(m))=f(gcd(n,m))
所以先求gcd再求斐波那契。

#include <iostream>
#include<cmath>
#include<string.h>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdio>
#define mn 1000010
using namespace std;
int a[1000009],sum[1000009],m;
struct pi
{
    long long ah[2][2];
    pi()
    {
        ah[0][0]=ah[1][1]=1;
        ah[0][1]=ah[1][0]=0;
    }

};
pi multi(pi x,pi y)
{
    pi z;
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            z.ah[i][j]=0;//这个小地方卡了我一下午。
            for(int k=0;k<2;k++)
                z.ah[i][j]=(z.ah[i][j]%m+x.ah[i][k]%m*y.ah[k][j]%m)%m;
            z.ah[i][j]%=m;
        }
    }
    return z;
}
pi quimul(pi x,long long int n)
{
    pi an;
    for(;n;n>>=1)
    {
        if(n&1)
            an=multi(an,x);
        x=multi(x,x);
    }
    return an;
}
long long max(long long a,long long b)
{
    if(a>b)
        return a;
    else
        return b;
}
int main()
{
    long long int n,l,r;
    cin>>m>>l>>r>>n;
        long long int ans=-1;
        for(long long int i=1;i*i<=r;i++)
        {
            if(r/i-(l-1)/i>=n)//判断r~l区间内有多少个i如果个数大于要分割的就说明有多个可能在同一区间即该数可能为最大公约数
                ans=max(ans,i);
            if(r/(r/i)-(l-1)/(r/i)>=n)
                ans=max(ans,r/i);
        }
        if(m==1)
            cout<<0<<endl;
        else
        if(ans<=2)
            cout<<1<<endl;
    else{
        pi aha;
        aha.ah[0][0]=1;
        aha.ah[0][1]=1;
        aha.ah[1][0]=1;
        aha.ah[1][1]=0;
        aha=quimul(aha,ans-1);
        cout<<(aha.ah[0][0]+m)%m<<endl;
    }
    
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值