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 ...
随机推荐
- CentOS7.6安装PM2(Npm方式全局安装)
安装前提: 1. node环境 2. npm 安装开始: 第一步:全局安装,npm install -g pm2 第二步: 保存当前进程状态,pm2 save 第三步: 生成开机自启动服务,pm2 s ...
- 关于where和having的直观理解
一,查询区别 where是对前面select的字段没有要求,直接查询库表的 having是对前面的select的字段有要求,字段已经select出来的 可以用having进行处理 select id, ...
- NIO编程介绍
代码: package bhz.nio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio. ...
- subprocess in python3.5
subprocess 该子模块允许你创建新的流程,连接到它们的输入/输出/错误管道,并获取他们的返回值.该模块打算替换多个旧的模块和功能:os.system 和 os.spawn * 使用sub ...
- 【转】iphone - ios app maximum memory budget
https://stackoverflow.com/questions/5887248/ios-app-maximum-memory-budget device: (crash amount/tota ...
- 【299】◀▶ IDL - LIST 函数
list 函数用来创建一个新的 list.list 可以包含不同的数据类型,包括数据.数组.结构体.指针.对象以及其他的 list 或者 哈希表. 序号 类名称 功能说明 语法 & 举 ...
- (转) iphone开发资源汇总
如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://g ...
- 【 Makefile 编程基础之…
本站文章均为李华明Himi原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/gcc-makefile/766.html 概述: ...
- Elasticsearch-PHP 安装
安装 Elasticsearch-PHP只有三个要求你需要担心: PHP 5.3.9 或更高版本(查看更多信息) Composer ext-curl: Libcurl的PHP扩展 其它的依赖会通过Co ...
- 蒟蒻LQL的博客
这里是蒟蒻LQL的博客!!! 一枚水的不能再水的弱校ACMer···· 可能会在这写一些题解或者别的什么乱七八糟的··· 可能大概没什么人看,就当错题本好了o(* ̄▽ ̄*)ブ 因为太弱了难免有错误!发 ...