prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.

A prime D is called a prime divisor of a positive integer P if there exists a positive integer K such that D * K = P. For example, 2 and 5 are prime divisors of 20.

You are given two positive integers N and M. The goal is to check whether the sets of prime divisors of integers N and M are exactly the same.

For example, given:

  • N = 15 and M = 75, the prime divisors are the same: {3, 5};
  • N = 10 and M = 30, the prime divisors aren't the same: {2, 5} is not equal to {2, 3, 5};
  • N = 9 and M = 5, the prime divisors aren't the same: {3} is not equal to {5}.

Write a function:

int solution(vector<int> &A, vector<int> &B);

that, given two non-empty zero-indexed arrays A and B of Z integers, returns the number of positions K for which the prime divisors of A[K] and B[K] are exactly the same.

For example, given:

    A[0] = 15   B[0] = 75
A[1] = 10 B[1] = 30
A[2] = 3 B[2] = 5

the function should return 1, because only one pair (15, 75) has the same set of prime divisors.

Assume that:

  • Z is an integer within the range [1..6,000];
  • each element of arrays A, B is an integer within the range [1..2,147,483,647].

Complexity:

  • expected worst-case time complexity is O(Z*log(max(A)+max(B))2);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

判断两个数是否有相同的素数约数。首先求出公约数gcd_val,那么gcd_val里应该包含了common prime divisor,下面分别判断a跟b与gcd_val的公约数是不是有自己的非common prime divisor的prime divisor。

 // you can use includes, for example:
// #include <algorithm> // you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int gcd(int a, int b) {
if (a < b) return gcd(b, a);
return b > ? gcd(b, a % b) : a;
} bool hasSamePrimeDivisors(int a, int b) {
int gcd_val = gcd(a, b);
int gcd_a, gcd_b;
while (a != ) {
gcd_a = gcd(a, gcd_val);
if (gcd_a == ) break;
a /= gcd_a;
}
if (a != ) return false;
while (b != ) {
gcd_b = gcd(b, gcd_val);
if (gcd_b == ) break;
b /= gcd_b;
}
return b == ;
} int solution(vector<int> &A, vector<int> &B) {
// write your code in C++11
int cnt = ;
for (int i = ; i < A.size() && i < B.size(); ++i) {
if (hasSamePrimeDivisors(A[i], B[i])) ++cnt;
}
return cnt;
}
 def gcd(x, y):
# Compute the greatest common divisor
if x%y == 0:
return y;
else:
return gcd(y, x%y) def hasSamePrimeDivisors(x, y):
gcd_value = gcd(x, y) # The gcd contains all
# the common prime divisors while x != 1:
x_gcd = gcd(x, gcd_value)
if x_gcd == 1:
# x does not contain any more
# common prime divisors
break
x /= x_gcd
if x != 1:
# If x and y have exactly the same common
# prime divisors, x must be composed by
# the prime divisors in gcd_value. So
# after previous loop, x must be one.
return False while y != 1:
y_gcd = gcd(y, gcd_value)
if y_gcd == 1:
# y does not contain any more
# common prime divisors
break
y /= y_gcd return y == 1 def solution(A, B):
count = 0
for x,y in zip(A,B):
if hasSamePrimeDivisors(x,y):
count += 1
return count

[Codility] CommonPrimeDivisors的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  8. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  9. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

随机推荐

  1. UE查找和替换技巧实例

    1 删除多余的空行 如果是在WORD中,则查找^p^p替换为^p. 如果是在EXCEL里,则为全部选中,然后点击编辑,定位,定位条件,空值. 将全部选中空白的行,如图所示 再次点击编辑,删除,删除整行 ...

  2. -Prefix.pch has been modified 的错误修复

    方法一: 选择 Product > Clean 然后重新编译--运行: 方法二: 找到-Prefix.pch文件,把中间的 #ifdef __OBJC__ #import <UIKit/U ...

  3. OPENCV 常用函数

    1.cvCloneImage: IplImage* cvCloneImage( const IplImage* image ); 在使用函数之前,不用特地开辟内存,即该函数会自己开一段内存,然后复制好 ...

  4. html5开放资料

    http://www.cnblogs.com/tim-li/archive/2012/08/06/2580252.html KineticJS教程(12) 摘要: KineticJS教程(12) 作者 ...

  5. [转]intellij 13新建javaweb项目并用tomcat 7启动

    来自:http://blog.csdn.net/little__zm/article/details/19570535 对intellij不熟,找了很多文章,只这篇最为顺利.其他都有各种各校的问题,先 ...

  6. 1z0-052 q209_5

    5: Your database is open and the LISTENER listener is running. The new DBA of the system stops the l ...

  7. ubuntu 下安装摄像头驱动

    sudo apt-get install cheese sudo apt-get install camorama 然后可以打开应用cheese,观察可以得到图像. 也可以通过代码获取图像.pytho ...

  8. win10开启IE11企业模式

    .右击任务栏开始按钮,选择“运行”,打开运行框(或使用组合键Win+R打开运行) .输入gpedit.msc,进入“本地组策略编辑器”(注:该功能不支持Win8/Win8.1核心版.需要Win8/Wi ...

  9. Debian GNU Linux服务列表的获取、服务的关闭/开启、服务在启动时是否自己主动执行的生效/失效

    /*********************************************************************  * Author  : Samson  * Date   ...

  10. ocat

    <!DOCTYPE html> <html lang="zh-CN" > <head><meta http-equiv="Con ...