51nod1355:斐波那契的最小公倍數(數論)

題面
題意給出n個a,問LCM{ f(a) },f爲斐波那契數。

知乎靠譜的題解

記住這兩個路人性質就好
①容斥求LCM

lcm{S}=TS,Tgcd{T}(1)|T|+1

②對多個數也成立
gcd(fn,fm)=fgcd(n,m)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
#define mmst(a, b) memset(a, b, sizeof(a))
#define mmcp(a, b) memcpy(a, b, sizeof(b))

typedef long long LL;

const LL mo=1e9+7;
const int N=1010000;

int n;
LL ans=1,f[N],g[N];
bool b[N];

LL cheng(LL a,LL b)
{
    LL res=1;
    for(;b;b>>=1,a=a*a%mo)
    if(b&1)
    res=res*a%mo;
    return res;
}

int main()
{
    g[1]=f[1]=1;
    for(int i=2;i<N;i++)
    f[i]=(f[i-1]+f[i-2])%mo,g[i]=f[i];

    for(int i=2;i<N;i++)
    {
        LL inv=cheng(g[i],mo-2);
        for(int j=i+i;j<N;j+=i)
        g[j]=g[j]*inv%mo;
    }

    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        b[x]=1;
    }

    for(int i=1;i<N;i++)
    {
        bool ok=0;
        for(int j=i;j<N;j+=i)
        if(b[j])
        {
            ok=true;
            break;
        }
        if(ok)
        ans=ans*g[i]%mo;
    }
    cout<<ans<<endl;

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章