codeforces 1979

Posted on By ᵇᵒ

codeforces 1979

problem A

Guess the Maximum

Alice and Bob came up with a rather strange game. They have an array of integers \(𝑎_1,𝑎_2,…,𝑎_𝑛\). Alice chooses a certain integer 𝑘 and tells it to Bob, then the following happens:

  • Bob chooses two integers 𝑖 and 𝑗 (1≤𝑖<𝑗≤𝑛), and then finds the maximum among the integers \(𝑎_𝑖,𝑎_{𝑖+1},…,𝑎_𝑗\);
  • If the obtained maximum is strictly greater than 𝑘, Alice wins, otherwise Bob wins.

Help Alice find the maximum 𝑘 at which she is guaranteed to win.

Input
Each test consists of multiple test cases. The first line contains a single integer 𝑡 \((1≤𝑡≤10^4)\) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer \(𝑛 (2≤𝑛≤5⋅10^4)\) — the number of elements in the array.

The second line of each test case contains 𝑛 integers \(𝑎_1,𝑎_2,…,𝑎_𝑛 (1≤𝑎_𝑖≤10^9)\) — the elements of the array.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed \(5⋅10^4\).

Output
For each test case, output one integer — the maximum integer 𝑘 at which Alice is guaranteed to win.

Example

input
6
4
2 4 1 7
5
1 2 3 4 5
2
1 1
3
37 8 16
5
10 10 10 10 9
10
3 12 9 5 2 3 2 9 8 2

output
3
1
0
15
9
2

Note
In the first test case, all possible subsegments that Bob can choose look as follows: [2,4],[2,4,1],[2,4,1,7],[4,1],[4,1,7],[1,7]. The maximums on the subsegments are respectively equal to 4,4,7,4,7,7. It can be shown that 3 is the largest integer such that any of the maximums will be strictly greater than it.

In the third test case, the only segment that Bob can choose is [1,1]. So the answer is 0.

solution

fun main() {
    repeat(readln().toInt()) {
        println(solve())
    }
}
 
fun solve(): Int {
    val n = readln().toInt()
    val a = readln().split(' ').map(String::toInt)
    var min = Int.MAX_VALUE
    repeat(n - 1) {
        min = min.coerceAtMost(a[it].coerceAtLeast(a[it + 1]))
    }
    return min - 1
}

problem B

XOR Sequences

You are given two distinct non-negative integers 𝑥 and 𝑦. Consider two infinite sequences \(𝑎_1,𝑎_2,𝑎_3,…\) and \(𝑏_1,𝑏_2,𝑏_3,…\), where

  • \(𝑎_𝑛=𝑛⊕𝑥\);
  • \(𝑏_𝑛=𝑛⊕𝑦\).

Here, 𝑥⊕𝑦 denotes the bitwise XOR operation of integers 𝑥 and 𝑦.

For example, with 𝑥=6, the first 8 elements of sequence 𝑎 will look as follows: [7,4,5,2,3,0,1,14,…]. Note that the indices of elements start with 1.

Your task is to find the length of the longest common subsegment of sequences 𝑎 and 𝑏. In other words, find the maximum integer 𝑚 such that \(𝑎_𝑖=𝑏_𝑗,𝑎_{𝑖+1}=𝑏_{𝑗+1},…,𝑎_{𝑖+𝑚−1}=𝑏_{𝑗+𝑚−1}\) for some 𝑖,𝑗≥1.

A subsegment of sequence 𝑝 is a sequence \(𝑝_𝑙,𝑝_{𝑙+1},…,𝑝_𝑟\), where 1≤𝑙≤𝑟.

Input
Each test consists of multiple test cases. The first line contains a single integer 𝑡 \((1≤𝑡≤10^4)\) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers 𝑥 and 𝑦 \((0≤𝑥,𝑦≤10^9,𝑥≠𝑦)\) — the parameters of the sequences.

Output
For each test case, output a single integer — the length of the longest common subsegment.

Example

input
4
0 1
12 4
57 37
316560849 14570961

output
1
8
4
33554432

Note
In the first test case, the first 7 elements of sequences 𝑎 and 𝑏 are as follows:

𝑎=[1,2,3,4,5,6,7,…]

𝑏=[0,3,2,5,4,7,6,…]

It can be shown that there isn’t a positive integer 𝑘 such that the sequence [𝑘,𝑘+1] occurs in 𝑏 as a subsegment. So the answer is 1.

In the third test case, the first 20 elements of sequences 𝑎 and 𝑏 are as follows:

𝑎=[56,59,58,61,60,63,62,49,48,51,50,53,52,55,54,41,40,43,42,45,…]

𝑏=[36,39,38,33,32,35,34,45,44,47,46,41,40,43,42,53,52,55,54,49,…]

It can be shown that one of the longest common subsegments is the subsegment [41,40,43,42] with a length of 4.

solution 1

fun main() {
    repeat(readln().toInt()) {
        println(solve())
    }
}
 
fun solve(): Int {
    val (x, y) = readln().split(' ').map(String::toInt)
    val cnt = x.xor(y).countTrailingZeroBits()
    return 1.shl(cnt)
}

solution 2

fun main() {
    repeat(readln().toInt()) {
        println(solve())
    }
}

fun solve(): Int {
    var (x, y) = readln().split(' ').map(String::toInt)
    x = x xor y
    return x and -x
}

problem C

Earning on Bets

You have been offered to play a game. In this game, there are 𝑛 possible outcomes, and for each of them, you must bet a certain integer amount of coins. In the event that the 𝑖-th outcome turns out to be winning, you will receive back the amount of coins equal to your bet on that outcome, multiplied by \(𝑘_𝑖\). Note that exactly one of the 𝑛 outcomes will be winning.

Your task is to determine how to distribute the coins in such a way that you will come out ahead in the event of any winning outcome. More formally, the total amount of coins you bet on all outcomes must be strictly less than the number of coins received back for each possible winning outcome.

Input
Each test consists of multiple test cases. The first line contains a single integer 𝑡 \((1≤𝑡≤10^4)\) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer 𝑛 (1≤𝑛≤50) — the number of outcomes.

The second line of each test case contains 𝑛 integers \(𝑘_1,𝑘_2,…,𝑘_𝑛 (2≤𝑘_𝑖≤20)\) — the multiplier for the amount of coins if the 𝑖-th outcome turns out to be winning.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed \(2⋅10^5\).

Output
For each test case, output −1 if there is no way to distribute the coins as required. Otherwise, output 𝑛 integers \(𝑥_1,𝑥_2,…,𝑥_𝑛 (1≤𝑥_𝑖≤10^9)\) — your bets on the outcomes.

It can be shown that if a solution exists, there is always a solution that satisfies these constraints.

If there are multiple suitable solutions, output any of them.

Example

input
6
3
3 2 7
2
3 3
5
5 5 5 5 5
6
7 9 3 17 9 13
3
6 3 2
5
9 4 6 8 3

output
27 41 12 
1 1 
-1
1989 1547 4641 819 1547 1071 
-1
8 18 12 9 24

Note
In the first test case, the coins can be distributed as follows: 27 coins on the first outcome, 41 coins on the second outcome, 12 coins on the third outcome. Then the total amount of coins bet on all outcomes is 27+41+12=80 coins. If the first outcome turns out to be winning, you will receive back 3⋅27=81 coins, if the second outcome turns out to be winning, you will receive back 2⋅41=82 coins, if the third outcome turns out to be winning, you will receive back 7⋅12=84 coins. All these values are strictly greater than 80.

In the second test case, one way is to bet one coin on each of the outcomes.

solution 1

fun main() {
    repeat(readln().toInt()) {
        solve()
    }
}

fun solve() {
    readln()
    val a = readln().split(' ').map(String::toInt)
    val l = a.reduce { acc, i -> lcm(acc, i) }
    val b = a.map { l.div(it) }
    if (l > b.sum()) {
        println(b.joinToString(" "))
    } else {
        println(-1)
    }
}

fun lcm(m: Int, n: Int): Int {
    return m.div(gcd(m, n)).times(n)
}

fun gcd(m: Int, n: Int): Int {
    return if (n == 0) m else gcd(n, m.mod(n))
}

solution 2

import kotlin.math.abs
import kotlin.math.min

fun main() {
    repeat(readln().toInt()) {
        solve()
    }
}

fun solve() {
    readln()
    val a = readln().split(' ').map(String::toInt)
    val l = a.reduce { acc, i -> lcm(acc, i) }
    val b = a.map { l.div(it) }
    if (l > b.sum()) {
        println(b.joinToString(" "))
    } else {
        println(-1)
    }
}

fun lcm(m: Int, n: Int): Int {
    return m.div(gcd(m, n)).times(n)
}

// 速度更快的最大公约数求法,详见博客最小公倍数一文
fun gcd(m: Int, n: Int): Int {
    if (m == n) return m
    return if ((m.and(n).and(1)) == 0) {
        val shiftRightM = (m + 1).and(1)
        val shiftRightN = (n + 1).and(1)
        gcd(m.shr(shiftRightM), n.shr(shiftRightN)).shl(shiftRightM.and(shiftRightN))
    } else {
        gcd(abs(m - n), min(m, n))
    }
}

solution 3

fun main() {
    val l = (2..20).reduce { acc, i -> lcm(acc, i) }.toLong()
    repeat(readln().toInt()) {
        solve(l)
    }
}

fun solve(l: Long) {
    readln()
    val a = readln().split(' ').map(String::toInt)
    val b = a.map { l.div(it) }
    if (l > b.sum()) {
        println(b.joinToString(" "))
    } else {
        println(-1)
    }
}

fun lcm(m: Int, n: Int): Int {
    return m.div(gcd(m, n)).times(n)
}

fun gcd(m: Int, n: Int): Int {
    return if (n == 0) m else gcd(n, m.mod(n))
}

solution 4

const val L = 232792560L // [2, 20] 区间所有数字的最小公倍数
fun main() {
    repeat(readln().toInt()) {
        solve()
    }
}

fun solve() {
    readln()
    val a = readln().split(' ').map(String::toInt)
    val b = a.map { L.div(it) }
    if (L > b.sum()) {
        println(b.joinToString(" "))
    } else {
        println(-1)
    }
}