Processing math: 100%

Friday, 23 July 2021

Codeforces Round #720 - 1521B. Nastia and a Good Array

This is an unofficial editorial for Nastia and a Good Array.

Problem Statement

Nastia has received an array of n positive integers as a gift.

She calls such an array a good that for all i (2<=i<=n) takes place gcd(ai1,ai) = 1, where gcd(u,v) denotes the greatest common divisor (GCD) of integers u and v.

You can perform the operation: select two different indices i, j (1<=i,j<=n,ij) and two integers x, y (1<=x,y<=2109) so that min(ai,aj)=min(x,y). Then change ai to x and aj to y.

The girl asks you to make the array good using at most n operations.

It can be proven that this is always possible.

Approach

From the statement, we can have our first obversation which is ai1 and ai are coprime, denoted by gcd(ai1,ai) = 1. Two integers are coprime if the only positive integer that is a divisor of both of them is 1, mathematically gcd(a,b)=1. Therefore, this question can be rephrased as "how to make a_i - 1 and a_i coprime using at most n operations".

Solution 1

We can use the fact that an integer x and x±1 are coprime. First let's find the min value x and its position pos in array a as we have to use this value to perform the operations. For all integer i where 1<=i<=n, we can perform (pos,i,x,abs(posi)) to replace ai to x + abs(posi). For the first test case, we can make [9,6,3,11,15] to [5,4,3,4,5] using n1 operations.

Solution 2

We can put a prime number, let's say 1e9+7 on every odd index if the minimum value is on even index and vice versa. In this case, we just need (n+1)/2 operations to turn [9,6,3,11,15] to [9,1e9+7,3,1e9+7,15].

Monday, 5 July 2021

Nezzar and Symmetric Array

This is an unofficial editorial for Nezzar and Symmetric Array.

Problem Statement

Long time ago there was a symmetric array a1, a2, ... ,a2n consisting of 2n distinct integers. Array a1, a2, ... ,a2n is called symmetric if for each integer 1<=i<=2n, there exists an integer 1<=j<=2n such that ai=aj.

For each integer 1<=i<=2n, Nezzar wrote down an integer di equal to the sum of absolute differences from ai to all integers in a, i. e. di = 2nj=1|aiaj|.

Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array 𝑎 consisting of 2n distinct integers that generates the array d.

Input

The first line contains a single integer t (1<=t<=105) — the number of test cases.

The first line of each test case contains a single integer 𝑛 (1<=n<=105).

The second line of each test case contains 2n integers d1,d2,...,d2n (1<=di<=1012).

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 105.

Output

For each test case, print "YES" in a single line if there exists a possible array 𝑎. Otherwise, print "NO".

You can print letters in any case (upper or lower).

Approach

Here's the simulation of the first example.

a = [1 -3 -1 3]
i = 0 : 0 + 4 + 2 + 2 = 8
i = 1 : 4 + 0 + 2 + 6 = 12 
i = 2 : 2 + 2 + 0 + 4 = 8 
i = 3 : 2 + 6 + 4 + 0 = 12
d = [8, 12, 8, 12]

Each element has a positive and negative value. Supposing the positive value is x, and the absolute difference to (y,y) would be |xy|+|x+y|. Similarly, for x, that would be |xy|+|x+y|, which is also |xy|+|x+y|. Therefore, for each d[i], there would be a pair as well.

For |xy|+|x+y|, we have two different cases. For x>y, |xy|+|x+y| would be 2x. For x>y, |xy|+|x+y| would be 2y. Now we know that |xy|+|x+y| = 2max(x,y).

For each d[i], it is the sum of each absolute differences from a[i] to all integers. Combined with the previous finding, we got

d[i]=2nj=1max(abs(a[i]),abs(a[j]))

Given the array d, how can we generate the array a back? Supposing the maximum element in a is called max1, the corresponding d[i] would be max12n. Therefore, we now have max1=d[i]/2/n. For the second maximum element, let's say max2, as there is only max1 which is greater than max2, all other (n1) elements are less than or equal to max2. The sum of absolute differences to them would be max22(n1). As max2<max1, we need to add max12 back to d[i].

Similarly, we can do the same thing one by one to get the original array a back. We can induce that

max1=d[i]/2/n

max2=(d[i]max12)/2/(n1)
max3=(d[i]max22)/2/(n2)
max4=(d[i]max32)/2/(n3)
maxm=(d[i]maxm12)/2/(nk)

For each elelment x, we need to check the occurence of its absolute value. If x is odd or its occurence is odd, then the answer is NO. Also, if x appears more than 2 times, that would be also NO. For each maxm, we can verify that if we can retrieve an integer, i.e. (d[i]maxm12)/2 is 0 or not. If not, that is also NO. if (d[i]maxm12)/2/(nk) appears before or it is less than or equal to 0, that is also not a correct answer. Therefore, we have 6 cases to check in total. For other cases, that would be YES.

Solution

void solve() {
    long long n; cin >> n;
    map<long long, long long> vis, m;
    for(int i = 0; i < 2 * n; i++) cin >> d[i], m[-d[i]]++;
    long long cur_sum = 0, k = 0;
    EACH(it, m) {
        long long abs_difference = -it.fi, occurrence = it.se;
        if(abs_difference % 2 || occurrence % 2 || occurrence > 2) {
            cout << "NO" << "\n";
            return;
        }
        long long p = (abs_difference - cur_sum * 2) / 2;
        long long q = n - k;
        if(p % q) {
            cout << "NO" << "\n";
            return;
        }
        p /= q;
        if(vis.count(p) || p <= 0) {
            cout << "NO" << "\n";
            return;
        }
        vis[p] = 1, cur_sum += p, k++;
    }
    cout << "YES" << "\n";
}

A Fun Problem - Math

# Problem Statement JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today t...