这题不能一个一个数过去。如果每头牛都是同一个数字,那一共要10^10次。
所以我们要找一个递推的办法,有点像dp的思路,从小到大遍历牛的号码,对于每一号码看看他的倍数是多少这些牛都能拍。这些数字能构成一个DAG,就能递推了,从小到大遍历就是为了满足拓扑序。
实际操作的时候我们是离散化处理的,开一个10^6的数组来记录每头牛被拍的次数,如果这头牛没出现过就跳过。还要一个数组统计同一个号码出现的次数。最后别忘了自己不能拍自己。
```
#include “bits/stdc++.h”
using namespace std;
typedef long long LL;
define INF 0x3f3f3f3f
const int MOD = 1e9+7;
typedef pair[HTML_REMOVED] PII;
const int N = 100010;
const int M = 1000010;
int n;
int a[N], cnt[M], dp[M];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
cnt[a[i]]++;
}
for (int i = 1; i < M; i++) {
if (!cnt[i])
continue;
for (int j = i; j < M; j += i) {
dp[j] += cnt[i];
}
}
for (int i = 0; i < n; i++) {
cout << dp[a[i]] - 1 << endl;
}
return 0;
}
```