您的位置:首页 > 其它

TOJ 3349 Counting Divisor / 素数筛选法

2013-10-23 20:15 211 查看

Counting Divisor

时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte



描述

If an integer A can divide integer B exactly, then A can be called a divisor of B.

Now there are N (1 <= N <= 100,000) integesr, for each of these number, please count the number of divisor from the rest N-1 integers.

输入

The first line of input data is an integer N, the following N rows have a positive integer,and each integer isn't above 1,000,000。

输出

According to the input order output every integer in the rest N - 1 integer number of divisor.

样例输入

5
2
1
2
3
4


样例输出

2
0
2
1
3


这题真的做了很久 求助了很多人才过的 感谢学长 学友

主要是利用了素数筛选那样的方法

自己TLE无数

看代码 一看就恍然大悟

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int cnt[1000010];
int map[1000010];
int a[100010];
int main()
{
	int n,i,j,max = 0;
	scanf("%d",&n);
	for(i = 1;i <= n; i++)
	{
		scanf("%d",&a[i]);
		cnt[a[i]]++;
		if(max < a[i])
			max = a[i];
	}
	for(i = 1;i <= max; i++)
	{
		if(cnt[i])
		{
			for(j = i + i; j <= max; j += i)//和素数筛选差不多 赞一个 
			{
				map[j] += cnt[i];
			}
		}
	}
	for(i = 1;i <= n; i++)
	{
		if(cnt[a[i]] > 1)
			printf("%d\n",map[a[i]] + cnt[a[i]] - 1);
		else
			printf("%d\n",map[a[i]]);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: