C. Anniversary
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 %I64dspecifier.
Output
Print a single integer — the residue from dividing the sought greatest common divisor by m.
Examples
input
Copy
10 1 8 2
output
Copy
3
input
Copy
10 1 8 3
output
Copy
1
題意:給你四個數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。
斐波那契數列有一個性質:
因此我們只需要從l~r中挑gcd最大的k個數即可。設最大gcd爲x,那麼答案就是Fx,並且有
這種形式還記得嗎?取值只有
種,對於上式,我們只需要枚舉x=1~sqrt(r),然後直接判斷x和r/x是否合法就行了。
x可能高達1e12,因此我們用矩陣快速冪計算出答案。不要忘了對m取模!!!
代碼:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=1e6+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
ll mo;
ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
ll l,m,r,k,n;
ll ans,tmp;
ll find(ll mid)
{
if((r/mid)-((l-1)/mid)>=k) return 1;
return 0;
}
struct node
{
ll n,m,a[7][7];
void init()
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++) a[i][i]=1;
}
};
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++)
{
ll x=0;
for(int k=0;k<m;k++)
{
x+=aa.a[i][k]*bb.a[k][j]%mo;
x=(x+mo)%mo;
}
cc.a[i][j]=x;
}
}
return cc;
}
node power(node a,ll m)
{
node d;
d.n=d.m=a.n;
d.init();
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;
}
ll poww(ll a,ll n)
{
ll sum=1;
while(n)
{
if(n&1) sum=sum*a%mo;
n>>=1;
a=a*a%mo;
}
return sum;
}
int main()
{
int T,cas=1;
scanf("%lld%lld%lld%lld",&m,&l,&r,&k);
ll as=1;
mo=m;
for(ll i=1;i*i<=r;i++)
{
if(find(i)) as=max(as,i);
if(find((r/i))) as=max(as,(r/i));
}
if(as<=2) printf("%lld\n",1LL%mo);
else
{
n=as;
node x;
x.n=2;x.m=2;
memset(x.a,0,sizeof(x.a));
x.a[0][0]=1;
x.a[0][1]=1;
x.a[1][0]=1;
x.a[1][1]=0;
node ans=power(x,n-2);
node tmp;
tmp.n=2;
tmp.m=1;
memset(tmp.a,0,sizeof(tmp.a));
tmp.a[0][0]=1;
tmp.a[1][0]=1;
ans=mul(ans,tmp,2,2,1);
ll sum=(ans.a[0][0]+mo)%mo;
printf("%lld\n",sum);
}
return 0;
}