Codeforces Round 891 (Div. 3)


A. Array Coloring

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

You are given an array consisting of n n n integers. Your task is to determine whether it is possible to color all its elements in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored.

You are given an array consisting of 𝑛
integers. Your task is to determine whether it is possible to color all its elements in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored.

For example, if the array is [ 1 , 2 , 4 , 3 , 2 , 3 , 5 , 4 1,2,4,3,2,3,5,4 1,2,4,3,2,3,5,4], we can color it as follows: [ 1 , 2 , 4 , 3 , 2 , 3 , 5 , 4 \color{blue}{1},\color{blue}{2},\color{red}{4},\color{blue}{3},\color{red}{2},\color{red}{3},\color{red}{5},\color{red}{4} 1,2,4,3,2,3,5,4], where the sum of the blue elements is 6 6 6 and the sum of the red elements is 18 18 18
.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases.

Each test case begins with a line containing an integer n n n ( 2 ≤ n ≤ 50 2 \le n \le 50 2n50) — the length of the array a a a.

The next line contains n n n integers a 1 , a 2 , … , a n a_1,a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 50 1 \le a_i \le 50 1ai50) — the elements of the array a a a.

Output

For each test case, output “YES” (without quotes) if it is possible to color the array in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored, and “NO” otherwise.

You can output “Yes” and “No” in any case (for example, the strings “yES”, “yes”, and “Yes” will be recognized as correct answers).

Example
input

7
8
1 2 4 3 2 3 5 4
2
4 7
3
3 9 8
2
1 7
5
5 4 3 2 1
4
4 3 4 5
2
50 48

output

YES
NO
YES
YES
NO
YES
YES

Note

The first sample is described in the statement.

In the second sample, there are only two colorings [ 4 , 7 ] \color{blue}{4},\color{red}{7}] 4,7] and [ 4 , 7 [\color{red}{4},\color{blue}{7} [4,7] , but in both cases the parity of sums is different.

In the third sample, you can color [ 3 , 9 , 8 \color{blue}{3},\color{blue}{9},\color{red}{8} 3,9,8] and 12 12 12 and 8 8 8 are both even.

Tutorial
数组 a a a 总和为偶数输出 YES, 否则输出NO

Solution

for _ in range(int(input())):
    input()
    print("NO" if sum(map(int, input().split())) & 1 else "YES")

B. Maximum Rounding

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Given a natural number x x x. You can perform the following operation:

  • choose a positive integer k k k and round x x x to the k k k-th digit

Note that the positions are numbered from right to left, starting from zero. If the number has k k k digits, it is considered that the digit at the k k k-th position is equal to 0 0 0.

The rounding is done as follows:

  • if the digit at the ( k − 1 ) (k-1) (k1)-th position is greater than or equal to 5 5 5, then the digit at the k k k-th position is increased by 1 1 1, otherwise the digit at the k k k-th position remains unchanged (mathematical rounding is used).

  • if before the operations the digit at the k k k-th position was 9 9 9, and it should be increased by 1 1 1, then we search for the least position k ′ k' k ( k ′ > k k'>k k>k), where the digit at the k ′ k' k-th position is less than 9 9 9 and add 1 1 1 to the digit at the k ′ k' k-th position. Then we assign k = k ′ k=k' k=k.

  • after that, all digits which positions are less than k k k are replaced with zeros.

Your task is to make x x x as large as possible, if you can perform the operation as many times as you want.

For example, if x x x is equal to 3451 3451 3451, then if you choose consecutively:

  • k = 1 k=1 k=1, then after the operation x x x will become 3450 3450 3450
  • k = 2 k=2 k=2, then after the operation x x x will become 3500 3500 3500
  • k = 3 k=3 k=3, then after the operation x x x will become 4000 4000 4000
  • k = 4 k=4 k=4, then after the operation x x x will become 0 0 0

To maximize the answer, you need to choose k = 2 k=2 k=2 first, and then k = 3 k=3 k=3, then the number will become 4000 4000 4000.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1t104) — the number of test cases.

Each test case consists of positive integer x x x with a length of up to 2 ⋅ 1 0 5 2 \cdot 10^5 2105. It is guaranteed that there are no leading zeros in the integer.

It is guaranteed that the sum of the lengths of all integers x x x over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each set of input data, output the maximum possible value of x x x after the operations. The number should not have leading zeros in its representation.

Example
input

10
1
5
99
913
1980
20444
20445
60947
419860
40862016542130810467

output

1
10
100
1000
2000
20444
21000
100000
420000
41000000000000000000

Note

In the first sample, it is better not to perform any operations.

In the second sample, you can perform one operation and obtain 10 10 10.

In the third sample, you can choose k = 1 k=1 k=1 or k = 2 k=2 k=2. In both cases the answer will be 100 100 100.

Tutorial
按题目要求倒序走一遍,在遍历过程中记录最后一个四舍五入的位置,最后在该位置后的所有数位都为0

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    string s;
    int idx = 0;
    cin >> s;
    ranges::reverse(s);
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] > '4') {
            s[i] = '0';
            idx = i + 1;
            if (i == s.size() - 1) {
                s += '1';
                break;
            } else {
                s[i + 1] += 1;
            }
        }
    }
    for (int i = 0; i < idx; ++i) {
        s[i] = '0';
    }
    ranges::reverse(s);
    cout << s << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _; cin >> _; while (_--)
    solve();
    return 0;
}

C. Assembly via Minimums

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Sasha has an array a a a of n n n integers. He got bored and for all i i i, j j j ( i < j i < j i<j), he wrote down the minimum value of a i a_i ai and a j a_j aj. He obtained a new array b b b of size n ⋅ ( n − 1 ) 2 \frac{n\cdot (n-1)}{2} 2n(n1).

For example, if a = a= a= [ 2 , 3 , 5 , 1 2,3,5,1 2,3,5,1], he would write [ min ⁡ ( 2 , 3 ) , min ⁡ ( 2 , 5 ) , min ⁡ ( 2 , 1 ) , min ⁡ ( 3 , 5 ) , min ⁡ ( 3 , 1 ) , m i n ( 5 , 1 ) \min(2, 3), \min(2, 5), \min(2, 1), \min(3, 5), \min(3, 1), min(5, 1) min(2,3),min(2,5),min(2,1),min(3,5),min(3,1),min(5,1)] = = = [ 2 , 2 , 1 , 3 , 1 , 1 2, 2, 1, 3, 1, 1 2,2,1,3,1,1].

Then, he randomly shuffled all the elements of the array b b b.

Unfortunately, he forgot the array a a a, and your task is to restore any possible array a a a from which the array b b b could have been obtained.

The elements of array a a a should be in the range [ − 1 0 9 , 1 0 9 ] [-10^9,10^9] [109,109].

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 200 1\le t\le 200 1t200) — the number of test cases.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 3 2\le n\le 10^3 2n103) — the length of array a a a.

The second line of each test case contains n ⋅ ( n − 1 ) 2 \frac{n\cdot (n-1)}{2} 2n(n1) integers b 1 , b 2 , … , b n ⋅ ( n − 1 ) 2 b_1,b_2,\dots,b_{\frac{n\cdot (n-1)}{2}} b1,b2,,b2n(n1) ( − 1 0 9 ≤ b i ≤ 1 0 9 −10^9\le b_i\le 10^9 109bi109) — the elements of array b b b.

It is guaranteed that the sum of n n n over all tests does not exceed 1 0 3 10^3 103 and for each array b b b in the test, there exists an original array.

Output

For each test case, output any possible array a a a of length n n n.

Example
input

5
3
1 3 1
2
10
4
7 5 3 5 3 3
5
2 2 2 2 2 2 2 2 2 2
5
3 0 0 -2 0 -2 0 0 -2 -2

output

1 3 3
10 10
7 5 3 12
2 2 2 2 2
0 -2 0 3 5

Note

In the first sample, Sasha chose the array [ 1 , 3 , 3 ] [1,3,3] [1,3,3], then the array b b b will look like [ min ⁡ ( a 1 , a 2 ) = 1 , min ⁡ ( a 1 , a 3 ) = 1 , min ⁡ ( a 2 , a 3 ) = 3 ] [\min(a_1,a_2)=1, \min(a_1,a_3)=1, \min(a_2,a_3)=3] [min(a1,a2)=1,min(a1,a3)=1,min(a2,a3)=3], after shuffling its elements, the array can look like [ 1 , 3 , 1 ] [1,3,1] [1,3,1].

In the second sample, there is only one pair, so the array [ 10 , 10 ] [10,10] [10,10] is suitable. Another suitable array could be [ 15 , 10 ] [15,10] [15,10].

Tutorial
从小到大遍历每一个数字,对于数组中的 a i a_i ai ,肯定有 l e n ( a ) − i − 1 len(a) - i - 1 len(a)i1 个数大于等于这个数,所以每次只需要在 b b b 数组中移动 l e n ( a ) − i − 1 len(a) - i - 1 len(a)i1 就可以找到下一个数

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int n;
    cin >> n;
    int x = 0, cnt = n;
    vector<int> b(n * (n - 1) / 2);
    for (int i = 0; i < n * (n - 1) / 2; ++i) {
        cin >> b[i];
    }
    ranges::sort(b);
    while (x < n * (n - 1) / 2) {
        cout << b[x] << " ";
        --cnt;
        x += cnt;
    }
    cout << 1000000000 << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _; cin >> _; while (_--)
    solve();
    return 0;
}

D. Strong Vertices

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Given two arrays a a a and b b b, both of length n n n. Elements of both arrays indexed from 1 1 1 to n n n. You are constructing a directed graph, where edge from u u u to v v v ( u ≠ v u\neq v u=v) exists if a u − a v ≥ b u − b v a_u-a_v \ge b_u-b_v auavbubv.

A vertex V V V is called strong if there exists a path from V V V to all other vertices.

A path in a directed graph is a chain of several vertices, connected by edges, such that moving from the vertex u u u, along the directions of the edges, the vertex v v v can be reached.

Your task is to find all strong vertices.

For example, if a = [ 3 , 1 , 2 , 4 ] a=[3,1,2,4] a=[3,1,2,4] and b = [ 4 , 3 , 2 , 1 ] b=[4,3,2,1] b=[4,3,2,1], the graph will look like this:

The graph has only one strong vertex with number 4 4 4

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1t104) — the number of test cases.

The first line of each test case contains an integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2\cdot 10^5 2n2105) — the length of a a a and b b b.

The second line of each test case contains n n n integers a 1 , a 2 … a n a_1,a_2 \dots a_n a1,a2an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 109ai109) — the array a a a.

The third line of each test case contains n n n integers b 1 , b 2 … b n b_1,b_2 \dots b_n b1,b2bn ( − 1 0 9 ≤ b i ≤ 1 0 9 -10^9 \le b_i \le 10^9 109bi109) — the array b b b.

It is guaranteed that the sum of n n n for all test cases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105.

Output

For each test case, output two lines: in the first line, output the number of strong vertices, and in the second line, output all strong vertices in ascending order.

Example
input

5
4
3 1 2 4
4 3 2 1
5
1 2 4 1 2
5 2 3 3 1
2
1 2
2 1
3
0 2 1
1 3 2
3
5 7 4
-2 -3 -6

output

1
4 
2
3 5 
1
2 
3
1 2 3 
2
2 3 

Note

The first sample is covered in the problem statement.

For the second sample, the graph looks like this:

The graph has two strong vertices with numbers 3 3 3 and 5 5 5. Note that there is a bidirectional edge between vertices 3 3 3 and 5 5 5.

In the third sample, the vertices are connected by a single directed edge from vertex 2 2 2 to vertex 1 1 1, so the only strong vertex is 2 2 2.

In the fourth sample, all vertices are connected to each other by bidirectional edges, so there is a path from every vertex to any other vertex.

Tutorial
对于式子 a u − a v ≥ b u − b v a_u-a_v \ge b_u-b_v auavbubv ,可以变形为 a u − b u ≥ a v − b v a_u-b_u \ge a_v-b_v aubuavbv,根据题意分析,对于每个 strong vertices,其 a u − b u a_u-b_u aubu 一定是最大的,本题就变为求最大的 a u − b u a_u-b_u aubu 是哪些点

Solution

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define endl '\n'
#define int long long
#define PII pair<int, int>

vector<PII> all;

void solve() {
    all.clear();
    int n, cnt = 0, x;
    cin >> n;
    vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; ++i) {
        cin >> x;
        all.emplace_back(a[i] - x, i);
    }
    ranges::sort(all, [](PII a, PII b) {
        if (a.fi == b.fi) {
            return a.se < b.se;
        }
        return a.fi > b.fi;
    });
    cout << count_if(all.begin(), all.end(), [](PII x) {
        return x.fi == all[0].fi;
    }) << endl;
    for (PII ai : all) {
        if (ai.fi == all[0].fi) {
            cout << ai.se << " ";
        } else {
            break;
        }
    }
    cout << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _; cin >> _; while (_--)
    solve();
    return 0;
}

E. Power of Points

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

You are given n n n points with integer coordinates x 1 , … x n x_1,\dots x_n x1,xn, which lie on a number line.

For some integer s s s, we construct segments [ s , x 1 s,x_1 s,x1], [ s , x 2 s,x_2 s,x2], … \dots , [ s , x n s,x_n s,xn]. Note that if x i < s x_i<s xi<s, then the segment will look like [ x i , s x_i,s xi,s]. The segment [ a , b a, b a,b] covers all integer points a , a + 1 , a + 2 , … , b a, a+1, a+2, \dots, b a,a+1,a+2,,b.

We define the power of a point p p p as the number of segments that intersect the point with coordinate p p p, denoted as f p f_p fp.

Your task is to compute ∑ p = 1 1 0 9 f p \sum\limits_{p=1}^{10^9}f_p p=1109fp for each s ∈ { x 1 , … , x n } s \in \{x_1,\dots,x_n\} s{x1,,xn}, i.e., the sum of f p f_p fp for all integer points from 1 1 1 to 1 0 9 10^9 109.

For example, if the initial coordinates are [ 1 , 2 , 5 , 7 , 1 ] [1,2,5,7,1] [1,2,5,7,1] and we choose s = 5 s=5 s=5, then the segments will be: [ 1 , 5 ] [1,5] [1,5], [ 2 , 5 ] [2,5] [2,5], [ 5 , 5 ] [5,5] [5,5], [ 5 , 7 ] [5,7] [5,7], [ 1 , 5 ] [1,5] [1,5]. And the powers of the points will be: f 1 = 2 , f 2 = 3 , f 3 = 3 , f 4 = 3 , f 5 = 5 , f 6 = 1 , f 7 = 1 , f 8 = 0 , … , f 1 0 9 = 0 f_1=2, f_2=3, f_3=3, f_4=3, f_5=5, f_6=1, f_7=1, f_8=0, \dots, f_{10^9}=0 f1=2,f2=3,f3=3,f4=3,f5=5,f6=1,f7=1,f8=0,,f109=0. Their sum is 2 + 3 + 3 + 3 + 5 + 1 + 1 = 18 2+3+3+3+5+1+1=18 2+3+3+3+5+1+1=18.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1t104) — the number of test cases.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2\cdot 10^5 1n2105) — the number of points.

The second line contains n n n integers x 1 , x 2 … x n x_1,x_2 \dots x_n x1,x2xn ( 1 ≤ x i ≤ 1 0 9 1 \le x_i \le 10^9 1xi109) — the coordinates of the points.

It is guaranteed that the sum of the values of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105.

Output

For each test case, output n n n integers, where the i i i-th integer is equal to the sum of the powers of all points for s = x i s=x_i s=xi.

Example
input

3
3
1 4 3
5
1 2 5 7 1
4
1 10 100 1000

output

8 7 6
16 15 18 24 16
1111 1093 1093 2893

Note

In the first test case we first choose s = x 1 = 1 s=x_1=1 s=x1=1, then the following segments are formed: [ 1 , 1 ] [1,1] [1,1], [ 1 , 4 ] [1,4] [1,4], [ 1 , 3 ] [1,3] [1,3].

The powers of the points will be as follows: f 1 = 3 , f 2 = 2 , f 3 = 2 , f 4 = 1 , f 5 = 0 … f_1=3, f_2=2, f_3=2, f_4=1, f_5=0 \dots f1=3,f2=2,f3=2,f4=1,f5=0 The sum of powers of the points: 3 + 2 + 2 + 1 + 0 + ⋯ + 0 = 8 3+2+2+1+0+\dots+0=8 3+2+2+1+0++0=8.

After that we choose s = x 2 = 4 s=x_2=4 s=x2=4. Then there will be such segments: [ 1 , 4 ] [1,4] [1,4], [ 4 , 4 ] [4,4] [4,4], [ 3 , 4 ] [3,4] [3,4], and powers of the points are f 1 = 1 , f 2 = 1 , f 3 = 2 , f 4 = 3 f_1=1, f_2=1, f_3=2, f_4=3 f1=1,f2=1,f3=2,f4=3.

At the end we take s = x 3 = 3 s=x_3=3 s=x3=3 and the segments look like this: [ 1 , 3 ] [1,3] [1,3], [ 3 , 4 ] [3,4] [3,4], [ 3 , 3 ] [3,3] [3,3], the powers of the points are f 1 = 1 , f 2 = 1 , f 3 = 3 , f 4 = 1 f_1=1, f_2=1, f_3=3, f_4=1 f1=1,f2=1,f3=3,f4=1.

Tutorial
先对给的 x x x 排序
假如排序后的数组是 [ 1 , 3 , 5 , 7 , 9 1,3,5,7,9 1,3,5,7,9]

  • 假设 s s s 5 5 5,区间[ 1 , 2 1,2 1,2] +1、[ 3 , 4 3,4 3,4] +2、[ 5 , 5 5,5 5,5] +5、[ 6 , 7 6,7 6,7] +2、[ 8 , 9 8,9 8,9] +1
  • 假设 s s s 7 7 7,区间[ 1 , 2 1,2 1,2] +1、[ 3 , 4 3,4 3,4] +2、[ 5 , 6 5,6 5,6] +3、[ 7 , 7 7,7 7,7] +5、[ 8 , 9 8,9 8,9] +1

其实就是除了 x = s x = s x=s,左边和右边阶梯递减,走到一个 x x x 就 -1
x = s x = s x=s 贡献是 n n n
对于 s = 7 s = 7 s=7 s s s 的位置是 4 4 4 7 7 7 的前缀可以这么计算: 7 − 1 + 7 − 3 + 7 − 5 < = > ( 4 − 1 ) ∗ s − p r e [ 4 − 1 ] 7-1+7-3+7-5<=>(4-1)*s-pre[4-1] 71+73+75<=>(41)spre[41],后缀同理

最后用前缀和、后缀和计算结果

Solution

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define endl '\n'
#define int long long
#define PII pair<int, int>

void solve() {
    int n, x, sumleft = 0, sumright = 0;
    cin >> n;
    vector<int> ans(n);
    vector<PII> all;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        sumright += x;
        all.emplace_back(x, i);
    }
    ranges::sort(all);
    sumright -= all[0].fi;
    for (int i = 0; i < n; ++i) {
        ans[all[i].se] = all[i].fi * i - sumleft + sumright - all[i].fi * (n - i - 1) + n;
        if (i == n - 1) {
            break;
        }
        sumleft += all[i].fi;
        sumright -= all[i + 1].fi;
    }
    for (int i = 0; i < n; ++i) {
        cout << ans[i] << " \n"[i == n - 1];
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _; cin >> _; while (_--)
    solve();
    return 0;
}

F. Sum and Product

time limit per test: 4 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

You have an array a a a of length n n n.

Your task is to answer q q q queries: given x , y x,y x,y, find the number of pairs i i i and j j j ( 1 ≤ i < j ≤ n 1 \le i < j \le n 1i<jn) that both a i + a j = x a_i + a_j = x ai+aj=x and a i ⋅ a j = y a_i \cdot a_j = y aiaj=y.

That is, for the array [ 1 , 3 , 2 ] [1,3,2] [1,3,2] and asking for x = 3 , y = 2 x=3,y=2 x=3,y=2 the answer is 1 1 1:

  • i = 1 i=1 i=1 and j = 2 j=2 j=2 fail because 1 + 3 = 4 1 + 3 = 4 1+3=4 and not 3 , 3, 3, also 1 ⋅ 3 = 3 1 \cdot 3=3 13=3 and not 2 2 2;
  • i = 1 i=1 i=1 and j = 3 j=3 j=3 satisfies both conditions;
  • i = 2 i=2 i=2 and j = 3 j=3 j=3 fail because 3 + 2 = 5 3 + 2 = 5 3+2=5 and not 3 , 3, 3, also 3 ⋅ 2 = 6 3 \cdot 2=6 32=6 and not 2 2 2;

Input

The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1t104) — the number of test cases.

The second line of each test case contains one integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2\cdot 10^5 1n2105) — the length of the array a a a.

The third line of each test case contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\dots,a_n a1,a2,,an ( 1 ≤ ∣ a i ∣ ≤ 1 0 9 1 \le |a_i| \le 10^9 1ai109) — array a a a.

The fourth line of each test case contains the integer q q q ( 1 ≤ q ≤ 2 ⋅ 1 0 5 1 \le q \le 2\cdot 10^5 1q2105) — the number of requests.

The next q q q lines contain two numbers each x x x and y y y ( 1 ≤ ∣ x ∣ ≤ 2 ⋅ 1 0 9 , 1 ≤ ∣ y ∣ ≤ 1 0 18 1 \le |x|\le 2\cdot 10^9,1\le |y|\le 10^{18} 1x2109,1y1018) — request.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105. This is also guaranteed for the sum of q q q values.

Output

For each test case print a line with q q q numbers — the answers to the queries.

Example
input

3
3
1 3 2
4
3 2
5 6
3 1
5 5
4
1 1 1 1
1
2 1
6
1 4 -2 3 3 3
3
2 -8
-1 -2
7 12

output

1 1 0 0 
6 
1 1 3 

Note

For the first test case, let’s analyze each pair of numbers separately:

  • pair ( a 1 , a 2 ) (a_1,a_2) (a1,a2): a 1 + a 2 = 4 a_1 + a_2 = 4 a1+a2=4, a 1 ⋅ a 2 = 3 a_1 \cdot a_2 = 3 a1a2=3
  • pair ( a 1 , a 3 ) (a_1,a_3) (a1,a3): a 1 + a 3 = 3 a_1 + a_3 = 3 a1+a3=3, a 1 ⋅ a 3 = 2 a_1 \cdot a_3 = 2 a1a3=2
  • pair ( a 2 , a 3 ) (a_2,a_3) (a2,a3): a 2 + a 3 = 5 a_2 + a_3 = 5 a2+a3=5, a 2 ⋅ a 3 = 6 a_2 \cdot a_3 = 6 a2a3=6

From this, we can see that for the first query, the pair ( a 1 , a 3 ) (a_1,a_3) (a1,a3) is suitable, for the second query, it is ( a 2 , a 3 ) (a_2,a_3) (a2,a3), and there are no suitable pairs for the third and fourth queries.

In the second test case, all combinations of pairs are suitable.

Tutorial
由题意得,本题需要查询有多少个 i , j i, j i,j 同时满足 a [ i ] + a [ j ] = x , a [ i ] ∗ a [ j ] = y a[i] +a [j] = x,a[i] * a[j] = y a[i]+a[j]=xa[i]a[j]=y
易推 a [ i ] − a [ j ] = s q r t ( x 2 − 4 ∗ y ) a[i] - a[j] = sqrt(x^2 - 4 * y) a[i]a[j]=sqrt(x24y)
由于知道 x x x y y y ,则 a i a_i ai a j a_j aj 都可以计算得出,由于可能出现 a i = a j a_i = a_j ai=aj 的情况,所以在计算结果时需要特判

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int n, q, x, y, a, b;
    cin >> n;
    map<int, int> mp;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        mp[x]++;
    }
    cin >> q;
    while (q--) {
        cin >> x >> y;
        int ans = 0;
        int z = sqrt(x * x - 4 * y);
        a = (x + z) / 2, b = (x - z) / 2;
        if (z < 0 or (a + b) ^ x or (a * b) ^ y) {
            cout << 0 << " ";
        } else if (a == b) {
            cout << mp[a] * (mp[a] - 1) / 2 << " ";
        } else {
            cout << mp[a] * mp[b] << " ";
        }
    }
    cout << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _; cin >> _; while (_--)
    solve();
    return 0;
}

G. Counting Graphs

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Given a tree consisting of n n n vertices. A tree is a connected undirected graph without cycles. Each edge of the tree has its weight, w i w_i wi.

Your task is to count the number of different graphs that satisfy all four conditions:

  1. The graph does not have self-loops and multiple edges.
  2. The weights on the edges of the graph are integers and do not exceed S S S.
  3. The graph has exactly one minimum spanning tree.
  4. The minimum spanning tree of the graph is the given tree.

Two graphs are considered different if their sets of edges are different, taking into account the weights of the edges.

The answer can be large, output it modulo 998244353 998244353 998244353.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1t104) — the number of test cases.

The first line of each test case contains two integers n n n and S S S ( 2 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ S ≤ 1 0 9 2 \le n \le 2 \cdot 10^5, 1\le S\le 10^9 2n2105,1S109) — the number of vertices and the upper bound of the weights.

Then follow n − 1 n-1 n1 lines describing the tree, the i i i-th line contains three integers u i u_i ui, v i v_i vi, and w i w_i wi ( 1 ≤ u i , v i ≤ n , u i ≠ v i , 1 ≤ w i ≤ S 1\le u_i,v_i\le n, u_i \ne v_i, 1\le w_i\le S 1ui,vin,ui=vi,1wiS) — an edge in the tree with weight w i w_i wi.

It is guaranteed that the sum of n n n for all tests does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105.

Output

For each test, output the number of different graphs that satisfy the conditions, modulo 998244353 998244353 998244353.

Example
input

4
2 5
1 2 4
4 5
1 2 2
2 3 4
3 4 3
5 6
1 2 3
1 3 2
3 4 6
3 5 1
10 200
1 2 3
2 3 33
3 4 200
1 5 132
5 6 1
5 7 29
7 8 187
7 9 20
7 10 4

output

1
8
80
650867886

Note

In the first sample, there is only one graph, which is the given tree.

In the second samle, the given tree looks like this:

All possible graphs for the second sample are shown below, the minimum spanning tree is highlighted in red:

Tutorial
跑一遍最小生成树,同时计算答案,在连边的时候,除了当前正在连接的边,任何一对左边的点 u u u 和右边的点 v v v 都选择加边或者不加边,即可以选择执行操作的点对数是 左边的点 * 右边的点 - 1
此时如果加边,由于需要保证最小生成树唯一,所以加的边权必须比当前合并的边大,因为有最大边权的限制,所以每个点对可以执行的操作是 s − w s - w sw,再加上不加边的一种情况,所以此时有 s − w + 1 s - w + 1 sw+1 种情况
最后只需要使用快速幂将答案乘以 ( S − w i + 1 ) s u ⋅ s v − 1 (S-w_i+1)^{s_u\cdot s_v-1} (Swi+1)susv1

Solution

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define endl '\n'
#define int long long
#define PII pair<int, int>
#define PIII pair<int, PII>
const int mod = 998244353;

int qmi(int a, int b) {
    int ans = 1;
    while (b) {
        if (b & 1) {
            ans = a * ans % mod;
        }
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}

void solve() {
    int n, s, u, v, w, ans = 1;
    cin >> n >> s;
    vector<PIII> a;
    vector<int> mp(n), cnt(n);
    for (int i = 0; i < n; ++i) {
        mp[i] = i;
        cnt[i] = 1;
    }
    for (int i = 1; i < n; ++i) {
        cin >> u >> v >> w;
        a.push_back({w, {u - 1, v - 1}});
    }
    ranges::sort(a);

    function<int(int)> dfs = [&](int x) -> int {
        if (mp[x] ^ x) {
            mp[x] = dfs(mp[x]);
        }
        return mp[x];
    };

    for (int i = 0; i < n - 1; ++i) {
        u = dfs(a[i].se.fi), v = dfs(a[i].se.se), w = a[i].fi;
        mp[u] = v;
        ans = ans * qmi(s - w + 1, cnt[u] * cnt[v] - 1) % mod;
        cnt[v] += cnt[u];
    }
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _; cin >> _; while (_--)
    solve();
    return 0;
}

写在后面

本次 div3 感觉有一丝丝脑筋急转弯的意味,只要想到解题思路了并不难解决,感觉用到的解题思想也比较有趣,最后一题的最小生成树解题思想感觉可以细细品味😊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值