题目链接

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 Yes if x divides the value returned from find(i,j) function, otherwise print No.

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 Yes or No as 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 ≤1016

No 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)的更多相关文章

  1. js入门之内置对象Math

    一. 复习数据类型 简单数据类型, 基本数据类型/值类型 Number String Boolean Null Undefined 复杂数据类型 引用类型 Object 数组 数据在内存中是如何存储的 ...

  2. UTC格式转换 & 十六进制换算为十进制

    UTC格式转换成北京时间格式: /// <summary> /// UTC格式与datatime的转换 /// </summary> /// <param name=&q ...

  3. JS 异常: Uncaught RangeError: Maximum call stack size exceeded

    遇到了这个js异常, 总是吧浏览器搞崩溃,这是什么原因呢? 开始我也只能想到死循环, 也许是哪个条件判断写错了,其实不是.经过google,发现了一篇文章,内容请看: ================ ...

  4. C# 十进制和十六进制转换

    转至:http://www.cnblogs.com/fwind/archive/2012/04/13/2445380.html 在C#中,十进制和十六进制转换非常简单,方法如下: 十进制转为十六进制: ...

  5. NumPy for MATLAB users

    http://mathesaurus.sourceforge.net/matlab-numpy.html Help MATLAB/Octave Python Description dochelp - ...

  6. 构建一个类jq的函数库

    jqfree core var $ = function(selector, context) { return new $.fn.init(selector, context); }; $.fn = ...

  7. ProE常用曲线方程:Python Matplotlib 版本代码(蝴蝶曲线)

    花纹的生成可以使用贴图的方式,同样也可以使用方程,本文列出了几种常用曲线的方程式,以取代贴图方式完成特定花纹的生成. 注意极坐标的使用................. 前面部分基础资料,参考:Pyt ...

  8. Problem 16

    Problem 16 pow(2, 15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.2的15次方等于32768,而这些数 ...

  9. javascript基础入门知识点整理

    学习目标: - 掌握编程的基本思维 - 掌握编程的基本语法 typora-copy-images-to: media JavaScript基础 HTML和CSS 京东 课前娱乐 众人皆笑我疯癫,我笑尔 ...

随机推荐

  1. .NET中DataTable的常用操作

    一.目的 在各种.NET开发中,DataTable都是一个非常常见且重要的类型,在与数据打交道的过程中可以说是必不可少的对象. 它功能强大,属性与功能也是相当丰富,用好的话,使我们在处理数据时,减少很 ...

  2. ie中报错---不能执行已释放 Script 的代码

    我的原因:使用jquery.colorbox.js.在页面中使用弹框,对于父页面中的变量进行修改(弹框页面的js--window.parent.obj.arr= 数组;), 当弹框关闭之后,在父页面中 ...

  3. [JZOJ4759] 【雅礼联考GDOI2017模拟9.4】石子游戏

    题目 描述 题目大意 在一棵树上,每个节点都有些石子. 每次将mmm颗石子往上移,移到根节点就不能移了. 双方轮流操作,问先手声还是后手胜. 有三种操作: 1. 询问以某个节点为根的答案. 2. 改变 ...

  4. JAVA 类加载机制学习笔记

    JAVA 类生命周期 如上图所示,Java类的生命周期如图所示,分别为加载.验证.准备.解析.初始化.使用.卸载.其中验证.准备.解析这三个步骤统称为链接. 加载:JVM根据全限定名来获取一段二进制字 ...

  5. LUOGU P1342 请柬(最短路)

    传送门 解题思路 又是一道语文题,弄清楚题意之后其实就能想出来了,从1跑一遍最短路,把$dis[n]$加入答案.在建个反图跑一遍最短路,把$dis[n]_$加入最短路就行了.第一遍是去的时候,第二遍是 ...

  6. 小程序关闭时暂停webview里的音乐

    document.addEventListener("visibilitychange", () => {  if(document.hidden) {     // 页面被 ...

  7. Codeforces Round #599 (Div. 2)的简单题题解

    难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...

  8. 基于知识图谱的APT组织追踪治理

    高级持续性威胁(APT)正日益成为针对政府和企业重要资产的不可忽视的网络空间重大威胁.由于APT攻击往往具有明确的攻击意图,并且其攻击手段具备极高的隐蔽性和潜伏性,传统的网络检测手段通常无法有效对其进 ...

  9. iOS之UIBezierPath贝塞尔曲线属性简介

    #import <Foundation/Foundation.h> #import <CoreGraphics/CoreGraphics.h> #import <UIKi ...

  10. 元素显示v-show

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...