P2926

[USACO08DEC]Patting Heads S

直接倍数筛即可。

时间复杂度 $O(n\ln n)$。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;

const ll N=1e5,M=1e6;

ll n;

ll f[M+5],ans[M+5],a[N+5];

inline ll read() {
ll ret=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-f;ch=getchar();}
while(ch>='0'&&ch<='9') {ret=(ret<<3)+(ret<<1)+ch-'0';ch=getchar();}
return ret*f;
}

void write(ll x) {
static char buf[22];static ll len=-1;
if(x>=0) {
do{buf[++len]=x%10+48;x/=10;}while(x);
}
else {
putchar('-');
do{buf[++len]=-(x%10)+48;x/=10;}while(x);
}
while(len>=0) putchar(buf[len--]);
}

void writeln(ll x) {
write(x);putchar('\n');
}

int main() {

n=read();

for(ll i=1;i<=n;i++) {
a[i]=read();f[a[i]]++;
}

for(ll i=1;i<=M;i++) {
if(f[i]) {
for(ll j=i;j<=M;j+=i) {
ans[j]+=f[i];
}
ans[i]-=1;
}
}

for(ll i=1;i<=n;i++) {
writeln(ans[a[i]]);
}


return 0;
}