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 %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

题意:在l-r中取k个数,使他们作为下标对应的斐波那契数gcd值最大,输出这个最大值%m的值

题解:
首先是斐波那契数列的高妙性质
gcd(Fi[a],Fi[b])=Fi[gcd(a,b)]
所以问题变成了在l-r区间里找k个数使他们的gcd最大
这可以在sqrt(r)的范围内搞出来
再用矩阵快速幂求一波斐波那契第n个数的值就可以了
代码如下:
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; long long l,r,k,mod; struct matrix
{
long long m[][];
}; matrix mul(matrix a,matrix b)
{
matrix c;
c.m[][]=(a.m[][]*b.m[][]+a.m[][]*b.m[][])%mod;
c.m[][]=(a.m[][]*b.m[][]+a.m[][]*b.m[][])%mod;
c.m[][]=(a.m[][]*b.m[][]+a.m[][]*b.m[][])%mod;
c.m[][]=(a.m[][]*b.m[][]+a.m[][]*b.m[][])%mod;
return c;
} matrix kasumi(matrix a,long long b)
{
matrix ans;
ans.m[][]=ans.m[][]=;
ans.m[][]=ans.m[][]=;
while(b)
{
if(b&)
{
ans=mul(ans,a);
}
a=mul(a,a);
b>>=;
}
return ans;
} int check(long long x)
{
long long uli=r/x*x;
long long dli=l%x?(l/x+)*x:l/x*x;
return k<=(uli-dli)/x+;
} int main()
{
scanf("%lld%lld%lld%lld",&mod,&l,&r,&k);
long long gg=;
for(long long i=;i*i<=r;i++)
{
if(check(i)) gg=max(gg,i);
if(check(r/i)) gg=max(gg,r/i);
}
matrix a;
a.m[][]=a.m[][]=a.m[][]=;
a.m[][]=;
matrix ans=kasumi(a,gg);
printf("%lld\n",ans.m[][]%mod);
}


CodeForces 227E Anniversary (斐波那契的高妙性质+矩阵快速幂)的更多相关文章

  1. POJ3070 斐波那契数列递推 矩阵快速幂模板题

    题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...

  2. Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)

    题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...

  3. D - Frog and Portal (利用斐波那契数列的性质)

    题目链接:https://cn.vjudge.net/contest/270201#problem/D 具体思路:利用斐波那契数列的性质,斐波那契数列可以构成任何正整数,所以按照顺序减下去肯定能减到0 ...

  4. codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质

    E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  5. HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  6. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

  7. P1962 斐波那契数列-题解(矩阵乘法扩展)

    https://www.luogu.org/problemnew/show/P1962(题目传送) n的范围很大,显然用普通O(N)的递推求F(n)铁定超时了.这里介绍一种用矩阵快速幂实现的解法: 首 ...

  8. HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)

    M斐波那契数列 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submi ...

  9. 关于斐波拉契数列(Fibonacci)

    斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10 ...

随机推荐

  1. 查看Android内存,cpu

    转自https://testerhome.com/topics/2583 一.查看内存 查看Android应用内存: adb shell dumpsys meminfo 1.查看详细的内存: adb ...

  2. ncbi API

    https://www.ncbi.nlm.nih.gov/sviewer/?db=nuccore&query_key=2&term=DYNLL2&page_size=1& ...

  3. 简单获取各大视频网站的flash地址

    最近做网站的时候遇到一个需求:给定一个视频地址,获取它的swf地址.例如,给一个优酷的视频地址:http://v.youku.com /v_show/id_XNDg4MzY5ODU2.html,想获取 ...

  4. Django-2的路由层(URLconf)

    URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码 ...

  5. 归纳一下:C#线程同步的几种方法

    转自原文 归纳一下:C#线程同步的几种方法 我们在编程的时候,有时会使用多线程来解决问题,比如你的程序需要在后台处理一大堆数据,但还要使用户界面处于可操作状态:或者你的程序需要访问一些外部资源如数据库 ...

  6. jquery 三元运算

    三元运算: 条件  ? 条件为真取此值 : 条件为假取此值; var v = $(:check).prop('checked')?faule:true; $(:check).prop('checked ...

  7. multiprocessing.dummy

    昨晚发现放在腾讯云主机上通过crontab定时执行用以爬去斗鱼分类页面数据的爬虫在执行的时候速度特别慢,于是想通过多线程来提高效率. 打开浏览器,键入关键字"python 多线程" ...

  8. Uniform & Attribute & Varying

    [Uniform & Attribute & Varying] 顶点着色器的输入变量用关键字“attribute”来限定. 片段着色器的输入变量(它和顶点着色器的输出变量相对应)用关键 ...

  9. Spark之 RDD

    简介 RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的集合. Resilien ...

  10. 【LA2531 训练指南】足球联赛 【最大流】

    题意: 有n支队伍进行比赛,每支队伍需要打的比赛数目相同.每场比赛恰好一支队伍胜,另一支败.给出每支队伍目前胜的场数和败的场数,以及每两支队伍还剩下的比赛场数,确定所有可能的冠军的球队.(获胜场数最多 ...