题目链接

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.

  1. find(int i,int j)
  2. {
  3. if(i>j) return 1;
  4. ans = pow(A[i],find(i+1,j))
  5. return ans
  6. }

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

  1. 4
  2. 2 3 4 5
  3. 2
  4. 1 2 4
  5. 1 3 7

Sample Output

  1. Yes
  2. No
  1. 首先,对于每次询问(ijx), 如果x中含有a[i]中没有的质因子,那么一定是No
    其次,求出需要几个a[i]才能被x整除之后(设为cnt),就需要判断find(i+1, j)和cnt的大小。
    对于a[i] = 0 或者 1 的情况可以进行特判,其他情况,因为x不大于1e16,所以可以直接暴力。
    Accepted Code:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <iostream>
  5. using namespace std;
  6. const int maxn = ;
  7. typedef long long LL;
  8. const int inf = 0x3f3f3f3f;
  9. LL a[maxn], x;
  10. int n, Q, i, j, cnt, near0[maxn], near1[maxn], c[maxn];
  11. bool flag;
  12.  
  13. void init() {
  14. memset(near0, 0x3f, sizeof(near0));
  15. memset(near1, 0x3f, sizeof(near1));
  16. cnt = ;
  17. for (i = ; i <= n; i++) if (a[i] == ) c[cnt++] = i;
  18. int be = ;
  19. for (i = ; i < cnt; i++) {
  20. for (j = be; j < c[i]; j++) near0[j] = c[i];
  21. be = c[i] + ;
  22. }
  23. cnt = ;
  24. for (i = ; i <= n; i++) if (a[i] == ) c[cnt++] = i;
  25. be = ;
  26. for (i = ; i < cnt; i++) {
  27. for (j = be; j < c[i]; j++) near1[j] = c[i];
  28. be = c[i] + ;
  29. }
  30. }
  31.  
  32. void read(LL &res) {
  33. res = ;
  34. char c = ' ';
  35. while (c < '' || c > '') c = getchar();
  36. while (c >= '' && c <= '') res = res * + c - '', c = getchar();
  37. }
  38. void read(int &res) {
  39. res = ;
  40. char c = ' ';
  41. while (c < '' || c > '') c = getchar();
  42. while (c >= '' && c <= '') res = res * + c - '', c = getchar();
  43. }
  44.  
  45. LL gcd(LL a, LL b) {
  46. if (!b) return a;
  47. else return gcd(b, a % b);
  48. }
  49.  
  50. bool ok(int be, int en) {
  51. LL res = ;
  52. for (int i = en; i >= be; i--) {
  53. //if (quick_pow(a[i], res, res)) return true;
  54. LL tmp = ;
  55. for (int j = ; j < res; j++) {
  56. if (tmp >= cnt || a[i] >= cnt) return true;
  57. tmp *= a[i];
  58. }
  59. if (tmp >= cnt) return true;
  60. res = tmp;
  61. }
  62. return res >= cnt;
  63. }
  64. int main(void) {
  65. read(n);
  66. for (i = ; i <= n; i++) read(a[i]);
  67. init();
  68. read(Q);
  69. while (Q--) {
  70. read(i), read(j), read(x);
  71. if (a[i] == ) {
  72. puts("Yes"); continue;
  73. }
  74. if (x == ) {
  75. puts("Yes"); continue;
  76. }
  77. if (a[i] == ) {
  78. puts("No"); continue;
  79. }
  80. if (a[i+] == && j >= i + ) {
  81. puts("No"); continue;
  82. }
  83. cnt = ; flag = true;
  84. while (x != ) {
  85. LL tmp = gcd(x, a[i]);
  86. if (tmp == ) {
  87. flag = false; break;
  88. }
  89. while (x % tmp == ) x /= tmp, ++cnt;
  90. }
  91. if (near0[i] <= j) j = min(j, near0[i] - );
  92. if (near1[i] <= j) j = min(j, near1[i] - );
  93. if (j == i) {
  94. if (!flag || cnt > ) puts("No");
  95. else if (a[i] % x == ) puts("Yes");
  96. else puts("No");
  97. continue;
  98. }
  99. if (!flag || !ok(i+, j)) puts("No");
  100. else puts("Yes");
  101. }
  102. return ;
  103. }

  1.  

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. html-圣杯布局

    1.两边固定 当中自适应 2.当中列要完整显示 3.当中列要优先加载 浮动: 搭建完整的布局框架 margin 为赋值:调整旁边两列的位置(使三列布局到一行上) 使用相对定位:调整旁边两列的位置(使两 ...

  2. 【BZOJ4916】神犇与蒟蒻

    题面 Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;\(1<=N<=10^9\),A.B模\(10^9+7 ...

  3. https搭建:ubuntu nginx配置 SSL证书

    HTTPS 是什么? 根据维基百科的解释: 超文本传输安全协议(缩写:HTTPS,英语:Hypertext Transfer Protocol Secure)是超文本传输协议和SSL/TLS的组合,用 ...

  4. Oracle - 用户及表空间的创建和删除

    -- 查询所有用户 SELECT USERNAME FROM ALL_USERS; -- 查询所有表空间 SELECT TABLESPACE_NAME FROM USER_TABLESPACES; - ...

  5. LinkedHashMap笔记

    一.最基本元素存储单元 /** * HashMap.Node subclass for normal LinkedHashMap entries. */ static class Entry<K ...

  6. spring retry 重试机制完整例子

    public static Boolean vpmsRetryCoupon(final String userId) { // 构建重试模板实例 RetryTemplate retryTemplate ...

  7. 第二周——1.项目中MySQL版本问题

    1.版本升级 经组长推荐,本地安装的是mysql-8.0.11,而主项目用的还是版本5.6, 因此需要升级版本. 首先,更新驱动:下载mysql-connector-java-8.0.11,将E:\P ...

  8. C#生成指定范围内的不重复随机数

    C#生成指定范围内的不重复随机数 // Number随机数个数 // minNum随机数下限 // maxNum随机数上限 public int[] GetRandomArray(int Number ...

  9. C开发系列-指针

    指针 通过一段简单的程序,引入指针的概念 #include <iostream> using namespace std; // changeValue函数的定义 void changeV ...

  10. [转]基于MefBootstrapper的Bootstrapper

    public class Bootstrapper : MefBootstrapper    {        ModuleCatalog moduleCatalog; protected overr ...