BZOJ 4146: [AMPPZ2014]Divisors
Description
给定一个序列\(a_1,a_2,...,a_n\)。求满足\(i\neq j\)且\(a_i \mid a_j\)的二元组\((i,j)\)的个数,\(n,a_i\leqslant 2\times 10^6\)。
Solution
欧拉筛.
一个欧拉筛统计一下...没了...相同的数是\(c_i\times (c_i-1)\)
Code
/************************************************************** Problem: 4146 User: BeiYu Language: C++ Result: Accepted Time:7984 ms Memory:9104 kb ****************************************************************/ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2000500; inline int in(int x=0,char s=getchar()) { while(s>'9'||s<'0')s=getchar(); while(s>='0'&&s<='9')x=x*10+s-'0',s=getchar();return x; } int n; int a[N]; ll ans; int main() { n=in(); for(int i=1,x;i<=n;i++) x=in(),a[x]++; for(int i=1;i<=2000000;i++) { ans+=1ll*a[i]*(a[i]-1); for(int j=i+i;j<=2000000;j+=i) ans+=1ll*a[i]*a[j]; }printf("%lld\n",ans); return 0; }