bzoj 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 (筛法)
http://www.lydsy.com/JudgeOnline/problem.php?id=1607
题意:n个数,求对于每个数来说,其他n-1个数中是它约数的数的个数。
思路:类似筛法,从小到大处理,数i对其所有倍数的数的答案有cnt[i]的贡献 。最后记得把自己是自己的约数的情况减掉。
/* ***********************************************
Author :111qqz
Created Time :2016年02月28日 星期日 01时06分35秒
File Name :code/bzoj/1607.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=1E5+7;
 7int n;
 8int a[N];
 9int cnt[N*10];
10int ans[N*10];
11int main()
12{
13	#ifndef  ONLINE_JUDGE 
14//	freopen("code/in.txt","r",stdin);
15  #endif
	ms(cnt,0);
1	cin>>n;
2	int mx = -1;
3	for ( int i = 0 ; i < n ; i++)
4	{
5	    scanf("%d",&a[i]);
6	    cnt[a[i]]++;
7	    mx = max(mx,a[i]);
8	}
1	for ( int i = 1 ; i <= mx; i++)
2	    if (cnt[i])                       //类似筛法,对所有倍数都有贡献
3		for ( int j = 1 ; j*i <= mx ; j++)
4		    ans[j*i]+=cnt[i];
	for ( int i = 0  ;i < n ; i++)
	    printf("%d\n",ans[a[i]]-1);//减去自己是自己约数的情况
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}