【BZOJ】1607: [Usaco2008 Dec]Patting Heads 轻拍牛头(特殊的技巧)

http://www.lydsy.com/JudgeOnline/problem.php?id=1607

其实题目描述不清楚,应该是 别人拿的数能整除自己拿的数

数据范围很大,n<=100000,a[i]<=1000000,暴力一个个统计显然不行

而我们发现,每个a[i]相同的牛,他们拍的应该是相同的数量,那么我们只需要算出一个a[i]需要拍的就行了

而a[j]能被拍当且只当a[j]|a[i],所以我们只要累计每一个a[i],然后当a[i]被拍后,所有的a[j]|a[i]的a[j]都累计a[i]的数量即可

(但是我觉得如果这样做还是会tle啊。。。虽然说题解都是这样,我觉得应该离散后做(好像还是很大))

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=100005, M=1000005;
int a[N], can[M], s[M], n;

int main() {
	read(n);
	int mx=0;
	for1(i, 1, n) {
		read(a[i]);
		++can[a[i]];
		mx=max(mx, a[i]);
	}
	for1(i, 1, mx)
		if(can[i])
			for(int j=i; j<=mx; j+=i)
				s[j]+=can[i];
	for1(i, 1, n) printf("%d\n", s[a[i]]-1);
	return 0;
}

 

 


 

 

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.

posted @ 2014-09-01 13:54  iwtwiioi  阅读(379)  评论(0编辑  收藏  举报