当前位置: 代码迷 >> 综合 >> Codeforces Round #140 (Div. 1) C. Anniversary (两个定理的应用(数论+矩阵快速幂)
  详细解决方案

Codeforces Round #140 (Div. 1) C. Anniversary (两个定理的应用(数论+矩阵快速幂)

热度:44   发布时间:2024-01-12 20:18:54.0

题目链接:

http://codeforces.com/problemset/problem/226/C

C. 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 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 %I64dspecifier.

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

 首先知道两个定理,算是规律吧

1. F_{GCD(n,m)}=GCD(F_n , F_m)

2   \left \lfloor \frac{x}{i} \right \rfloor = k   表是1-x中能够整除i的数有k个i

题意:

给你四个数m,l,r,k (1 ≤ m ≤ 1e9; 1 ≤ l < r ≤ 1e12; 2 ≤ k ≤ r - l + 1),让你从斐波那契数列的第l项,l+1项,...,第r项中挑k项,使这k项的gcd最大(设最大为x)。输出x%m。

F_{GCD(n,m)}=GCD(F_n , F_m)

因此我们只需要从l~r中挑gcd最大的k个数即可。设最大gcd为x,那么答案就是Fx,并且有

\left \lfloor \frac{r}{x} \right \rfloor - \left \lfloor \frac{l-1}{x} \right \rfloor >= k  找最大的x

对于上式,我们只需要枚举x=1~sqrt(r),然后直接判断x和r/x是否合法就行了。

x可能高达1e12,因此我们用矩阵快速幂计算出答案。不要忘了对m取模!!

This is the  code:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{int ret=0, flag=0;char ch;if((ch=getchar())=='-')flag=1;else if(ch>='0'&&ch<='9')ret = ch - '0';while((ch=getchar())>='0'&&ch<='9')ret=ret*10+(ch-'0');return flag ? -ret : ret;
}
LL l,r,k;
LL mod;
bool Find(LL mid)
{if(r/mid-(l-1)/mid >= k)return true;return false;
}
struct node
{LL n,m,a[7][7];node(){memset(a,0,sizeof(a));}
};
node mul(node aa, node bb, LL n, LL m, LL k)
{node cc;cc.n=n;cc.m=k;for(int i=0;i<n;i++)for(int j=0;j<k;j++)for(int k=0;k<m;k++)cc.a[i][j]=(cc.a[i][j]+aa.a[i][k]*bb.a[k][j]%mod)%mod;return cc;
}
node power(node a, LL m)
{node d;d.n=d.m=a.n;for(int i=0;i<d.n;++i)d.a[i][i]=1;while(m){if(m&1)d=mul(d,a,d.n,d.m,a.m);m>>=1;a=mul(a,a,a.n,a.n,a.n);}return d;
}
int main()
{scanf("%lld%lld%lld%lld",&mod,&l,&r,&k);LL maxx=1;for(LL i=1; i*i<=r;++i){if(Find(i))maxx=max(maxx,i);if(Find(r/i))maxx=max(maxx,r/i);}if(maxx<=2)//表示斐波那契的前两项printf("%lld\n",1LL%mod);else{node x;//系数矩阵x.n=2;x.m=2;x.a[0][0]=1;x.a[0][1]=1;x.a[1][0]=1;x.a[1][1]=0;node tmp;//初始值矩阵tmp.n=2;tmp.m=1;tmp.a[0][0]=1;tmp.a[1][0]=1;node ans=power(x,maxx-2);//计算系数ans=mul(ans,tmp,2,2,1);printf("%lld\n",ans.a[0][0]);}return 0;
}

 

  相关解决方案