TOJ 3349 Counting Divisor / 素数筛选法

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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值