greatest-common-divisor

Maximize the sum of GCDs (Greatest Common Divisors) of a bipartition?

亡梦爱人 提交于 2019-12-05 00:38:29
问题 Given an array of positive numbers. I want to split the array into 2 different subset(s) such that the sum of their gcd (greatest common divisor) is maximum. Example array: {6,7,6,7} . Answer: The two required subsets are: {6,6} and {7,7} ; their respective gcd(s) are 6 and 7, their sum = 6+7=13 ; which is the maximum gcd summation possible. Gcd: Gcd of {8,12} is {4} as 4 is the greatest number which divides 8 as well as 12. Note: gcd(X)=X in case the subset contains only one element. My

Subsequence sum and GCD

丶灬走出姿态 提交于 2019-12-04 14:34:56
问题 I came across this question on a programming challenge about a month ago, but the editorial wasn't released so I am asking it here. There is an Array A of size N. Find the sum * GCD of K length subsequences of A. Example: If A = [1, 2, 3] and K = 2, {1, 2} = 3(sum) * 1(GCD) = 3 {1, 3} = 4(sum) * 1(GCD) = 4 {2, 3} = 5(sum) * 1(GCD) = 5 Ans => 3 + 4 + 5 = 12 回答1: Here it is from scratch (didn't test it thoroughly though): Let C[k, i, d] be the number of all k -length subsequences of A[1..i]

GCD algorithms for a large integers

跟風遠走 提交于 2019-12-04 13:56:05
I looking for the information about fast GCD computation algorithms. Especially, I would like to take a look at the realizations of that. The most interesting for me: - Lehmer GCD algorithm, - Accelerated GCD algorithm, - k-ary algorithm, - Knuth-Schonhage with FFT. I have completely NO information about the accelerated GCD algorithm, I just have seen a few articles where it was mentioned as the most effective and fast gcd computation method on the medium inputs (~1000 bits) They looks much difficult for me to understand from the theory view. Could anybody please share the code (preferable on

What is the fastest way to check if two given numbers are coprime?

こ雲淡風輕ζ 提交于 2019-12-04 10:43:27
问题 One way is to calculate their gcd and check if it is 1. Is there some faster way? 回答1: The Euclidean algorithm (computes gcd ) is very fast. When two numbers are drawn uniformly at random from [1, n] , the average number of steps to compute their gcd is O(log n) . The average computation time required for each step is quadratic in the number of digits. There are alternatives that perform somewhat better (i.e., each step is subquadratic in the number of digits), but they are only effective on

Knuth the art of computer programming ex 1.1.8

こ雲淡風輕ζ 提交于 2019-12-04 07:02:47
I can't figure out what Knuth meant in his instructions for an exercise 8 from Chapter 1.1. The task is to make an efficient gcd algorithm of two positive integers m and n using his notation theta[j] , phi[j] , b[j] and a[j] where theta and phi are strings and a and b - positive integers which represent computational steps in this case. Let an input be the string of the form a^mb^n . An excellent explanation of Knuth's algorithm is given by schnaader here . My question is how this may be connected with the direction given in the exercise to use his Algorithm E given in the book with original r

Juggling Algorithm

自作多情 提交于 2019-12-04 06:28:51
问题 METHOD (A Juggling Algorithm) Divide the array in different sets where number of sets is equal to GCD of n and d and move the elements within sets. If GCD is 1 as is for the above example array (n = 7 and d =2), then elements will be moved within one set only, we just start with temp = arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the right place. Here is an example for n =12 and d = 3. GCD is 3 and Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a) Elements are

Maximize the sum of GCDs (Greatest Common Divisors) of a bipartition?

半世苍凉 提交于 2019-12-03 16:55:30
Given an array of positive numbers. I want to split the array into 2 different subset(s) such that the sum of their gcd (greatest common divisor) is maximum. Example array: {6,7,6,7} . Answer: The two required subsets are: {6,6} and {7,7} ; their respective gcd(s) are 6 and 7, their sum = 6+7=13 ; which is the maximum gcd summation possible. Gcd: Gcd of {8,12} is {4} as 4 is the greatest number which divides 8 as well as 12. Note: gcd(X)=X in case the subset contains only one element. My approach: By brute-forcing, I find all possible sub-sequences of the array,then, I find the maximum sum,

RSA: Private key calculation with Extended Euclidean Algorithm

為{幸葍}努か 提交于 2019-12-03 09:50:57
I'm a high school student writing a paper on RSA, and I'm doing an example with some very small prime numbers. I understand how the system works, but I can't for the life of me calculate the private key using the extended euclidean algorithm. Here's what I have done so far: I have chosen the prime numbers p=37 and q=89 and calculated N=3293 I have calculated (p-1)(q-1)=3168 I have chosen a number e so that e and 3168 are relatively prime. I'm checking this with the standard euclidean algorithm, and that works very well. My e=25 Now I just have to calculate the private key d, which should

What is the fastest way to check if two given numbers are coprime?

吃可爱长大的小学妹 提交于 2019-12-03 06:26:35
One way is to calculate their gcd and check if it is 1. Is there some faster way? The Euclidean algorithm (computes gcd ) is very fast. When two numbers are drawn uniformly at random from [1, n] , the average number of steps to compute their gcd is O(log n) . The average computation time required for each step is quadratic in the number of digits. There are alternatives that perform somewhat better (i.e., each step is subquadratic in the number of digits), but they are only effective on very large integers. See, for example, On Schönhage's algorithm and subquadratic integer gcd computation .

How to find sum of all the GCDs of all the subarrays of an array?

主宰稳场 提交于 2019-12-01 14:01:38
Given an array A of length n = 10^5. I have to find the sum of GCD of all subarrays of this array efficiently. import math def lgcd(a): g = a[0] for i in range(1,len(a)): g = math.gcd(g,a[i]) return g n = int(input()) A = list(map(int,input().split())) ans = 0 for i in range(n): for j in range(i,n): ans+=lgcd(A[i:j+1]) print(ans) The first thing that should be marked is that GCD(A[i-1 : j]) * d = GCD(A[i : j]) where d is natural number. So for the fixed subarray end, there will be some blocks with equal GCD, and there will be no more than *log n* blocks, since GCD in one block is the divisor