Hackerrank--Divisibility of Power(Math)
You are given an array A of size N. You are asked to answer Q queries.
Each query is of the form :
i j x
You need to print
Yesif x divides the value returned from find(i,j) function, otherwise printNo.find(int i,int j)
{
if(i>j) return 1;
ans = pow(A[i],find(i+1,j))
return ans
}
Input Format
First line of the input contains N. Next line contains N space separated numbers. The line, thereafter, contains Q , the number of queries to follow. Each of the next Q lines contains three positive integer i, j and x.
Output Format
For each query display
YesorNoas explained above.Constraints
2≤N≤2×105
2≤Q≤3×105
1≤i,j≤N
i≤j
1≤x≤1016
0≤ value of array element ≤1016No 2 consecutive entries in the array will be zero.
Sample Input
4
2 3 4 5
2
1 2 4
1 3 7
Sample Output
Yes
No
首先,对于每次询问(i,j,x), 如果x中含有a[i]中没有的质因子,那么一定是No
其次,求出需要几个a[i]才能被x整除之后(设为cnt),就需要判断find(i+1, j)和cnt的大小。
对于a[i] = 0 或者 1 的情况可以进行特判,其他情况,因为x不大于1e16,所以可以直接暴力。
Accepted Code:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn = ;
typedef long long LL;
const int inf = 0x3f3f3f3f;
LL a[maxn], x;
int n, Q, i, j, cnt, near0[maxn], near1[maxn], c[maxn];
bool flag; void init() {
memset(near0, 0x3f, sizeof(near0));
memset(near1, 0x3f, sizeof(near1));
cnt = ;
for (i = ; i <= n; i++) if (a[i] == ) c[cnt++] = i;
int be = ;
for (i = ; i < cnt; i++) {
for (j = be; j < c[i]; j++) near0[j] = c[i];
be = c[i] + ;
}
cnt = ;
for (i = ; i <= n; i++) if (a[i] == ) c[cnt++] = i;
be = ;
for (i = ; i < cnt; i++) {
for (j = be; j < c[i]; j++) near1[j] = c[i];
be = c[i] + ;
}
} void read(LL &res) {
res = ;
char c = ' ';
while (c < '' || c > '') c = getchar();
while (c >= '' && c <= '') res = res * + c - '', c = getchar();
}
void read(int &res) {
res = ;
char c = ' ';
while (c < '' || c > '') c = getchar();
while (c >= '' && c <= '') res = res * + c - '', c = getchar();
} LL gcd(LL a, LL b) {
if (!b) return a;
else return gcd(b, a % b);
} bool ok(int be, int en) {
LL res = ;
for (int i = en; i >= be; i--) {
//if (quick_pow(a[i], res, res)) return true;
LL tmp = ;
for (int j = ; j < res; j++) {
if (tmp >= cnt || a[i] >= cnt) return true;
tmp *= a[i];
}
if (tmp >= cnt) return true;
res = tmp;
}
return res >= cnt;
}
int main(void) {
read(n);
for (i = ; i <= n; i++) read(a[i]);
init();
read(Q);
while (Q--) {
read(i), read(j), read(x);
if (a[i] == ) {
puts("Yes"); continue;
}
if (x == ) {
puts("Yes"); continue;
}
if (a[i] == ) {
puts("No"); continue;
}
if (a[i+] == && j >= i + ) {
puts("No"); continue;
}
cnt = ; flag = true;
while (x != ) {
LL tmp = gcd(x, a[i]);
if (tmp == ) {
flag = false; break;
}
while (x % tmp == ) x /= tmp, ++cnt;
}
if (near0[i] <= j) j = min(j, near0[i] - );
if (near1[i] <= j) j = min(j, near1[i] - );
if (j == i) {
if (!flag || cnt > ) puts("No");
else if (a[i] % x == ) puts("Yes");
else puts("No");
continue;
}
if (!flag || !ok(i+, j)) puts("No");
else puts("Yes");
}
return ;
}
Hackerrank--Divisibility of Power(Math)的更多相关文章
- js入门之内置对象Math
一. 复习数据类型 简单数据类型, 基本数据类型/值类型 Number String Boolean Null Undefined 复杂数据类型 引用类型 Object 数组 数据在内存中是如何存储的 ...
- UTC格式转换 & 十六进制换算为十进制
UTC格式转换成北京时间格式: /// <summary> /// UTC格式与datatime的转换 /// </summary> /// <param name=&q ...
- JS 异常: Uncaught RangeError: Maximum call stack size exceeded
遇到了这个js异常, 总是吧浏览器搞崩溃,这是什么原因呢? 开始我也只能想到死循环, 也许是哪个条件判断写错了,其实不是.经过google,发现了一篇文章,内容请看: ================ ...
- C# 十进制和十六进制转换
转至:http://www.cnblogs.com/fwind/archive/2012/04/13/2445380.html 在C#中,十进制和十六进制转换非常简单,方法如下: 十进制转为十六进制: ...
- NumPy for MATLAB users
http://mathesaurus.sourceforge.net/matlab-numpy.html Help MATLAB/Octave Python Description dochelp - ...
- 构建一个类jq的函数库
jqfree core var $ = function(selector, context) { return new $.fn.init(selector, context); }; $.fn = ...
- ProE常用曲线方程:Python Matplotlib 版本代码(蝴蝶曲线)
花纹的生成可以使用贴图的方式,同样也可以使用方程,本文列出了几种常用曲线的方程式,以取代贴图方式完成特定花纹的生成. 注意极坐标的使用................. 前面部分基础资料,参考:Pyt ...
- Problem 16
Problem 16 pow(2, 15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.2的15次方等于32768,而这些数 ...
- javascript基础入门知识点整理
学习目标: - 掌握编程的基本思维 - 掌握编程的基本语法 typora-copy-images-to: media JavaScript基础 HTML和CSS 京东 课前娱乐 众人皆笑我疯癫,我笑尔 ...
随机推荐
- position:fixed失效问题
fixed定位的元素,如果父级有transform样式,值不为none,那么fixed定位就会失效. 解决方法:使用transform样式的元素,不要包含fixed定位的子元素.
- pyqt点击右上角关闭界面但是子线程仍在运行
现象: 通过右上角的叉关闭图形界面后,程序运行的子线程却不会被自动关闭,依然留存在系统中原因: 子线程没有正确关闭解决方法: 1.将子线程设置成守护线程 self.your_thread = thre ...
- 菜鸟nginx源码剖析数据结构篇(五) 基数树 ngx_radix_tree_t[转]
菜鸟nginx源码剖析数据结构篇(五) 基数树 ngx_radix_tree_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blo ...
- C++系列作业
1.编写一个完整的程序,实现功能:向用户提问“现在正在下雨吗?”,提示用户输入Y或N.若输入为Y,显示“现在正在下雨.”:若输入为N,显示“现在没有下雨”:否则继续提问“现在正在下雨吗?” #incl ...
- Python学习day11-函数基础(1)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Luogu P3007 [USACO11JAN]大陆议会The Continental Cowngress
P3007 [USACO11JAN]大陆议会The Continental Cowngress 题意 题意翻译 简述:给出\(n\)个法案,\(m\)头牛的意见,每头牛有两个表决格式为"支持 ...
- Twisted的WEB开发
1 简介 在WEB开发中,偶尔需要对HTTP协议更多底层细节进行控制,这时的django/web.py等等显然无法满足要求,所以只好求助于Twisted了.使用Twisted进行WEB开发,其实更 ...
- php冒泡算法
1.冒泡算法 网上搜了很多,但是总是对于每次循环的边界值思路讲的比较笼统. 不是很容易被新手记住,我自己平时也是硬记下来的. 但是对于算法,硬记,时间长了还是容易忘记,所以自己写了一次,把每次思路尽量 ...
- LUOGU P3178 [HAOI2015]树上操作
传送门 解题思路 树链剖分裸题,线段树维护. 代码 #include<iostream> #include<cstdio> #include<cstring> #d ...
- vue.js_12_vue的watch和computed
1.watch用来监测指定Vue实例上的数据变动. watch主要用于监控vue实例的变化,它监控的变量当然必须在data里面声明才可以,它可以监控一个变量,也可以是一个对象. 1.>使用wat ...