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
Yes
if 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
Yes
orNo
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 ≤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 京东 课前娱乐 众人皆笑我疯癫,我笑尔 ...
随机推荐
- 【笔记篇】C#笔记3
笔记目录:http://blog.csdn.net/enzymii/article/details/77169928 C#的接口有点意思,我们说过可以用来多重继承.. using System; na ...
- QSerialPort类
一.简介 QSerialPort类是Qt5封装的串口类,可以与串口进行通信.QSerialPortInfo是一个辅助类,提供串口的一些信息,如可用的串口名称,描述,制造商,序列号,串口16位产 ...
- leetcode--81-搜索旋转排序数组②
题目描述: 33题 方法一: class Solution: def search(self, nums: List[int], target: int) -> bool: l, r = 0, ...
- Python-包与常用模块(2)
目录 包 导入包内包 相对导入和绝对导入 注意事项 time模块 datetime模块 random模块 argparse 模块 configparser模块 hashlib模块和hmac模块 typ ...
- 使用phpStudy自带的mysql-front学习建库建表以及基本的mysql语句
1.鼠标左键phpStudy图标,点击mysql管理器,如下图 2.选择Mysql-Front,选择localhost进入,可以看到本地建立的数据库.然后新建一个数据库,如下图: 3.在新建的数据库上 ...
- C#多线程实现方法——线程池(Thread Pool)
ThreadPool使用 同步机制 ThreadPool使用 需要定义waitcallback委托形式如 public delegate void WaitCallback(object stat ...
- 03. 将pdb调试文件包含到.vsix包中
vs插件如何把pdb文件打包进去,方便记录日志和调试 <PropertyGroup> <CopyLocalLockFileAssemblies>true</CopyLoc ...
- PHP实现图片的汉明码提取与降维
作者感言:数学不好,遇到算法问题分分钟狗带,毫无转寰的余地-_-||| 最近心血来潮,看了相似图片的搜索,最最最初级的方法即提取汉明码,之后匹配汉明距离.当然,在数以亿计的汉明码中,要筛出需要的图片, ...
- TensorFlow实战Google深度学习框架-人工智能教程-自学人工智能的第二天-深度学习
自学人工智能的第一天 "TensorFlow 是谷歌 2015 年开源的主流深度学习框架,目前已得到广泛应用.本书为 TensorFlow 入门参考书,旨在帮助读者以快速.有效的方式上手 T ...
- <scrapy爬虫>爬取猫眼电影top100详细信息
1.创建scrapy项目 dos窗口输入: scrapy startproject maoyan cd maoyan 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # -*- ...