TOJ 1868.Count the factors(埃式筛法)

题目链接:http://acm.tju.edu.cn/toj/showp1868.html


1868.    Count the factors
Time Limit: 0.5 Seconds    Memory Limit: 65536K
Total Runs: 2691    Accepted Runs: 855



The Problem

Write a program, that computes the number of different prime factors in a positive integer.

The Input

The input tests will consist of a series of positive integers. Each number is on a line on its own. The maximum value is 1000000. The end of the input is reached when the number 0 is met. The number 0 shall not be considered as part of the test set.

Output

The program shall output each result on a line by its own, following the format given in the sample output.

Sample Input

289384
930887
692778
636916
747794
238336
885387
760493
516650
641422
0

Sample Output

289384 : 3
930887 : 2
692778 : 5
636916 : 4
747794 : 3
238336 : 3
885387 : 2
760493 : 2
516650 : 3
641422 : 3


Source: UVA
Submit   List    Runs   Forum   Statistics

埃式筛法的题,只不过时间要求比较紧,必须得记住有多少个质数,要不然就会超时,一开始偷懒。。结果超时啦。。


#include <stdio.h>
#include <string.h>
#define Max 1000000+5
using namespace std;
int prime[Max];
bool isprime[Max];
int main(){
	int n,sum,count=0;
	memset(isprime,true,sizeof(isprime));
	isprime[0]=isprime[1]=false;
	for(int i=2;i<=Max/2;i++)
		if(isprime[i]){
			for(int j=i<<1;j<=Max;j+=i)
				isprime[j]=false;
			prime[count++]=i;
		}
	while(scanf("%d",&n)&&n>0){
		printf("%d : ",n);
		sum=0;
		for(int i=0;i<count&&prime[i]<=n;++i){
			if(n%prime[i]==0)
				sum++;
			while(n%prime[i]==0)
				n/=prime[i];	
			}
		printf("%d\n",sum);
	}
} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值