How can I find the number of pairs a,b<N such that GCD(a,b) = x? - c++

I am trying to solve the SPOJ problem PGCD, which asks how many primes appear in a table of greatest common divisors.
The first idea that came to my mind is to generate the primes first by sieving.
Then, for each prime p, see how many pairs (a, b), where a and b are less than the given bounds, satisfy GCD(a,b)=p.
For example, how many pairs less than (20, 20) satisfy GCD(a,b)=7?
Of course, as mentioned, a and b are bounded.
So is it possible to reverse GCD? Or is this solution completely invalid?

Obviously the GCD function is not reversible/invertible because, for example,
GCD(10,15) == 5
GCD(5, 15) == 5
So if you are given 5 and try to guess the inputs, it is impossible.
I might be missing something here because I don't understand what you're saying about the bounding, but I think that's your responsibility to explain the problem better. Exactly what information do you have and what information are you trying to calculate? Example inputs and ouputs would be really useful. Also, proofreading and spell-checking.

Related

C++/ Help me understand the logic

I was told to solve this problem:
given a1, ..., an are real numbers. Need to calculate min(a1, -a1a2, a1a2a3, ...,(-1)^(n+1) a1a2,... an)
but I cannot understand the logic of the task. Could you tell me what I should do?
For example, what is (-l)^n+1? I've never seen it before.
What you should do is:
use the n real numbers of input to ...
... calculate the n numbers defined by the quoted formula (though you only need one value at a time to be more efficient)
while doing so keep track of the smallest number you encounter, that is the final result
concerning the (-1)^(n+1), it is reasonable to assume (as e.g. in the comment by Weather Vane and others) that it means powers of -1 (in a lazy and unexplained but non-C++ syntax)
note that you can easily calculate one value from the previous one by simple multiplication
probably you should do all of that by writing a program, an assumption based on the fact that you are asking on StackOverflow and tag a programming language

Efficient way for generating coprime pairs

I need to print the number of coprime pairs (a,b), 0 < a <= b <= n and for n = 10^8 the program should run in less than 10 seconds. I have used this method : http://mathworld.wolfram.com/CarefreeCouple.html
But the program isn't as fast as I expected.
I have heard about an effient way of solving this problem by using something called 'Farey Sequence' but the code was written in PHP and I can only understand C.
So which method can help me solve the problem? thanks for the time.
Your stated interest is in co-prime pairs (a, b). The carefree couple adds an additional restriction that a is square-free. Therefore it is not the same problem, though some of the math is similar. As I understand your problem it is equivalent to summing the Euler totient function from 1 to n, the so-called Totient Summatory Function.
I do not know of any tricks that give one a simple closed form solution to come up with the answer. However, I think modifying a straightforward Sieve of Eratosthenes (SoT) should get you an answer in much less than 10 seconds in most programming languages.
Normally running the SoT simply yields a list of the primes <= n. Our goal will change, however, to computing the complete prime-power factorization of each integer between 1 and n inclusive. To do that we must store more than a single bit of information for each sieve entry, we must store a list. As we sieve a prime p through the array, we add (p, 1) to the list already stored at that entry. Then we sieve by p2 and change the (p,1) entries in each location we hit to (p,2), and so one for each power of p <= n and every p <= n. When it finishes, you
can compute the Totient function quickly for every value 0 <= x <= n and sum them up.
EDIT:
I see that there is already a much better set of answers to the question on math.stackexchange.com here. I'll leave this answer up for awhile until the disposition of the question is settled.

Solving equation. Counting (x,y)

I'm having a trouble with my math:
Assume that we have a function: F(x,y) = P; And my question is: what would be the most efficient way of counting up suitable (x,y) plots for this function ? It means that I don't need the coordinates themself, but I need a number of them. P is in a range: [0 ; 10^14]. "x" and "y" are integers. Is it solved using bruteforce or there are some advanced tricks(math / programming language(C,C++)) to solve this fast enough ?
To be more concrete, the function is: x*y - ((x+y)/2) + 1.
x*y - ((x+y)/2) + 1 == P is equivalent to (2x-1)(2y-1) == (4P-3).
So, you're basically looking for the number of factorizations of 4P-3. How to factor a number in C or C++ is probably a different question, but each factorization yields a solution to the original equation. [Edit: in fact two solutions, since if A*B == C then of course (-A)*(-B) == C also].
As far as the programming languages C and C++ are concerned, just make sure you use a type that's big enough to contain 4 * 10^14. int won't do, so try long long.
You have a two-parameter function and want to solve it for a given constant.
This is a pretty big field in mathematics, and there are probably dozens of algorithms of solving your equation. One key idea that many use is the fact that if you find a point where F<P and then a point F>P, then somewhere between these two points, F must equal P.
One of the most basic algorithms for finding a root (or zero, which you of course can convert to by taking F'=F-P) is Newton's method. I suggest you start with that and read your way up to more advanced algorithms. This is a farily large field of study, so happy reading!
Wikipedia has a list of root-finding algorithms that you can use as a starting place.

Is long long in C++ known to be very nasty in terms of precision?

The Given Problem:
Given a theater with n rows, m seats, and a list of seats that are reserved. Given these values, determine how many ways two friends can sit together in the same row.
So, if the theater was a size of 2x3 and the very first seat in the first row was reserved, there would be 3 different seatings that these two guys can take.
The Problem That I'm Dealing With
The function itself is supposed to return the number of seatings that there are based on these constraints. The return value is a long long.
I've gone through my code many many times...and I'm pretty sure that it's right. All I'm doing is incrementing this one value. However, ALL of the values that my function return differ from the actual solution by 1 or 2.
Any ideas? And if you think that it's just something wrong with my code, please tell me. I don't mind being called an idiot just as long as I learn something.
Unless you're overflowing or underflowing, it definitely sounds like something is wrong with your code. For integral types, there are no precision ambiguities in c or c++
First, C++ doesn't have a long long type. Second, in C99, long long can represent any integral value from LLONG_MIN (<= -2^63) to LLONG_MAX (>= 2^63 - 1) exactly. The problem lies elsewhere.
Given the description of the problem, I think it is unambiguous.
Normally, the issue is that you don't know if the order in which the combinations are taken is important or not, but the example clearly disambiguate: if the order was important we would have 6 solutions, not 3.
What is the value that your code gives for this toy example ?
Anyway I can add a few examples with my own values if you wish, so that you can compare against them, I can't do much more for you unless you post your code. Obviously, the rows are independent so I'm only going to show the result row by row.
X occupied seat
. free seat
1: X..X
1: .X..
2: X...X
3: X...X..
5: ..X.....
From a computation point of view, I should note it's (at least) an O(N) process where N is the number of seats: you have to inspect nearly each seat once, except the first (and last) ones in case the second (and next to last) are occupied; and that's effectively possible to solve this linearly.
From a technic point of view:
make sure you initialize your variable to 0
make sure you don't count too many seats on toy example
I'd be happy to help more but I would not like to give you the full solution before you have a chance to think it over and review your algorithm calmly.

How can I solve this median programming problem in C++

Formulate the steps of identifying the median from five unique numbers and visualize them in flow chart.
Develop an application that shows the median after getting five unique numbers from users.
Extend the feature for allowing six unique numbers input and computing the median.
Example:
Input: 5 4 2 1 10
Output: Median = 4
I found this question in a Problem Solving with C++ by Walter Savitch but I couldn't solve it. Can anyone explain it to me?
Trying to give homework-friendly advice:
1) Make sure you know how to get a Median. Can you, in your head or on paper, figure it out? Now, how do you write a program to do this for you? Make a flowchart.
2) Write the program to do it. A user gives your program 5 numbers, your program gives the median as an answer.
3) Make the program better. An even amount of numbers changes the method to get a median. Change your program so that it will allow 6 numbers.
3b) Make your program accept any amount of numbers. (I added this, not in your post or in your book but should be super-easy to do if you've already done 2 and 3).
Can anyone explain it to me?
"Formulate the steps" means, "explain how to do it". For example, imagine that you're explaining to me how to solve the problem, that I don't need to use a computer (that I'm trying to do it with pencil and paper), and that I don't know what a "median" is.
"Develop an application" means, "write software". The software will need to: a) get five numbers from the user (and, possibly, ensure that the numbers are "unique"); b) find the "median" (using the steps you've previously formulated in step 1); c) show (output) the median which it found.
You'll need to define what "median" means when there's an even number of inputs, and alter your program accordingly.
I know no one asked for an answer using STL, but it could be useful for someone coming here later.
In C++ with STL there is a function called nth_element, which takes three arguments. It will sort a container just enough to get nth element in the right spot.
An example:
int numbers[] = { 5, 4, 2, 1, 10 };
std::nth_element(numbers, numbers+2, numbers+5);
std::cout << numbers[2] << "\n";