【数学】Prime-Factor Prime

article/2025/9/14 6:29:11

Prime-Factor Prime

题目描述

A positive integer is called a "prime-factor prime" when the number of its prime factors is prime. For example, 12 is a prime-factor prime because the number of prime factors of 12=2×2×3 is 3, which is prime. On the other hand, 210 is not a prime-factor prime because the number of prime factors of 210=2×3×5×7 is 4, which is a composite number.

In this problem, you are given an integer interval [l,r]. Your task is to write a program which counts the number of prime-factor prime numbers in the interval, i.e. the number of prime-factor prime numbers between l and r, inclusive.

 

输入

The input consists of a single test case formatted as follows.

l r
A line contains two integers l and r (1≤l≤r≤109), which presents an integer interval [l,r]. You can assume that 0≤r−l<1,000,000.

 

输出

Print the number of prime-factor prime numbers in [l,r].

 

样例输入

1 9

样例输出

4


 

【题解】

区间素数筛即可解决。

 

【队友代码】

 1 //
 2 //  IniAully
 3 //
 4 //#pragma GCC optimize("Ofast,no-stack-protector")
 5 //#pragma GCC optimize("O3")
 6 #pragma GCC optimize(2)
 7 #include <bits/stdc++.h>
 8 #define inf 0x3f3f3f3f
 9 #define linf 0x3f3f3f3f3f3f3f3fll
10 #define pi acos(-1.0)
11 #define nl "\n"
12 #define pii pair<ll,ll>
13 #define ms(a,b) memset(a,b,sizeof(a))
14 #define FAST_IO ios::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL)
15 using namespace std;
16 typedef long long ll;
17 const ll mod = 1e9+7;
18 ll qpow(ll x, ll y){ll s=1;while(y){if(y&1)s=s*x%mod;x=x*x%mod;y>>=1;}return s;}
19 //ll qpow(ll a, ll b){ll s=1;while(b>0){if(b%2==1)s=s*a;a=a*a;b=b>>1;}return s;}
20 inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}
21   
22 const int maxn = 1e5+5;
23 ll vis[maxn], prime[maxn], cnt;
24 void isprime()
25 {
26     memset(vis,0,sizeof(vis));
27     for(ll i=2;i<maxn;i++)
28     {
29         if(!vis[i])prime[cnt++]=i;
30         for(ll j=0;j<cnt && i*prime[j]<maxn;j++)
31         {
32             vis[i*prime[j]]=1;
33             if(i%prime[j]==0)break;
34         }
35     }
36     vis[1] = 1;
37     vis[0] = 1;
38 }
39   
40 const int N = 1e6+5;
41 ll bas[N];
42 int amo[N];
43   
44 int main()
45 {
46     int l, r;
47     isprime() ;
48   
49     scanf("%d%d",&l,&r);
50     for(int i=0;i<=r-l+5;i++) bas[i] = 1;
51     for(int i=0;i<cnt;i++)
52     {
53         int u = (l-1)/prime[i] + 1;
54         int v = r/prime[i];
55         for(int j=u;j<=v;j++)
56         {
57             int _ = j*prime[i], __=_;
58             while(_%prime[i]==0){
59                 amo[__-l+1]++;
60                 _ /= prime[i];
61                 bas[__-l+1] *= prime[i];
62             }
63         }
64     }
65     //cout<<1<<nl;
66     int ans = 0;
67     for(int i=l;i<=r;i++)
68     {
69         if(i<=3) continue;
70         //cout<<i<<" : "<<amo[i-l+1]<<nl;
71         if(bas[i-l+1] != i)amo[i-l+1] ++;
72         if(!vis[amo[i-l+1]]) ans ++;
73     }
74     cout<<ans<<nl;
75   
76     return 0;
77 }
View Code

 

转载于:https://www.cnblogs.com/Osea/p/11397653.html


http://chatgpt.dhexx.cn/article/nmNZLPho.shtml

相关文章

Prime Factory (Training, Math)

Prime Factory (Training, Math) 题目描述 Your task is simple: Find the first two primes above 1 million, whose separate digit sums are also prime. As example take 23, which is a prime whose digit sum, 5, is also prime. The solution is the concatination of t…

Fiori Fundamentals和SAP UI5 Web Components

这周有位同事邀请我给团队讲一讲SAP技术的演进历史&#xff0c;所以我准备了下面几个主题来介绍。 其中SAP的技术回顾和演进&#xff0c;我的思路就是从前后台两方面分别介绍。 我画了一张非常简单的图&#xff1a; 去年5月我写过一篇文章&#xff1a;SAP UI和Salesforce UI开…

C++Prime Plus(3)

目录 51.抽象和类52.类的使用53.对象构造54.对象析构55.const与类56.this指针57.类作用域58.运算符重载59.运算符重载的实例60.友元61.运算符重载-成员或非成员62.类的类型转换63.拷贝构造函数与赋值运算符重载64.静态数据成员65.静态成员函数 51.抽象和类 类型的构成 1.数据占…

C++Prime Plus(6)

目录 92.STL(1)容器93.STL(2)迭代器94.STL(3)函数对象95.STL(4)算法 92.STL(1)容器 标准模板库 STL&#xff08;Standard Template Library&#xff09;&#xff0c;是 C 标准库的一部分&#xff0c;不需要单独安装&#xff0c;只需要#include 头文件。STL提供了容器&#xff…

C++Prime Plus(5)

目录 85.异常(1)异常处理机制86.异常(2)exception类87.RTTI(1)88.RTTI(2)89.类型转换运算符90.string类91.智能指针 85.异常(1)异常处理机制 异常&#xff1a;运行错误&#xff08;比如无法打开文件&#xff0c;动态内存申请失败&#xff09;&#xff0c;导致程序无法继续正常…

SAP Fiori学习笔记

资料链接&#xff1a;有些是需要自带梯子的哦&#xff5e; Fiori Design Guidelines​experience.sap.com戴团长&#xff1a;SAP Fiori Design​zhuanlan.zhihu.com如何评价 SAP Fiori Design Guidelines&#xff1f;​www.zhihu.comhttps://mp.weixin.qq.com/s?__bizMzIyNjY…

C++Prime Plus(2)

目录 21.for循环(1)22.for循环(2)23.while循环24.do while循环25.二维数组与嵌套循环26.if语句27.逻辑表达式28.条件表达式29.switch语句30.文件概念31.文本文件的输入输出32.函数详解(1)回顾33.函数详解(2)参数传递34.函数详解(3)数组传递35.函数详解(4)C风格字符串36.递归概念…

C++Prime Plus(1)

目录 1.C简介2.程序生成&#xff08;创建源码&#xff0c;编译和链接&#xff09;3.进入C4.C语句5.函数入门6.整型7.char&#xff0c;bool&#xff08;小整数&#xff09;8.const与符号常量9.浮点数10.算术表达式11.数组12.C风格字符串13.C风格字符串14.结构15.指针16.动态内存…

C++Prime Plus(7)

目录 96.输入输出概述97.使用cout输出(1)ostream基本功能98.使用cout输出(2)格式化输出99.使用cin输入100.文件(1)简单的文件IO101.文件打开的进一步讨论102.二进制文件访问103.随机读写 96.输入输出概述 C的输入输出是由库iostream中提供的一组类实现的&#xff1b; 流 C把输…

E-Prime软件包及安装

E-Prime软件包及安装 1 版本问题2 安装过程3 注意事项4 唠唠叨叨 Hello&#xff0c; 这里是行上行下&#xff0c;我是喵君姐姐~ 众所周知&#xff0c;E-Prime是实验设计的执行者。 当我们提出一个想法&#xff0c;则需要一个具体的软件来实现它。 而E-Prime相对于Matlab和Py…

Prime Sample

又发现了个框架 但没有代码啊~~ 还是搬来了&#xff0c;重要样本关注机制&#xff0c;一种新颖的目标检测框架 上论文 论文地址&#xff1a; https://arxiv.org/pdf/1904.04821.pdf 在目标检测框架中&#xff0c;平等对待所有样本并以平均性能最大化目标是一种常见的范例。在…

Prime Factors

此题需要使用到质因子分解的算法&#xff0c;可以参考以下链接&#xff1a; https://blog.csdn.net/qq_42410605/article/details/100150140 题目描述&#xff1a; Given any positive integer N,you are supposed to find all of prime factors,and write them in the form…

SpringBoot实现分布式锁

SpringBoot实现分布式锁 先了解一下线程数&#xff1a; 线程锁 线程锁&#xff1a;主要用来给类&#xff0c;方法&#xff0c;代码加锁&#xff0c;当某个方法或者某块代码使用synchronize关键字来修饰&#xff0c;那么在同一时刻最多只能有一个线程执行该代码&#xff0c;如…

深入理解ConcurrentHashMap原理分析以及线程安全性问题

在之前的文章提到ConcurrentHashMap是一个线程安全的&#xff0c;那么我么看一下ConcurrentHashMap如何进行操作的。 ConcurrentHashMap与HashTable区别&#xff1f; HashTable put()源代码 我们来看一下put 操作&#xff1a; 方法体 被 同步锁标记&#xff0c;由于同步锁的…

Redis分布式锁到底安全吗?

若有收获,请记得分享和转发哦 这篇文章我想和你聊一聊&#xff0c;关于 Redis 分布式锁的「安全性」问题。 Redis 分布式锁的话题&#xff0c;很多文章已经写烂了&#xff0c;我为什么还要写这篇文章呢&#xff1f; 因为我发现网上 99% 的文章&#xff0c;并没有把这个问题真正…

Java线程安全

前段时间有测试一个后端对账单和话单采集服务,在测试过程中有涉及到数据库读写逻辑和并发的场景,所以结合经验针对系统技术架构设计了部分并发场景结合数据库读写时可能出现的一些问题的用例,也确实出现了一些测试环境容易忽视,线上环境确确实实可能出现的问题,当然最后还是得到…

线程安全之 - ThreadLocal

ThreadLocal的底层原理 ThreadLocal是Java中所提供的线程本地存储机制&#xff08;线程内共享&#xff09;&#xff0c;可以利⽤该机制将数据缓存在某个线程内部&#xff0c; 该线程可以在任意时刻、任意⽅法中获取缓存的数据&#xff1b;ThreadLocal底层是通过ThreadLocalMap…

线程锁(ReentrantLock、synchronized)为何不能用作分布式锁

为什么使用分布式锁 分布式锁实现目前有三种&#xff1a; 数据库乐观锁&#xff1b;ZooKeeper的分布式锁;Redis的分布式锁&#xff1b; 在以前单体架构Web应用场景下&#xff0c;我们可以使用ReentrantLock或synchronized进行上锁&#xff0c;保证资源安全&#xff0c;现如今大…

Redis分布式锁真的安全吗?

大家好&#xff0c;今天我们来聊一聊Redis分布式锁。 首先大家可以先思考一个简单的问题&#xff0c;为什么要使用分布式锁&#xff1f;普通的jvm锁为什么不可以&#xff1f; 这个时候&#xff0c;大家肯定会吧啦吧啦想到一堆&#xff0c;例如java应用属于进程级&#xff0c;…

ThreadLocal能解决线程安全问题?胡扯!本文教你正确的使用姿势【享学Java】

跟对领导很重要&#xff1a;愿意教你的&#xff0c;并且放手让你做的领导要珍惜。 目录 前言正文ThreadLocal是什么&#xff1f;ThreadLocal怎么用&#xff1f;局限性InheritableThreadLocal向子线程传递数据开源框架使用示例 ThreadLocal不能解决共享变量的线程安全问题Thread…