[ABC238G] Cubic?
Problem Statement
Given a sequence $A$ of $N$ numbers, answer the following $Q$ questions.
- In the $i$-th question, you are given integers $L_i$ and $R_i$. Is $A_{L_i} \times A_{L_i+1} \times \dots \times A_{R_i}$ a cubic number?
Here, a positive integer $x$ is said to be a cubic number when there is a positive integer $y$ such that $x=y^3$.
Constraints
- All values in input are integers.
- $1 \le N,Q \le 2 \times 10^5$
- $1 \le A_i \le 10^6$
- $1 \le L_i \le R_i \le N$
Input
Input is given from Standard Input in the following format:
$N$ $Q$
$A_1$ $A_2$ $\dots$ $A_N$
$L_1$ $R_1$
$L_2$ $R_2$
$\vdots$
$L_Q$ $R_Q$
Output
Print $Q$ lines.
The $i$-th line should contain Yes if, in the $i$-th question, $A_{L_i} \times A_{L_i+1} \times \dots \times A_{R_i}$ is a cubic number, and No otherwise.
The checker is case-insensitive; output can be either uppercase or lowercase.
Sample Input 1
8 5
7 49 30 1 15 8 6 10
1 2
2 3
4 4
5 8
3 8
Sample Output 1
Yes
No
Yes
No
Yes
- For the first question, $7 \times 49 = 343$ is a cubic number.
- For the second question, $49 \times 30 = 1470$ is not a cubic number.
- For the third question, $1$ is a cubic number.
- For the fourth question, $15 \times 8 \times 6 \times 10 = 7200$ is not a cubic number.
- For the fifth question, $30 \times 1 \times 15 \times 8 \times 6 \times 10 = 216000$ is a cubic number.
一个有亿种方法的题目。
什么情况下乘积为立方数?当且仅当分解后所有的素数的指数都是3的倍数。 \(A_i\le 10^6\),所以先预处理出每一种数的各种质因子。
法一:莫队
这就不用说了吧。加入时枚举每个质因子,维护有多少个质因子的指数模3余0。复杂度 \(O(n\sqrt{n}logn)\),3秒钟已经是能过得了。
法二:普通哈希
给每个质数分配一个随机权值 \(g_i\),如果前 \(i\) 个数质数 \(i\) 指数 \(p_i\) 模 3 为 \(k_i\),那么定义 \(hash(1,n)=\sum\limits_{j=1}^{10^6}g_ik_i\)。处理出前缀 \(h_i=hash(1,i)\),因为 \(h_i\) 到 \(h_{i+1}\) 会变化的就是在每个因数中加了或减去几个随机权值。若 \([l,r]\) 符合条件,那么 \(h_{l-1}=h_r\)。判断即可。
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5,P=998244353;
int n,q,a[N],l,r,pri[N],c[N],op;
long long s[N],f[N];
vector<int>g[N];
int main()
{
srand(time(0));
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=1;i<N;i++)
f[i]=1LL*rand()*rand()%P;
for(int i=2;i<N;i++)
{
if(!pri[i])
{
g[i].push_back(i);
for(int j=2;j*i<N;j++)
g[j*i].push_back(i),pri[j*i]=1;
}
}
for(int i=1;i<=n;i++)
{
s[i]=s[i-1];
for(int j=0;j<g[a[i]].size();j++)
{
int k=a[i],cnt=0;
while(k%g[a[i]][j]==0)
k/=g[a[i]][j],++cnt;
op=((c[g[a[i]][j]]+cnt)%3-c[g[a[i]][j]]%3);
c[g[a[i]][j]]+=cnt;
(s[i]+=(1LL*f[g[a[i]][j]]*op%P+P)%P)%=P;
}
}
while(q--)
{
scanf("%d%d",&l,&r);
if(s[r]==s[l-1])
printf("Yes\n");
else
printf("No\n");
}
}
[ABC238G] Cubic?的更多相关文章
- 3DShader之立方体环境映射(cubic environment mapping)
前面讲了球形环境映射,然而目前采用更多的是立方体环境映射.国际惯例:上图先: 1.反射: 2.折射 3.fresnel(反射+折射) 4.色散 好了,大概讲下原理, 立方体纹理我就不多讲了,它以一个3 ...
- BITMAP图片压缩算法三则--bilinear、nearest、cubic
原文:http://blog.chinaunix.net/uid-253932-id-3037805.html 工作需要,要弄截图且缩小.截图倒是好说,WIN API可以搞定,但是缩小且尽量不失真,这 ...
- 泡泡一分钟:Cubic Range Error Model for Stereo Vision with Illuminators
Cubic Range Error Model for Stereo Vision with Illuminators 带有照明器的双目视觉的三次范围误差模型 "链接:https://pan ...
- cubic与spline插值点处的区别
cubic与spline都是Matlab的三次样条插值法,但是它们在插值点处仍然有着很微妙的区别,这个区别说明不了两种方法的好坏,只能根据实际情况进行合理筛选.以一维插值为例: clc clear % ...
- UVA-1604 Cubic Eight-Puzzle (双向BFS+状态压缩+限制搜索层数)
题目大意:立体的八数码问题,一次操作是滚动一次方块,问从初始状态到目标状态的最少滚动次数. 题目分析:这道题已知初始状态和目标状态,且又状态数目庞大,适宜用双向BFS.每个小方块有6种状态,整个大方格 ...
- tcp cubic代码分析
/* * TCP CUBIC: Binary Increase Congestion control for TCP v2.3 * Home page: * http://netsrv.csc.ncs ...
- hdu 6216 A Cubic number and A Cubic Number【数学题】
hdu 6216 A Cubic number and A Cubic Number[数学] 题意:判断一个素数是否是两个立方数之差,就是验差分.. 题解:只有相邻两立方数之差才可能,,因为x^3-y ...
- 从CUBIC/BBR的TCP ACK失速说起
上周有同事问,延迟ACK到底对应用层会产生什么后果,我也不知道该如何作答,于是丢了一个链接: TCP之Delay ACK在Linux和Windows上实现的异同-Linux的自适应ACK: 是的,这是 ...
- HDU 6216 A Cubic number and A Cubic Number【数学思维+枚举/二分】
Problem Description A cubic number is the result of using a whole number in a multiplication three t ...
- 2017青岛网络赛1011 A Cubic number and A Cubic Number
A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/3276 ...
随机推荐
- AI绘画StableDiffusion:云端在线版免费使用笔记分享-Kaggle版
玩AI绘画(SD),自己电脑配置不够?今天给大家介绍一下如何baipiao在线版AI绘画StableDiffusion. Kaggle 是世界上最大的数据科学社区,拥有强大的工具和资源,可帮助您实现数 ...
- Iphone通过ssh进行访问
Iphone通过usb进行ssh访问文件系统 在公司里wifi很不给力,而我又想通过ssh访问我的iphone,进行一些权限访问,这时我们该 itunnel_mux_rev71这个工具可以帮我们的忙 ...
- Dami 基于事件总线的本地过程调用框架(首次发版)
Dami,专为本地多模块之间通讯解耦而设计(尤其是未知模块.隔离模块.领域模块).零依赖,特适合 DDD. 特点 结合 Bus 与 RPC 的概念,可作事件分发,可作接口调用,可作异步响应. 支持事务 ...
- Linux离线安装Mysql-5.7
1.背景描述 在真实业务场景下,Linux服务器一般位于内网,所以无法直接访问互联网资源: 特别是安装数据库的Linux服务器,在网络方面的管控只会更加严格: 因此,需要提前下载好相关资源,再传输到内 ...
- Node.js 20 —— 几个令人大开眼界的特性
前言:欢迎来到 Node.js 20 Node.js 20 已经发布,带来了创新和激动人心的新时代.这个开创性的版本于2023年4月18日首次亮相,并将在2023年10月发布长期支持(LTS)版本,并 ...
- MySQL——MySQL面试题
文章目录 数据库基础知识 为什么要使用数据库 什么是SQL? 什么是MySQL? 数据库三大范式是什么 mysql有关权限的表都有哪几个 MySQL的binlog有有几种录入格式?分别有什么区别? 数 ...
- 起风了,NCC 云原生项目孵化计划
时间回到 2016 年,彼时 .NET Core 1.0 刚刚发布 1.0 版本,我跟几位好友共同发起 .NET Core 中文学习组(.NET Core China Studying Group)和 ...
- PostgreSQL学习笔记-7.基础知识:子查询、自增、PRIVILEGES 权限
子查询 子查询或称为内部查询.嵌套查询,指的是在 PostgreSQL 查询中的 WHERE 子句中嵌入查询语句.一个 SELECT 语句的查询结果能够作为另一个语句的输入值.子查询可以与 SELEC ...
- Excel--比较两列数据的异同
首先得到的数据分为两列,两种类型.由于在网站上搜索的时候,网站的"特殊性"会将000638-32-4 前面的0全部去掉.变成了638-32-4.基于得到了两列稍有不同的数据.由于人 ...
- 【PHP正则表达式】
[PHP正则表达式] 最近写题总是遇到php正则表达式的匹配函数,于是进行一个总结. 1.什么是正则表达式 是php在进行搜索时用于匹配的模式字符串.一般用于php对特定字符序列的替换和搜索. 2.正 ...