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. 数据库设计不推荐使用Bool类型

    参见博文:http://blog.csdn.net/zhang_xinxiu/article/details/8521673

  2. Java静态代码块

    static{ //代码 } 在加载类的时候,会执行静态代码块-->非静态代码块--->构造函数 http://www.cnblogs.com/panjun-Donet/archive/2 ...

  3. 通过devtools在centos系统中启用高版本的gcc

    C++11出来好久了,现在还是使用c++03的,需要在centos6.6的系统上实现gcc的升级,又不想自己编译代码. 于是选用了devtoolsset系列,安装脚本如下 安装脚本如下 functio ...

  4. ios的xxxAppDelegate.h分析

    #import "BIDAppDelegate.h" #import "BIDViewController.h" @implementation BIDAppD ...

  5. 【原】Coursera—Andrew Ng机器学习—Week 11 习题—Photo OCR

    [1]机器学习管道 [2]滑动窗口 Answer:C ((200-20)/4)2 = 2025 [3]人工数据 [4]标记数据 Answer:B (10000-1000)*10 /(8*60*60) ...

  6. Mysql安装配置,修改初试密码。

    绿色版本,解压缩 D:\Software\mysql-advanced-5.6.18-winx64 my-default.ini 改名my.ini my.ini内容如下 # For advice on ...

  7. Python与Go插入排序

    #!/usr/bin/env python # -*- coding: utf-8 -*- # 插入排序 # 时间复杂度 O(n^2) import time def logger(func): st ...

  8. 【jdbc】【c3p0】c3p0三种配置方式【整理】

    c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...

  9. GLSL in ShaderLab

    [Syntax] However, use of raw GLSL is only recommended for testing, or when you know you will only ta ...

  10. Excel VBA入门(四)流程控制2-循环控制

    所谓循环控制,即在循环执行一段代码,用于完成一些重复性任务. VBA中的循环控制语句主要有3种:for.while.loop.对于大多数人来说,for的使用频率最高,而我个人也觉得for是最为灵活的, ...