題目:http://www.lydsy.com/JudgeOnline/problem.php?id=1607
1607: [Usaco2008 Dec]Patting Heads 輕拍牛頭
Time Limit: 3 Sec Memory Limit: 64 MB
Submit: 1654 Solved: 874
[Submit][Status][Discuss]
Description
今天是貝茜的生日,爲了慶祝自己的生日,貝茜邀你來玩一個遊戲.
貝茜讓N(1≤N≤100000)頭奶牛坐成一個圈.除了1號與N號奶牛外,i號奶牛與i-l號和i+l號奶牛相鄰.N號奶牛與1號奶牛相鄰.農夫約翰用很多紙條裝滿了一個桶,每一張包含了一個獨一無二的1到1,000,000的數字.
接着每一頭奶牛i從柄中取出一張紙條Ai.每頭奶牛輪流走上一圈,同時拍打所有編號能整除在紙條上的數字的牛的頭,然後做回到原來的位置.牛們希望你幫助他們確定,每一頭奶牛需要拍打的牛.
Input
第1行包含一個整數N,接下來第2到N+1行每行包含一個整數Ai.
Output
第1到N行,每行的輸出表示第i頭奶牛要拍打的牛數量.
Sample Input
5 //有五個數,對於任一個數來說,其它的數有多少個是它的約數
2
1
2
3
4
INPUT DETAILS:
The 5 cows are given the numbers 2, 1, 2, 3, and 4, respectively.
Sample Output
2
0
2
1
3
OUTPUT DETAILS:
The first cow pats the second and third cows; the second cows pats no cows;
etc.
思路:篩數,統計更新I的倍數的約數;
代碼:
#include<iostream>
#include<stdio.h>
#define maxn 100005
#define N 1000005
using namespace std;
int n;
int a[maxn];
int cnt[N];
int ans[N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
cnt[a[i]]++;
}
for(int i=1;i<=N;i++)
if(cnt[i])
{
for(int j=i;j<=N;j+=i) ans[j]+=cnt[i];
}
for(int i=1;i<=n;i++)
{
printf("%d\n",ans[a[i]]-1);
}
}