hdu 5072 Coprime (容斥)
Posted uid001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 5072 Coprime (容斥)相关的知识,希望对你有一定的参考价值。
大意: 给定长$n$的序列$a$, 元素互不相同, 求有多少个三元组$(x,y,z)$满足两两互质或两两不互质.
考虑计算不合法情况. 若互质连一条白边, 不互质连一条黑边, 那么一个不合法的三元环一定有两个角是异色的, 合法三元环三个角都是同色的, 所以只要数出异色角即可.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ‘ ‘ #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘,‘;hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;} //head const int N = 1e5+10; int n,a[N],s[N],vis[N]; int p[100],cnt; ll ret; void dfs(int d, int num, int z) { if (d>cnt) ret+=z*s[num]; else dfs(d+1,num,z),dfs(d+1,num*p[d],-z); } ll solve(int x) { cnt = 0; for (int i=2; i*i<=x; ++i) if (x%i==0) { p[++cnt] = i; while (x%i==0) x/=i; } if (x>1) p[++cnt] = x; ret = 0, dfs(1,1,1); return ret; } void work() { scanf("%d",&n); REP(i,1,n) scanf("%d",a+i),vis[a[i]]=1; REP(i,1,N-1) { s[i] = 0; for (int j=i; j<N; j+=i) s[i]+=vis[j]; } ll ans = 0; REP(i,1,n) { if (a[i]!=1) { ll A = solve(a[i]), B = n-A-1; ans += A*B; } vis[a[i]] = 0; } printf("%lld ",(ll)n*(n-1)*(n-2)/6-ans/2); } int main() { int t; scanf("%d",&t); while (t--) work(); }
以上是关于hdu 5072 Coprime (容斥)的主要内容,如果未能解决你的问题,请参考以下文章
CodeForces 803F Coprime Subsequences 莫比乌斯,容斥