您的位置:首页 > 其它

【codeforces26A】Almost Prime

2015-09-04 20:19 417 查看
A. Almost Prime

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A number is called almost prime if it has exactly two distinct prime divisors. For example, numbers 6, 18, 24 are almost prime, while 4, 8, 9, 42 are not. Find the amount of almost prime numbers which are between 1 and n, inclusive.

Input

Input contains one integer number n (1 ≤ n ≤ 3000).

Output

Output the amount of almost prime numbers between 1 and n, inclusive.

Sample test(s)

Input

10

Output

2

Input

21

Output

8

题意:输出小于等于n的有且只有两个质因数的个数。

解题思路:本来看数据3000,就想着纯打表,后来看看,也还有蛮多,就老老实写吧,先用prime数组存1-1500之间的素数,然后判断小于等于n之间的满足题意的个数。

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
using namespace std;
int prime[250];
int is_prime(int n)
{
for(int i=2;i<=n/2;i++)
if(n%i==0)
return 0;
return 1;
}
bool is_alpme(int i)
{
bool flag=false;
int pos=0,nbr=i;
while(1)
{
if(prime[pos]>nbr)
return false;
if(flag==false && nbr%prime[pos]==0)
{
flag=true;
while(nbr%prime[pos]==0)
nbr=nbr/prime[pos];
}
else if(flag==true && nbr%prime[pos]==0)
{
nbr=nbr/prime[pos];
while(nbr%prime[pos]==0)
nbr=nbr/prime[pos];
if(nbr==1)
return true;
else
return false;
}
if(prime[pos]==1499)
return false;
pos++;
}
}
int main()
{
int cnt=0;
int res=0;
int n;
for(int i=2;i<1500;i++)
if(is_prime(i)){
prime[res++]=i;
}
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
if(is_alpme(i))
cnt++;
}
printf("%d\n",cnt);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: