[Codility] CommonPrimeDivisors
A 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的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
随机推荐
- unity3d插件Daikon Forge GUI 中文教程-3-基础控件Button和Sprite的使用
(游戏蛮牛首发)大家好我是孙广东.官网提供了专业的视频教程http://www.daikonforge.com/dfgui/tutorials/,只是是在youtube上.要观看是须要FQ的. 只是教 ...
- 算法笔记_126:算法集训之编程大题集二(Java)
目录 1 连续数的公倍数 2 漏掉的账目明细 3 罗马数字转十进制 4 逻辑推断 5 平面4点最小距离 6 取球博弈 7 人民币金额大写 8 人员排日程 9 三角螺旋阵 10 手机尾号评分 1 ...
- 转:StdRegProv类所属方法的使用
在root\default命名空间中的StdRegProv类(标准注册表提供程序)提供了下面16种方法,我们将陆续介绍这些方法的使用规则,并给出分别用WBscript和Powershell编写的例子. ...
- SettingsHBuilder
迁移时间:2017年5月20日10:56:50CreateTime--2016年9月27日14:22:26Author:Marydon1.修改HBuilder开发工具快捷键工具-->首选项- ...
- 【Oracle】事务处理
名词解释 DML:Data Manipulation Language (数据库操纵语言) 例如:DELETE.INSERT.UPDATE.SELECT DDL:Data Definition Lan ...
- Drupal administration theme
Drupal允许为管理后台设置独立的theme,保存在系统变量variable_get('admin_theme'). Drupal使用全局变量$theme来保存当前请求对应的主题.Drupal在启动 ...
- JavaScript中的数组与伪数组的区别
在JavaScript中,除了5种原始数据类型之外,其他所有的都是对象,包括函数(Function). 基本数据类型:String,boolean,Number,Undefined, Null 引用数 ...
- vi中全选的命令或者快捷方式
http://blog.163.com/boby_boke/blog/static/126877354200910308522382/网上有两种说法比较多:“:1,$y”和 “dG” 但是我查到有资料 ...
- Python学习笔记020——数据库知识概述
数据库概述 1 提供数据库的软件都有哪些 MySQL.SQL_Server.Oracle.DB2.Mariadb.MongoDB ... (1)是否开源 开源软件:MySQL.Mariadb.Mong ...
- 转:Web优化 及常用工具包
Web优化: 减少http请求 避免404错误 在html页面header加入缓存标签 Gzip压缩网页 减少cookie体积 使用外部的js和css 消减js和css 压缩js 使用css spri ...