首页 > 代码库 > loj6102 「2017 山东二轮集训 Day1」第三题

loj6102 「2017 山东二轮集训 Day1」第三题

传送门:https://loj.ac/problem/6102

【题解】

贴一份zyz在知乎的回答吧  https://www.zhihu.com/question/61218881

其实是经典问题

技术分享
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int N = 5e4 + 10, M = 1e6 + 10, F = 1e6;
const int mod = 1e9 + 7;

int n, a[N], f[F + 5], g[F + 5];
bool h[F + 5];

inline int pwr(int a, int b) {
    int ret = 1;
    while(b) {
        if(b&1) ret = 1ll * ret * a % mod;
        a = 1ll * a * a % mod;
        b >>= 1;
    }
    return ret;
}

int main() {
    cin >> n;
    for (int i=1; i<=n; ++i) scanf("%d", a+i);
    f[0] = 0, f[1] = g[1] = 1; 
    for (int i=2; i<=F; ++i) {
        f[i] = f[i-1] + f[i-2];
        if(f[i] >= mod) f[i] -= mod;
        g[i] = f[i];
    }
    for (int i=1; i<=F; ++i) {
        int t = pwr(g[i], mod-2);
        for (int j=i+i; j<=F; j+=i)
            g[j] = 1ll * g[j] * t % mod;
    }
    for (int i=1; i<=n; ++i) h[a[i]] = 1;
    int ans = 1;
    for (int i=1; i<=F; ++i) {
        for (int j=i+i; j<=F; j+=i) h[i] |= h[j];
        if(h[i]) {
//            cout << i << ‘ ‘ << g[i] << endl;
            ans = 1ll * ans * g[i] % mod;
        }
    }
    cout << ans;
    return 0;
}
View Code

 

loj6102 「2017 山东二轮集训 Day1」第三题