列表

详情


NC54564. Special number

描述

        HH likes math very much. Today he found some numbers that are special. These special numbers are defined as "numbers that can be divisible by less than 3 positive integers." Now HH wants to ask you how many special numbers there are in the interval [L,R].

输入描述

The first line contains two integers L,R  — the numbers of is the interval.

( 0 <= L <= R <=1e5 )

输出描述

Output the number of special numbers in the interval [L,R].

示例1

输入:

3 6

输出:

2

说明:

Only 3, 5 in 3, 4, 5, 6 are special numbers

示例2

输入:

8 8

输出:

0

说明:

8 is not special number

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

pypy3 解法, 执行用时: 501ms, 内存消耗: 22548K, 提交时间: 2023-05-07 19:18:25

l,r=map(int,input().split())
cnt=0
import math
if l==0:
    cnt=-1
else:
    cnt=0
for i in range(l,r+1):
    k=0
    for j in range(1,int(math.sqrt(i))+1):
        if i%j==0:
            k+=1
            if i//j!=j:
                k+=1
    if k<3:
        cnt+=1
print(cnt)

C++14(g++5.4) 解法, 执行用时: 92ms, 内存消耗: 496K, 提交时间: 2020-02-21 20:48:45

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int l,r,ans=0;
	cin>>l>>r;
	for(int i=l;i<=r;i++)
	{
		int cnt=0;
		for(int j=1;j*j<=i;j++)
			if(i%j==0)
				cnt++;
		if(cnt<=1&&cnt!=0)
			ans++;
	}
	cout<<ans<<endl;
	return 0;
}

C++ 解法, 执行用时: 577ms, 内存消耗: 512K, 提交时间: 2021-10-18 23:29:30

#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
	int l,r,i,a,j,k=0;
	scanf("%d %d",&l,&r);
	for(a=l;a<=r;a++)
	{
		for(i=2;i<=a/2;i++)
		{
			if(a%i==0) break;
		}
		if(i>a/2&&a>=1) k++;
	}
	printf("%d",k);

}

C(clang 3.9) 解法, 执行用时: 849ms, 内存消耗: 460K, 提交时间: 2020-01-11 12:57:46

#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
	int l,r,i,a,j,k=0;
	scanf("%d %d",&l,&r);
	for(a=l;a<=r;a++){
		for(i=2;i<=a/2;i++){
			if(a%i==0)break;
		}
		if(i>a/2&&a>=1)k++;
	}
	printf("%d",k);
}

Python3 解法, 执行用时: 464ms, 内存消耗: 4608K, 提交时间: 2023-04-09 11:13:52

L,R=map(int,input().split())
count=0
for i in range(L,R+1):
    if(i==0):
        continue
    for j in range(2,int(i**0.5)+1):
        if(i%j==0):
            break
    else:
        count+=1
print(count)

上一题