GYM103688E Exclusive Multiplication - 莫比乌斯反演 -
题目链接:https://codeforces.com/gym/103688/problem/E
题解:
辣鸡题解,好多地方有错,遂自己写一个
// by SkyRainWind
#include <bits/stdc++.h>
#define mpr make_pair
#define debug() cerr<<"Yoshino\n"
#define pii pair<int,int>
#define pb push_back
using namespace std;
typedef long long ll;
typedef long long LL;
const int inf = 1e9, INF = 0x3f3f3f3f, maxn = 2e5+5, mod = 1e9+7;
int notpm[maxn], pm[maxn], pcnt=0, md = 2e5;
int mu[maxn];
int f[maxn], a[maxn], G[maxn];
int pw(int x,int y){
if(!y)return 1;
if(y==1)return x;
int mid=pw(x,y>>1);
if(y&1)return 1ll*mid*mid%mod*x%mod;
return 1ll*mid*mid%mod;
}
void xxs(){
notpm[1] = 1;
mu[1] = 1;
for(int i=2;i<=maxn-5;i++){
if(!notpm[i]){
pm[++ pcnt] = i;
mu[i] = -1;
}
for(int j=1;pm[j]*i<=maxn-5;j++){
notpm[i*pm[j]] = 1;
if(i%pm[j] == 0){
mu[i*pm[j]] = 0;
break;
}
mu[i*pm[j]] = -mu[i];
}
}
}
int mp[maxn], n;
signed main(){
xxs();
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
f[a[i]] = 1;
int tmp = a[i];
for(int j=2;j*j<=a[i];j++){
if(tmp%j == 0){
int cc = 0;
while(tmp%j == 0){
cc ^= 1;
tmp /= j;
}
if(cc)f[a[i]] *= j;
}
}
if(tmp != 1)f[a[i]] *= tmp;
++ mp[f[a[i]]];
}
for(int i=1;i<=md;i++){
G[i] = 0;
for(int j=i;j<=md;j+=i){
if(mp[j]){
G[i] += 1ll * mp[j] * j % mod,
G[i] %= mod;
}
}
G[i] = 1ll*G[i]*G[i] % mod;
}
int ans = 0;
for(int i=1;i<=md;i++){
int res = 0;
for(int j=i;j<=md;j+=i){
(res += 1ll*G[j]*mu[j/i]%mod) %= mod;
}
res = 1ll * res * pw(1ll*i*i%mod,mod-2) % mod;
(ans += res) %= mod;
}
ans -= n;
ans = 1ll * ans * pw(2, mod-2) % mod;
ans = (ans%mod + mod) % mod;
printf("%d\n",ans);
return 0;
}
分类:
数论 / 莫比乌斯反演
, 题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示