algorithms – How can we count the number of pairs of coprime integers in an array of integers? (CSES)

For reference, I am trying to solve this CSES Problem.

The problem basically states that given up to $10^5$ positive integers in the range $(1, 10^6)$, find the number of pairs of those positive integers that are coprime.

After some thought, I’ve come up with the following 3 ideas that I think may be on the right track, but are each missing something.

Brute-Force Idea: Simply iterate through all $O(N^2)$ pairs of integers in the array, and increment our answer every time the pair of integers has a Greatest Common Divisor of 1. Using the Euclidean Algorithm to find GCDs, we obtain a time complexity of $O(N^2logN)$, but this is too slow since $N <= 10^5$.

Idea #2 (Sieve of Erathosthenes): I thought that there had to be some significance to all the numbers being bounded by the rather low $10^6$ and not something like $10^9$ or $10^{18}$. Specifically, the bound allows us to create an Extended Sieve of Eratosthenes (basically just a normal sieve except we keep, for each number, the smallest prime number that is a factor of it). However, only comparing the smallest prime factors of 2 numbers is not enough to determine if they’re actually coprime (consider $14$ and $21$), and besides, this still involves testing all pairs of numbers, which is too slow.

Idea #3 (Principle of Inclusion and Exclusion): My most recent idea is that perhaps we can construct, for each prime number in the range $(1, 10^6)$, whichever given numbers are divisible by it. Then, the number of coprime pairs = the total number of pairs ($frac{N(N-1)}{2}$) – the number of pairs of prime numbers such that both numbers appear in at least 1 same set, and the last term can be calculated with the PIE’s formula. The problem with this is that the Principle involves the intersections of sets, and I have no idea how to calculate these intersections without re-iterating through all integers, not to mention that calculating the various terms in the formula of PIE requires iterating through all subsets of the integers ($O(2^N)$) – again, this approach seems too inefficient.

Overall, I’m pretty sure that at least one of these ideas is somehow on the right track, but I have been unable to find the missing link to the final solution. Can someone point me in the right direction? Thanks a lot!

algorithms – CSES Question – Tower

algorithms – CSES Question – Tower – Computer Science Stack Exchange

python – CSES standard solution for range query problem giving TLE

Problem statement is from CSES

This is a standard question where we are given a list of numbers and a number of queries. For each query, you have to give the sum of numbers in the given range.

My implementation in python:

def solve():
    n,q = (int(a) for a in input().split(' '))
    arr = (int(a) for a in input().split(' '))
    cumulativeArray = (0) # Initial entry added to make 1 based indexing
    sum = 0
    for i in range(len(arr)):
        sum += arr(i)
    for i in range(q):
        x,y = (int(a) for a in input().split(' '))
        print(cumulativeArray(y) - cumulativeArray(x-1))

This gives the required answers correctly for all my custom test cases. But gives TLE for huge test cases.

I believe this is happening due to split() method in my code.
Any leads are welcome.

dynamic programming – CSES Problem Set : Two Sets Problem

I saw the solution given below but could not understand the part where we only involve subsets upto n-1 term. Can anybody please explain?

This is a 0-1 knapsack in disguise. If we are to have two subsets of equal sum, they must sum to half the total sum each. 
This means if the total sum n(n+1)/2 is odd, the answer is zero (no possibilities). Otherwise we run 0-1 knapsack to get the number of ways to reach n(n+1)/4 using subsets of the numbers 1..n-1. 

Why n-1? Because by only considering numbers up to n-1, we always put n in the second set, and therefore only count each pair of sets once(otherwise we count every pair of sets twice).

DreamProxies - Cheapest USA Elite Private Proxies 100 Cheapest USA Private Proxies Buy 200 Cheap USA Private Proxies 400 Best Private Proxies Cheap 1000 USA Private Proxies 2000 USA Private Proxies 5000 Cheap USA Private Proxies - Buy Cheap Private Proxies Buy 50 Private Proxies Buy 100 Private Proxies Buy 200 Private Proxies Buy 500 Private Proxies Buy 1000 Private Proxies Buy 2000 Private Proxies New Proxy Lists Every Day Buy Quality Private Proxies