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. Linux无法登录,显示module is unknown,一闪而过

    1.使用单用户模式登录系统(不作介绍) 2.查看日志:vim /var/log/secure 3.记忆起曾经配置oracle添加过该参数 vim/etc/pam.d/login中加入了: sessio ...

  2. 用 Sqlmap 识别 WAF

    命令: ┌─[root@sch01ar]─[~] └──╼ #sqlmap -u "http://www.sch01ar.com/" --identify-waf --batch ...

  3. php if判断

    php if判断 例子如下: True是否等于False 变量haq是不是老婆呢? <?php $ts=true; $f=false; if (isset($ts)&&isset ...

  4. php Reflection

    <?php function title($title, $name) { return sprintf("%s. %s\r\n", $title, $name); } $f ...

  5. Tkinter Cursors

      Tkinter Cursors:   Python的Tkinter的支持很多不同的鼠标光标的数字.确切的图形可能会有所不同,根据您的操作系统. 这里是大量有趣的的名单: "arrow&q ...

  6. CentOS7.6安装Redis

    官网下载地址:https://redis.io/download 第一步:软件下载安装 进行安装目录:cd /opt/software (如果目录不存在,请先创建目录) 下载二进制包:wget htt ...

  7. Android六大进程间通信方式总结之一:基本知识

    因为不同进程都是享有独立资源的,所以全局变量这些都是无效的,必须有其他的进程间通信方式. 一.基本知识 1:怎样使用多进程 Android正常使用的多进程的办法只有一种,就是在Service或Acti ...

  8. C#利用QrCode.Net生成二维码(Qr码

    http://www.cnblogs.com/Soar1991/archive/2012/03/30/2426115.html 现在网上很多应用都是用二维码来分享网址或者其它的信息.尤其在移动领域,二 ...

  9. 「小程序JAVA实战」Springboot版mybatis逆向生成工具(32)

    转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootbanmybatisnixiangshengchenggongju32/ ...

  10. 塔防游戏 Day3

    1. 添加按钮动画 选择 Button->Transition 为 Animation ,然后自定义四种状态动画即可. 2. 控制升级面板的显示和隐藏 // 升级处理 // 若点击同一炮塔,并且 ...