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. Oracle查询client编码集

    Oracle查询client编码集 SQL> select userenv('language') from dual; USERENV('LANGUAGE') ---------------- ...

  2. C#.NET常见问题(FAQ)-delegate委托链如何使用

    委托链本质就是你把一堆要执行的东西放到一个list里面,当要触发一组事情的时候,就不需要一个一个写一遍了(比如厂里食堂开饭了,这个方法一执行,要让厨师A时间在食堂等候打饭,B类员工在某个时间排队打饭, ...

  3. MySQL Cluster配置文件-SQL节点4G内存

    # Example MySQL config file for large systems. # # This is for a large system with memory = 512M whe ...

  4. 在网页浏览器中原生显示PDF文件

    在网页中直接显示pdf格式的文件方便阅读.但是如果文件较大加载速度会很慢,另外如果客户端没有安装pdf阅读插件的话,也就看不了了. 这种方式的好处就是不需要转换,直接显示,而且在加载时(高级的浏览器, ...

  5. 算法笔记_190:历届试题 幸运数(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 幸运数是波兰数学家乌拉姆命名的.它采用与生成素数类似的“筛法”生成 . 首先从1开始写出自然数1,2,3,4,5,6,.... 1 就是第 ...

  6. JVM内存管理、JVM垃圾回收机制、新生代、老年代以及永久代

    内存模型 JVM运行时数据区由程序计数器.堆.虚拟机栈.本地方法栈.方法区部分组成,结构图如下所示. JVM内存结构由程序计数器.堆.栈.本地方法栈.方法区等部分组成,结构图如下所示: 1)程序计数器 ...

  7. NET的基本用法(摘)

    摘自:http://baike.baidu.com/link?url=Knc-OicoX8CPcaMS0r3eU8z8ns9z1S6OsRaBTYUIT1raU0FsPWQ35xL-dlxKg9Oy# ...

  8. MongoDB-开始学习MongoDB(一)

    先来看看MongoDB的优缺点: 优点:简单的扩展.快速的读写.灵活的数据类型 缺点:不支持对SQL的支持.支持的特性不够丰富.现有产品不够成熟 应用场景: 适用场景: 持久化缓存层.实时的高效性(读 ...

  9. js:获取节点相关的 nodeName,nodeType,nodeValue

    getElementById() getElementsByName() getElementsByTagName() hasChildNodes() nodeName nodeType=1元素节点/ ...

  10. 【转载】Delphi下实现鼠标自动点击器

    本文最早于2009年6月1日在编程论坛(programbbs.com)上发表,页面地址:http://programbbs.com/bbs/view12-20849-1.htm . 众所周知,当鼠标指 ...