E. Anniversary

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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

inputCopy

10 1 8 2

outputCopy

3

inputCopy

10 1 8 3

outputCopy

1

题意很简单,就是给你第L到第R个斐波那契额数列,让你选K个求K个数的最大公约数模MOD;

在这里首先要明确性质,斐波那契数列第K个数与第S个数的最大公约数是,第N个斐波那契数,N为S与K的最大公约数。

所以这个题转化为先求N选K的最大公约数+矩阵快速幂求斐波那契,N选K的数的最大公约数,因为K是连续的,所有有这个性质,每N个数一定有一个N的倍数,这是后应该判断K与区间长度的关系,再判断L与R,与N的关系,选取最大值即为K组的最大公约数。

带入最大公约数到矩阵快速幂即可。

矩阵快速幂 https://blog.csdn.net/weixin_43627118/article/details/97394804

#include<bits/stdc++.h>
using namespace std;
int MOD=1e8+5;
const int maxn=2; //定义方阵的阶数
struct JZ{ long long m[maxn][maxn]; };//定义maxn阶方阵
JZ muti(JZ a,JZ b,int mod);
JZ quick_mod(JZ a,long long k,int mod);
bool chk(long long u, long long L, long long R, long long K) {
if(u == 0) {
return 0;
}
return (R / u) - ((L - 1) / u) >= K;
}
int main()
{
long long L,R,K;
cin >> MOD >> L >> R >> K;
long long te = 0;
for(long long i = 1; i * i <= R; i++) {
if(chk(i, L, R, K)) {
te = max(te, i);
}
}
for(long long bs = 1; bs * bs <= R; bs++) {
if(chk(R / bs, L, R, K)) {
te = max(te, R / bs);
}
}
for(long long bs = 1; bs * bs <= L - 1; bs++) {
if((chk(((L - 1) / bs) - 1, L, R, K))) {
te = max(te, ((L - 1) / bs) - 1);
}
}
JZ demo,ans;
demo.m[0][0]=0; demo.m[0][1]=1; demo.m[1][0]=1; demo.m[1][1]=1;
ans=quick_mod(demo,te,MOD);
cout<<ans.m[1][0]<<endl;
}
JZ muti(JZ a,JZ b,int mod)
{
JZ temp;
memset(temp.m,0,sizeof(temp.m));
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++){
for(int k=0;k<maxn;k++)
{
temp.m[i][j]+=(long long) a.m[i][k]*b.m[k][j]%mod;
}
temp.m[i][j]%=mod;
}
return temp;
}
JZ quick_mod(JZ a,long long k,int mod)
{
JZ ans;
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
ans.m[i][j]=(i==j);
while(k) {
if(k &1) ans =muti(ans,a,mod);
a = muti(a,a,mod);
k >>=1;
}
return ans;
}

codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质的更多相关文章

  1. POJ 3070(求斐波那契数 矩阵快速幂)

    题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace ...

  2. CodeForces 227E Anniversary (斐波那契的高妙性质+矩阵快速幂)

    There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathemati ...

  3. UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)

    题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...

  4. poj3070矩阵快速幂求斐波那契数列

      Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13172   Accepted: 9368 Desc ...

  5. HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. poj3070 求斐波那契数列第n项 ——矩阵快速幂

    题目:http://poj.org/problem?id=3070 用矩阵快速幂加速递推. 代码如下: #include<iostream> #include<cstdio> ...

  7. Codeforces 719E [斐波那契区间操作][矩阵快速幂][线段树区间更新]

    /* 题意:给定一个长度为n的序列a. 两种操作: 1.给定区间l r 加上某个数x. 2.查询区间l r sigma(fib(ai)) fib代表斐波那契数列. 思路: 1.矩阵操作,由矩阵快速幂求 ...

  8. HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理

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

  9. 斐波那契数列第N项f(N)[矩阵快速幂]

    矩阵快速幂 定义矩阵A(m*n),B(p*q),A*B有意义当且仅当n=p.即A的列数等于B的行数. 且C=A*B,C(m*q). 例如: 进入正题,由于现在全国卷高考不考矩阵,也没多大了解.因为遇到 ...

随机推荐

  1. Unity - 旋转方法

    前言 本文梳理了Unity中常用的旋转方法,涉及两大类:Transform.Quaternion. Transform 类 Rotate() 此方法重载多,易理解,在连续动态旋转中较为常用. /* o ...

  2. Python 1基础语法二(标识符、关键字、变量和字符串)

    一.标识符 标识符就是程序员自己命名的变量名.名字需要有见名知义的效果,不要随意起名 :比如 a=1 a是个变量,a这个变量名属于标识符 1 company = '小米 2 employeeNum = ...

  3. Python Requests-学习笔记(7)-Cookies

    如果某个响应中包含一些Cookie,你可以快速访问它们: url = 'http://example.com/some/cookie/setting/url' r = requests.get(url ...

  4. 【java 数据结构】还不会二叉树?一篇搞定二叉树

    二叉树是我们常见的数据结构之一,在学习二叉树之前我们需要知道什么是树,什么是二叉树,本篇主要讲述了二叉树,以及二叉树的遍历. 你能get到的知识点? 1.树的介绍 2.二叉树的介绍 3.二叉树遍历的四 ...

  5. Tomcat5的web应用启动顺序详解

    Tomcat5的web应用启动顺序详解 [收藏此页] [打印]   作者:佚名  2007-07-17 内容导航: 第1页   [IT168技术文档]摘要: 应用Tomcat对于我们来讲实在是司空见惯 ...

  6. MyBatis通用 Mapper4使用小结

    官网地址: http://www.mybatis.tk/ https://gitee.com/free 1.使用springboot,添加依赖: 使用tk的mybatis后不需要引用官方原生的myba ...

  7. AJ学IOS 之第一次打开Xcode_git配置,git简单学习

    AJ分享,必须精品 一:错误 当第一次打开Xcode我们进行commit操作的时候会报错: The working copy “测试” failed to commit files. * Please ...

  8. 试验使用t检验

    官方解释 Excel中使用T.TEST函数 T.TEST(array1,array2,tails,type) Array1      必需.第一个数据集. Array2      必需.第二个数据集. ...

  9. 绝地求生模拟登陆!高难度JS解密教程,Python高级爬虫开发,

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取htt ...

  10. 智能可视化搭建系统 Atom 服务架构演变

    作者:凹凸曼 - Manjiz Atom 是什么?Atom 是集结业内各色资深电商行业设计师,提供一站式专业智能页面和小程序设计服务的平台.经过 2 年紧凑迭代,项目越来越庞大,需求不断变更优化,内部 ...