传送门

考场上魔改了一下线性筛,觉得要筛到 \(\frac{R}{2}\) 就没让它跑

其实正解就是这样,只不过由于接下来类似埃氏筛的过程只要筛到根号就行了

  • 线性筛有的时候其实并不需要筛到 \(\frac{n}{2}\),如果接下来需要枚举倍数,注意可能只需要枚举到根号就行了

发现 \(R\) 的范围很大,但 \(R-L\) 的范围有限

而 \(L\) 的范围只有 \(1e7\),可以筛出质数来,再用类似埃氏筛的方法筛掉 \([L, R]\) 内的类质数

然后枚举一遍统计个数就好了

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 10000010
#define ll long long
#define reg register int
#define rll register long long
//#define int long long char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline ll read() {
ll ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
} ll l, r, k;
int pri[N], pcnt, ans;
bool npri[N]; namespace force{
ll ans;
void solve() {
for (ll i=l; i<=r; ++i) {
for (ll j=2; j<=min(i-1, k); ++j)
if (!(i%j)) goto jump;
//printf("%lld,%lld\n", i, (ans^=i));
//printf("%lld ", i);
ans^=i;
jump: ;
}
//printf("\n");
printf("%lld\n", ans);
exit(0);
}
} namespace task1{
void solve() {
for (reg i=2,limr=r,limk=k; i<=limr; ++i) {
if (i<=limk && !npri[i]) pri[++pcnt]=i;
for (reg j=1; j<=pcnt&&1ll*i*pri[j]<=r; ++j) {
npri[i*pri[j]]=1;
if (!(i%pri[j])) break;
}
}
for (reg i=l,limr=r; i<=limr; ++i)
if (!npri[i]) ans^=i; //, cout<<i<<' '; cout<<endl;
printf("%d\n", ans);
exit(0);
}
} namespace task2{
void solve() {
ll ans=0;
for (rll i=l; i<=r; ++i) ans^=i;
printf("%lld\n", ans);
exit(0);
}
} namespace task3{
bool nspr[N];
void solve() {
for (reg i=2,lim=min((ll)(sqrt(r)),k); i<=lim; ++i) {
if (!npri[i]) pri[++pcnt]=i;
for (reg j=1; j<=pcnt&&1ll*i*pri[j]<=lim; ++j) {
npri[i*pri[j]]=1;
if (!(i%pri[j])) break;
}
}
for (reg i=1; i<=pcnt; ++i) {
//cout<<"i: "<<i<<' '<<pri[i]<<endl;
for (rll j=max((l-1)/pri[i]+1,2ll),lim=r/pri[i]; j<=lim; ++j) {
//cout<<j*pri[i]<<' '<<j*pri[i]-l<<endl;
nspr[j*pri[i]-l]=1; //, cout<<j*pri[i]<<endl;
}
}
ll ans=0;
//for (int i=0; i<=100; ++i) cout<<nspr[i]<<' '; cout<<endl;
for (reg i=0,lim=r-l+1; i<lim; ++i) if (!nspr[i]) ans^=(l+i);
printf("%lld\n", ans);
exit(0);
}
} signed main()
{
l=read(); r=read(); k=read();
//force::solve();
if (k==1) task2::solve();
else if (r<=1000) force::solve();
else if (r<=(ll)(1e7)) task1::solve();
else task3::solve(); return 0;
}

题解 Prime的更多相关文章

  1. 【数论】8.30题解-prime素数密度 洛谷p1835

    prime 洛谷p1835 题目描述 给定区间[L, R](L <= R <= 2147483647, R-L <= 1000000),请计算区间中 素数的个数. 输入输出 输入 两 ...

  2. 【题解】UVA10140 [Prime Distance]

    [题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...

  3. 【题解】CF45G Prime Problem

    [题解]CF45G Prime Problem 哥德巴赫板子题? \(\frac{n(n+1)}{2}\)若是质数,则不需要分了. 上式 若是奇数,那么拆成2和另一个数. 上式 若是偶数吗,直接\(O ...

  4. CF912E Prime Gift题解(搜索+二分答案)

    CF912E Prime Gift题解(搜索+二分答案) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1314956 洛谷题目链接 $     $ CF题目 ...

  5. HDU 1016 Prime Ring Problem 题解

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  6. poj2689 Prime Distance题解报告

    题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...

  7. 题解-hdu2866 Special Prime

    Problem hdu-2866 题意:求区间\([2,L]\)有多少素数\(p\)满足\(n^3+pn^2=m^3\),其中\(n,m\)属于任意整数 Solution 原式等价于\(n^2(p+n ...

  8. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  9. LintCode 896. Prime Product 简明题解

    Given a non-repeating prime array arr, and each prime number is used at most once, find all the prod ...

随机推荐

  1. Redhat 6.9 升级SSH到OpenSSH_8.6p1完整文档

    这个文章是转载,原文连接在这个:https://www.cnblogs.com/xshrim/p/6472679.html 这个问题遇到过,下面可以解决 ----------------------- ...

  2. 从2021强网杯的一道题学习docx文件操作

    [强网先锋]寻宝 啊对就是这道题,大佬们都贼快,菜如我还得慢慢整 key1 大佬们都一笔带过,哎,虽然简单,但是也别这么虐我们啊 我来简单写一下吧 <?php header('Content-t ...

  3. 怎么实现系统调用wait和exit

    例程 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/wai ...

  4. Maven工程 报 Diamond types are not supported at language level '5'

    Maven工程 报 Diamond types are not supported at language level '5' 出现这种信息,一般表示的是你的language level(IDEA下J ...

  5. Leetcode:559. N叉树的最大深度

    Leetcode:559. N叉树的最大深度 Leetcode:559. N叉树的最大深度 Talk is cheap . Show me the code . /* // Definition fo ...

  6. Linux 安装 Nodejs 的两种方式

    Linux 安装 Nodejs 的两种方式 目录 Linux 安装 Nodejs 的两种方式 一.压缩包安装 Nodejs 二.源码编译安装 Nodejs 一.压缩包安装 Nodejs 下载 Node ...

  7. SpringBoot+Redis 实现消息订阅发布

    什么是 Redis Redis 是一个开源的使用 ANSI C语言编写的内存数据库,它以 key-value 键值对的形式存储数据,高性能,读取速度快,也提供了持久化存储机制. Redis 通常在项目 ...

  8. 第八篇 -- 对数据库mysql进行连接并压测

    参考链接:https://blog.csdn.net/laofashi2015/article/details/81296929 工具:mysql-8.0.12-winx64,apache-jmete ...

  9. 开机时自动启动的AutoHotkey脚本 2019年10月09日

    ;;; 开机时自动启动的AutoHotkey脚本 2019年10月09日;; http://www.autoahk.com/archives/16600; https://www.cnblogs.co ...

  10. UI_UE在线就业-笔记

    UI设计 一.字体设计的应用范围 Logo设计.平面海报.包装设计.banner设计.APP引导页.UI图标.UI界面.影视设计 二.字体的分类 衬线字体和非衬线字体之分 区别在于笔画开始与结尾地方有 ...