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 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:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <iostream>
  5 using namespace std;
  6 const int maxn = 200002;
  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 = 0;
 17     for (i = 1; i <= n; i++) if (a[i] == 0) c[cnt++] = i;
 18     int be = 1;
 19     for (i = 0; i < cnt; i++) {
 20         for (j = be; j < c[i]; j++) near0[j] = c[i];
 21         be = c[i] + 1;
 22     }
 23     cnt = 0;
 24     for (i = 1; i <= n; i++) if (a[i] == 1) c[cnt++] = i;
 25     be = 1;
 26     for (i = 0; i < cnt; i++) {
 27         for (j = be; j < c[i]; j++) near1[j] = c[i];
 28         be = c[i] + 1;
 29     }
 30 }
 31
 32 void read(LL &res) {
 33     res = 0;
 34     char c = ‘ ‘;
 35     while (c < ‘0‘ || c > ‘9‘) c = getchar();
 36     while (c >= ‘0‘ && c <= ‘9‘) res = res * 10 + c - ‘0‘, c = getchar();
 37 }
 38 void read(int &res) {
 39     res = 0;
 40     char c = ‘ ‘;
 41     while (c < ‘0‘ || c > ‘9‘) c = getchar();
 42     while (c >= ‘0‘ && c <= ‘9‘) res = res * 10 + c - ‘0‘, 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 = 1;
 52     for (int i = en; i >= be; i--) {
 53         //if (quick_pow(a[i], res, res)) return true;
 54         LL tmp = 1;
 55         for (int j = 0; 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 = 1; 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] == 0) {
 72             puts("Yes"); continue;
 73         }
 74         if (x == 1) {
 75             puts("Yes"); continue;
 76         }
 77         if (a[i] == 1) {
 78             puts("No"); continue;
 79         }
 80         if (a[i+1] == 0 && j >= i + 1) {
 81             puts("No"); continue;
 82         }
 83         cnt = 0; flag = true;
 84         while (x != 1) {
 85             LL tmp = gcd(x, a[i]);
 86             if (tmp == 1) {
 87                 flag = false; break;
 88             }
 89             while (x % tmp == 0) x /= tmp, ++cnt;
 90         }
 91         if (near0[i] <= j) j = min(j, near0[i] - 2);
 92         if (near1[i] <= j) j = min(j, near1[i] - 1);
 93         if (j == i) {
 94             if (!flag || cnt > 1) puts("No");
 95             else if (a[i] % x == 0) puts("Yes");
 96             else puts("No");
 97             continue;
 98         }
 99         if (!flag || !ok(i+1, j)) puts("No");
100         else puts("Yes");
101     }
102     return 0;
103 }

Hackerrank--Divisibility of Power(Math)

时间: 2024-08-26 17:47:23

Hackerrank--Divisibility of Power(Math)的相关文章

math、string

1 Math.PI // 圆周率 2 Math.random() // 生成随机数 3 Math.floor()/Math.ceil() // 向下取整/向上取整 4 Math.round() // 取整,四舍五入 5 Math.abs() // 绝对值 6 Math.max()/Math.min() // 求最大和最小值 7 Math.sin()/Math.cos() // 正弦/余弦 8 Math.power()/Math.sqrt() // 求指数次幂/求平方根 //underScore

TCP进制转换

/// <summary> /// 将十六进制字符串转化为字节数组 /// </summary> /// <param name="src"></param> /// <returns></returns> public byte[] HexString2Bytes(string src) { byte[] retBytes = new byte[src.Length / 2]; for (int i = 0; i

JS 异常:Uncaught RangeError: Maximum call stack size exceeded解析

出现这种异常的原因是递归成了死循环或者代码中出现了死循环. 检查代码发现没有明显的死循环,故上网搜索了下,发现这篇文章中的讲解非常有意思,认真理解了下文章中提到的代码. // A Math.power = Math.pow; Math.pow = function(x,y) { if (x != 0) { return Math.power(x,y); } else { return 0; } }; // B Math.power = Math.pow; Math.pow = function(

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

遇到了这个js异常, 总是吧浏览器搞崩溃,这是什么原因呢? 开始我也只能想到死循环, 也许是哪个条件判断写错了,其实不是.经过google,发现了一篇文章,内容请看: ================================================================= 文章地址: http://www.zizhujy.com/blog/post/2012/03/18/Uncaught-RangeError-Maximum-call-stack-size-exceed

UTC格式转换 &amp; 十六进制换算为十进制

UTC格式转换成北京时间格式: /// <summary> /// UTC格式与datatime的转换 /// </summary> /// <param name="utc"></param> /// <returns></returns> public DateTime ConvertIntDatetime(double utc) { System.DateTime startTime = TimeZone.C

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

在C#中,十进制和十六进制转换非常简单,方法如下:十进制转为十六进制: int a = 100; String strA = a.ToString("x8"); 十六进制转为十进制: int b= Int32.Parse("266", System.Globalization.NumberStyles.HexNumber); 也可以使用下述方法: /// <summary> /// 十六进制换算为十进制 /// </summary> ///

第七课:数值以及函数的扩展和修复

1.数值扩展和修复 toFixed(num) 方法可把 Number 四舍五入为指定小数位数的数字.num必需,规定小数的位数,是 0 ~ 20 之间的值,包括 0 和 20,有些实现可以支持更大的数值范围.如果省略了该参数,将用 0 代替.返回 NumberObject 的字符串表示,不采用指数计数法,小数点后有固定的 num 位数字.如果必要,该数字会被舍入,也可以用 0 补足,以便它达到指定的长度.如果 num 大于 le+21,则该方法只调用 NumberObject.toString(

Problem 16

Problem 16 pow(2, 15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.2的15次方等于32768,而这些数字(3+2+7+6+8)的和为26What is the sum of the digits of the number pow(2, 1000)?2的1000次方的位数之和为多少? import math power = math.pow(2, 1000) sum_of_digits = 0 wi

javaScript基础知识总汇

javaScript是什么: 1.JavaScript 运行在客户端(浏览器)的编程语言 2.用来给HTML网页增加动态功能 3.用来给HTML网页增加动态功能. 4.Netscape在最初将其脚本语言命名为LiveScript,后来Netscape在与Sun合作之后将其改名为JavaScript.为什么开头叫java,因为当时java特别牛逼吗,所以可能为了营销吧. 应用场景: 网页特效 服务端开发(Node.js) 命令行工具(Node.js) 桌面程序 App 控制硬件-物联网 网页游戏开