题意翻译
今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏。贝茜让 N (1≤N≤10^5) 头奶牛坐成一个圈。除了 1 号与 N 号奶牛外,i号奶牛与 i−1 号和 i+1 号奶牛相邻。N 号奶牛与 1 号奶牛相邻。农夫约翰用很多纸条装满了一个桶,每一张包含了一个不一定是独一无二的 1 到 10^6 的数字。接着每一头奶牛 i从柄中取出一张纸条 Ai。每头奶牛轮流走上一圈,同时拍打所有手上数字能整除在自己纸条上的数字的牛的头,然后做回到原来的位置。牛们希望你帮助他们确定,每一头奶牛需要拍打的牛。
输入输出样例
输入 #1
5
2
1
2
3
4
输出 #1
2
0
2
1
3
说明/提示
The 5 cows are given the numbers 2, 1, 2, 3, and 4, respectively.
The first cow pats the second and third cows; the second cows pats no cows; etc.
分析:
- 类似筛法,其实觉得就想看题写代码,加点小主意。
- 不能拍自己,最后处理记得-1.
- 实际a[i],对于最后的结果的ans[j]的影响就是1.
代码:
1 #include<bits/stdc++.h> 2 #define inf 0x7fffffff 3 using namespace std; 4 inline int read() 5 { 6 int x=0;char ch=getchar(); 7 while(ch<'0'||ch>'9')ch=getchar(); 8 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 9 return x; 10 } 11 int n,mx; 12 int a[100005],cnt[1000005],s[1000005]; 13 int main() 14 { 15 n=read(); 16 for(int i=1;i<=n;i++) 17 { 18 a[i]=read(); 19 cnt[a[i]]++;//类似桶 统计相同数字出现的次数 20 mx=max(a[i],mx);//找出最大的a[i]. 21 } 22 for(int i=1;i<=mx;i++) 23 if(cnt[i])//若cnt[i]不为0 ,证明有i的存在 24 for(int j=i;j<=mx;j+=i)//注意是j=j+i; 25 s[j]+=cnt[i];//统计符合要求的个数 26 for(int i=1;i<=n;i++) 27 printf("%d ",s[a[i]]-1); //输出结果 28 return 0; 29 }
虽然渺小,但不放弃