BZOJ_1607_ [Usaco2008_Dec]_Patting_Heads_轻拍牛头_(筛数)
描述
http://www.lydsy.com/JudgeOnline/problem.php?id=1607
给出一组n个数,求每个数能被多少个其他的数整除.
分析
暴力一点的做法就是每个数去筛它的倍数,这样外层循环是n.优化一点的做法就是统计每个数字i出现过的次数cnt[i],从数字1到出现过的最大数字Max,每次筛的时候加上数字i的贡献cnt[i],这样外层循环是小于等于n的(只要有重复数字就小于n).
不过好像有更厉害的做法,没有在网上找到= =.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=1e5+5; 5 int n,Max; 6 int a[maxn],cnt[maxn*10],ans[maxn*10]; 7 8 int main(){ 9 scanf("%d",&n); 10 for(int i=1;i<=n;i++){ 11 scanf("%d",&a[i]); 12 cnt[a[i]]++; 13 Max=max(Max,a[i]); 14 } 15 for(int i=1;i<=Max;i++) 16 if(cnt[i]) 17 for(int j=i;j<=Max;j+=i) 18 ans[j]+=cnt[i]; 19 for(int i=1;i<=n;i++) printf("%d\n",ans[a[i]]-1); 20 return 0; 21 }
1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1667 Solved: 884
[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.
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.
0
2
1
3
OUTPUT DETAILS:
The first cow pats the second and third cows; the second cows pats no cows;
etc.
HINT
Source