Codeforces 258C Little Elephant and LCM

Little Elephant and LCM

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;

int n, tot, tmp = 1, ans, a[N], b[N];
vector<int> fac[N];

int power(int a, int b) {
    if(!a) return 0;
    int ans = 1;
    while(b) {
        if(b & 1) ans = 1ll * ans * a % mod;
        a = 1ll * a * a % mod; b >>= 1;
    }
    return ans;
}

int main() {
    for(int i = 1; i < N; i++)
        for(int j = i; j < N; j += i)
            fac[j].push_back(i);
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= a[n]; i++, tot = 0, tmp = 1) {
        for(auto& t : fac[i]) b[++tot] = lower_bound(a + 1, a + 1 + n, t) - a;
        for(int i = 1; i < tot; i++) tmp = 1ll * tmp * power(i, b[i + 1] - b[i]) % mod;
        ans = (ans + 1ll * tmp * power(tot, n + 1 - b[tot]) % mod) % mod;
        ans = (ans - 1ll * tmp * power(tot - 1, n + 1 - b[tot]) % mod + mod) % mod;
    }
    printf("%d\n", ans);
    return 0;
}

/*
*/

猜你喜欢

转载自www.cnblogs.com/CJLHY/p/10390801.html