题目链接:

link,点击这里喵。

前置知识:

【模板】线性筛素数欧拉函数,点击这里喵。

题意简述:

给定整数 $l,r,k$,求出 $[l,r]$ 中有多少个整数不断对自己取欧拉函数刚好 $k$ 次结果为 $1$。

思路:

看眼数据范围,$10^{10}$ 的量级显然不容我们每次暴力,故考虑预处理 $\varphi(i),can(i,k),sum(i,k)$。定义如其名。

做法:

1. 预处理 $\varphi(i)$:

这里采用线性筛,这里在注释中简要说明,证明过程详见:筛法求欧拉函数

void get_phi(const int n){
bool isprime[n];
memset(isprime,1,sizeof(isprime));
phi[1]=1;isprime[0]=isprime[1]=0;
vector<int> prime;
for(int i=2;i<n;++i){
if(isprime[i]){phi[i]=i-1;prime.push_back(i);} //当 i 为质数时,小于她且与之互质的显然有 (i-1) 个
for(auto e: prime){
if(e*i>=n){break;}
isprime[e*i]=0;
if(i%e==0){phi[i*e]=phi[i]*e;break;} //当 i 中含有 e 这个质因子时,phi(i * e) = phi(i) * e
phi[i*e]=phi[i]*phi[e]; //当 i 中不含有 e 这个质因子时,phi(i * e) = phi(i) * (e-1)
}
}
}

2. 预处理 $can(i,k)$ 以及 $sum(i,k)$:

唯一要注意的点是,是恰好 $k$ 次,所以尽管 $\varphi(1)=1$,仍然不能无限套娃,这点在求 $sum(i,k)$ 时一定要注意。

sum[1][0]=can[1][0]=1;
for(int i=2;i<N;++i){
for(int e=0;e<21;++e){
can[i][e]=can[phi[i]][e-1];
sum[i][e]=sum[i-1][e]+can[i][e];
}
}

小贴士:

请万分注意 $sum(i,k)$ 的求值过程。

时间复杂度分析:

预处理 $O(kn)$,查询 $O(T)$,总体之间复杂度 $O(kn)$。

代码:

#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <string.h>
#include <vector>
#define lnt long long
#define dnt double
#define inf 0x3f3f3f3f
using namespace std;
int xx;char ff,chh;inline int read(){
xx=ff=0;while(!isdigit(chh)){if(chh=='-'){ff=1;}chh=getchar();}
while(isdigit(chh)){xx=(xx<<1)+(xx<<3)+chh-'0';chh=getchar();}return ff? -xx: xx;
}
const int N=1e6+2e4;
int phi[N];
int can[N][22],sum[N][22];
void get_phi(const int);
int main(){
get_phi(N);
sum[1][0]=can[1][0]=1;
for(int i=2;i<N;++i){ //从 2 开始避免无线套娃
for(int e=0;e<21;++e){
can[i][e]=can[phi[i]][e-1];
sum[i][e]=sum[i-1][e]+can[i][e];
}
}
int G=read();
while(G--){
int l=read(),r=read(),k=read();
printf("%d\n",sum[r][k]-sum[l-1][k]);
} return 0;
}
void get_phi(const int n){
bool isprime[N];
memset(isprime,1,sizeof(isprime));
phi[1]=1;isprime[0]=isprime[1]=0;
vector<int> prime;
for(int i=2;i<n;++i){
if(isprime[i]){phi[i]=i-1;prime.push_back(i);}
for(auto e: prime){
if(e*i>=n){break;}
isprime[e*i]=0;
if(i%e==0){phi[i*e]=phi[i]*e;break;} //线性筛减免时间复杂度的核心操作
phi[i*e]=phi[i]*phi[e];
}
}
}

公式真的没有中文标点了

题解:SP22382 ETFD - Euler Totient Function Depth的更多相关文章

  1. 模反元素 RSA Euler's totient function

    https://baike.baidu.com/item/模反元素/20417595 如果两个正整数a和n互质,那么一定可以找到整数b,使得 ab-1 被n整除,或者说ab被n除的余数是1.这时,b就 ...

  2. Euler's totient function

    https://en.wikipedia.org/wiki/Euler's_totient_function counts the positive integers up to a given in ...

  3. 【LeetCode】【Python题解】Single Number &amp; Maximum Depth of Binary Tree

    今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...

  4. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  5. RSA算法原理与加密解密 求私钥等价求求模反元素 等价于分解出2个质数 (r*X+1)%[(p-1)(q-1)]=0

    Rsapaper.pdf http://people.csail.mit.edu/rivest/Rsapaper.pdf [概述Abstract 1.将字符串按照双方约定的规则转化为小于n的正整数m, ...

  6. 2018HDU多校训练-3-Problem D. Euler Function

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6322 Problem Description In number theory, Euler's toti ...

  7. HDU 5597 GTW likes function 打表

    GTW likes function 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5596 Description Now you are give ...

  8. Project Euler 516 5-smooth totients (数论)

    题目链接: https://projecteuler.net/problem=516 题目: \(5\)-smooth numbers are numbers whose largest prime ...

  9. 2018 Multi-University Training Contest 3(部分题解)

    Problem F. Grab The Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Ja ...

  10. hdu-5597 GTW likes function(欧拉函数+找规律)

    题目链接: GTW likes function Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 131072/131072 K (J ...

随机推荐

  1. STM32 CubeMX 学习:有关说明

    背景 STM32 是我以前学过的,而很久没有整理过的.因为之前学习的时间比较早,再加上各种资料要么不成熟,要么不齐全:再加上自己一开始没有比较完善的学习经验:以至于我的学习并不扎实. 趁着 STM 的 ...

  2. Linux 提权-Docker 容器

    本文通过 Google 翻译 Docker Breakout – Linux Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校正及个别注释补充. ...

  3. python基础-列表list [ ]

    列表的定义和操作 列表的特性: 元素数量 支持多个 元素类型 任意 下标索引 支持 重复元素 支持 可修改性 支持 数据有序 是 使用场景 可修改.可重复的 一批数据记录场景 # 定义一个列表list ...

  4. .NET Framework 4 请求https接口

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  5. HTTP 和 HTTPS,为什么HTTPS安全?

    HTTP协议通常承载与 TCP协议之上,在HTTP和TCP之间添加一个安全协议层(SSL或TSL),这个时候,就成了我们常说的HTTPS 默认HTTP的端口号为80,HTTPS的端口号为443 因为网 ...

  6. 【java深入学习第1章】深入探究 MyBatis-Spring 中 SqlSession 的原理与应用

    前言 在使用 MyBatis 进行持久层开发时,通常会与 Spring 框架集成,以便更好地管理事务和依赖注入.在 MyBatis-Spring 集成中,SqlSession 是一个非常重要的概念.本 ...

  7. Swift开发基础08-高阶函数

    高阶函数是指接受其它函数作为参数,或者返回其它函数的函数.Swift 提供了许多内置的高阶函数,这些函数在处理集合类型数据(如数组.集合等)时尤其有用.常见的高阶函数包括 map.filter.red ...

  8. 题解:AT_abc357_f [ABC357F] Two Sequence Queries

    题意 维护一个数据结构,支持两个数列的区间求和,和查询区间内两数列各元素积的和. 分析 线段树万岁! 这道题要维护两个序列,所以线段树中要同时存储两个区间和.但还要在维护一个信息,是该区间内两序列元素 ...

  9. Node.js 处理 File

    Node.js 处理 File fs 模块 常规使用 运用递归遍历目录树 创建文件和目录 读写文件 path 模块 对于 file 的理解,此处 fs 模块 Node.js 提供了处理文件系统的内置模 ...

  10. 学习笔记--Java中this关键字

    Java中this关键字 关于Java语言中的this关键字 this 是一个关键字,翻译为:这个 this 是一个引用,一个变量,this变量中保存的内存地址指向自身 每一个对象都有自己的this, ...