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. ZH奶酪:PHP图片压缩(TinyPNG在线API)和(使用Imagick扩展)

    1.调用TinyPng网站提供的API 1.1.须知 (1)tinypng的官网:https://tinypng.com/ 不知道国内访问会不会很慢,在Singapore打开这个网站很流畅: (2)A ...

  2. android:View的setTag和getTag使用

    1.用于区分非常多类似的View 比如: button1.setOnClickListener(new OnClickListener ... ); button2.setOnClickListene ...

  3. spring 中常用的配置项

    1.spring 中常用的配置项 application.properties #端口 server.port=8081 #调试模式 debug=false #上下文 #一般情况下,小项目通常都是在t ...

  4. alipay

    //安装 插件cordova plugin add https://github.com/charleyw/cordova-plugin-alipay.git --variable PARTNER_I ...

  5. iOS 推断设备为iPhone还是iPad

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.viewControlle ...

  6. 解决-bash: fork: retry: Resource temporarily unavailable (修改最大线程数)

    错误提示的本质是Linux操作系统无法创建更多进程,导致出错.因此要解决这个问题需要修改Linux允许创建更多的进程. 方案一: cat /etc/security/limits.conf echo ...

  7. windows installer服务无法启动,无法打开任何msi文件

    如果不成功就在"依存关系"中找是否有其他的文件没有启用. 启用"remote procedure call(rpc)" 启用"workstation& ...

  8. oracle 批量更新之将一个表的数据批量更新至另一个表

      oracle 批量更新之将一个表的数据批量更新至另一个表 CreationTime--2018年7月3日17点38分 Author:Marydon Oracle 将一个表的指定字段的值更新至另一个 ...

  9. 2014年,daliu_it 年末文章汇总清单

    一.javabase 1. Windows环境下JDK安装与环境变量配置 详细的介绍了JDK的安装以及配图,同时安装的注意事项. 2. 项目的命名规范,为以后的程序开发中养成良好的行为习惯 详细的介绍 ...

  10. PHP进行安全字段和防止XSS跨站脚本攻击过滤(通用版)

    废话不多说,直接贴使用方法和代码: 使用方式:1)写在公共方法里面,随时调用即可.2)写入类文件,使用是include_once 即可 代码: /* 进行安全字段和xss跨站脚本攻击过滤(通用版) - ...