第一次接触莫比乌斯这么高端的东西,心里很是激动。
莫比乌斯是照着ACdreamer大神的博客学习的:点击打开链接
本来想写一篇详细的博客纪念一下第一次写莫比乌斯,但是鉴于时间太晚(从23:00一直TLE到01:28),这里就给大家推荐一篇讲得很好的博客:点击打开链接
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int MOD = 1e9+7;
const int MAX_N = 1e5+5;
const int INF = 0x3f3f3f3f;
int prime[MAX_N], cnt;
int vis[MAX_N];
int u[MAX_N]; // 莫比乌斯函数
// 快速线性筛法求莫比乌斯函数
void Mobius()
{
memset(vis, 0, sizeof(vis));
u[1] = 1;
cnt = 0;
for (LL i = 2; i < MAX_N; i++)
{
if (!vis[i])
{
prime[cnt++] = i;
u[i] = -1;
}
for (LL j = 0; j < cnt && i * prime[j] < MAX_N; j++)
{
vis[i * prime[j]] = 1;
if (i % prime[j])
u[i * prime[j]] = -u[i];
else
{
u[i * prime[j]] = 0;
break;
}
}
}
}
LL mod_pow(LL a, LL n, LL mod)
{
LL res = 1;
while (n)
{
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main()
{
//freopen("test.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin.sync_with_stdio(false);
Mobius();
int T, Case = 1;
int A[MAX_N], n;
cin >> T;
while (T--)
{
cin >> n;
int Max = -1, Min = INF;
int pre[2 * MAX_N] = {0};
for (int i = 1; i <= n; i++)
{
cin >> A[i];
Max = max(Max, A[i]);
Min = min(Min, A[i]);
pre[A[i]]++;
}
for (int i = 1; i < 2 * MAX_N; i++)
pre[i] += pre[i - 1];
LL ans = 0;
for (int i = 2; i <= Min; i++)
{
LL tmp = 1;
for (int j = 1; j * i <= Max; j++)
tmp = (tmp * mod_pow(j, pre[i * (j + 1) - 1] - pre[i * j - 1], MOD)) % MOD;
ans = (ans - u[i] * tmp + MOD) % MOD;
}
cout << "Case #"<< Case++ << ": " << ans << endl;
}
return 0;
}