CodeForces 227E Anniversary (斐波那契的高妙性质+矩阵快速幂)
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
10 1 8 2
3
10 1 8 3
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 (斐波那契的高妙性质+矩阵快速幂)的更多相关文章
- POJ3070 斐波那契数列递推 矩阵快速幂模板题
题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...
- 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 ...
- D - Frog and Portal (利用斐波那契数列的性质)
题目链接:https://cn.vjudge.net/contest/270201#problem/D 具体思路:利用斐波那契数列的性质,斐波那契数列可以构成任何正整数,所以按照顺序减下去肯定能减到0 ...
- codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质
E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...
- HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- 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 ...
- P1962 斐波那契数列-题解(矩阵乘法扩展)
https://www.luogu.org/problemnew/show/P1962(题目传送) n的范围很大,显然用普通O(N)的递推求F(n)铁定超时了.这里介绍一种用矩阵快速幂实现的解法: 首 ...
- HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)
M斐波那契数列 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submi ...
- 关于斐波拉契数列(Fibonacci)
斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10 ...
随机推荐
- Android屏幕相关概念和适配方法
参考文档: 1.http://blog.csdn.net/carson_ho/article/details/51234308(略有修改) 2.http://www.cnblogs.com/cheng ...
- EDAS字体嵌入问题解决方法
提交IEEE EDAS文章时出现:“The paper PDF file cannot be accepted: Publishers require that PDF fonts are embed ...
- clipboard使用总结
官方网站:https://clipboardjs.com/ 使用总结:http://blog.csdn.net/hry2015/article/details/70941912
- GNU/Linux操作系统总览
计算机科学本科的专业课包括高等数学.离散数学.模拟电子技术.数字电子技术.微机原理.汇编语言原理.高级程序语言.操作系统原理.高级编译原理.嵌入式原理.网络原理.计算机组成与结构等诸多科目.GNU计算 ...
- Redis实战——简单介绍
出自:https://www.cnblogs.com/moonlightL/p/7364107.html Redis简单介绍 Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能, ...
- 如何安装和使用Karma-Jasmine
注意:本文中出现的资料链接.karma的插件安装等,均可能需要翻$墙后才能正确执行. Jasmine是一个JavaScript的测试工具,在Karma上运行Jasmine可完成Javascript的自 ...
- kibana-4.6.3-linux-x86_64.tar.gz的安装(图文详解)(升级)
前期博客 kibana-4.6.3-linux-x86_64.tar.gz的下载(图文详解) 因为,我的机器情况是如下: 1.上传 [hadoop@master app]$ rz [hadoop@m ...
- spring 提供的属性值拷贝工具类
当需要把一个原生的类中属性拷贝到扩展类中时,使用以下类很方便:
- 201671010140. 2016-2017-2 《Java程序设计》java学习第十四周
java学习第十四周 本周,主要精力放在了第十二章swing用户界面组件知识的学习,swing是一个用于开发Java应用程序用户界面的开发工具包.它以抽象窗口工具包(AWT)为基础使跨 ...
- Oracle 日志报错导致的 “没有登录” 问题
遇到的问题是日志空间满了,导致Oracle无法登陆,但用PL/SQL登录仅会提示“没有登录” # 首先检查日志空间是否满了,并删除过期日志 rman target sysdba/password@or ...