Codeforces Round 988 (Div. 3)

Codeforces Round 988 (Div. 3)

C. Superultra’s Favorite Permutation

time limit per test

2 seconds

memory limit per test

256 megabytes

Superultra, a little red panda, desperately wants primogems. In his dreams, a voice tells him that he must solve the following task to obtain a lifetime supply of primogems. Help Superultra!

Construct a permutation∗∗ pp of length nn such that pi+pi+1pi+pi+1 is composite†† over all 1≤i≤n−11≤i≤n−1. If it’s not possible, output −1−1.

∗∗A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

††An integer xx is composite if it has at least one other divisor besides 11 and xx. For example, 44 is composite because 22 is a divisor.

Input

The first line contains tt (1≤t≤1041≤t≤104) — the number of test cases.

Each test case contains an integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, if it’s not possible to construct pp, output −1−1 on a new line. Otherwise, output nn integers p1,p2,…,pnp1,p2,…,pn on a new line.

Example

Input

Copy

238

Output

Copy

-1
1 8 7 3 6 2 4 5

Note

In the first example, it can be shown that all permutation of size 33 contain two adjacent elements whose sum is prime. For example, in the permutation [2,3,1][2,3,1] the sum 2+3=52+3=5 is prime.

In the second example, we can verify that the sample output is correct because 1+81+8, 8+78+7, 7+37+3, 3+63+6, 6+26+2, 2+42+4, and 4+54+5 are all composite. There may be other constructions that are correct.

题目描述

构造一个长度为 nnn 的排列 ppp,使得每两个相邻元素之和 p[i]+p[i+1]p[i] + p[i+1]p[i]+p[i+1] 都是一个合数。如果无法构造,输出 -1。

解题思路
  1. 观察性质

    • p[i]+p[i+1]p[i] + p[i+1]p[i]+p[i+1] 为合数,表示它不能是质数。小于等于 nnn 的整数排列具有一定组合特点。
  2. 构造方法

    • 如果 n<5n < 5n<5,无法避免质数和,输出 -1。

    • 对于

      n≥5n \geq 5n≥5

      ,按如下顺序构造:

      • 奇数从小到大(排除 5);
      • 插入 5 和 4;
      • 偶数从小到大(排除 4)。

C++代码实现:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define endl '\n'
#define int long long
#define Max(a, b) (((a) > (b)) ? (a) : (b))
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define BoBoowen ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;

signed main()
{
    BoBoowen;

    int T;
    cin >> T;

    while (T--)
    {
        int n;
        cin >> n;
        if (n < 5)
        {
            cout << "-1" << endl;
            continue;
        }
        else
        {
            for (int i = 1; i <= n; i += 2)
            {
                if (i == 5)
                {
                    continue;
                }
                cout << i << ' ';
            }
            cout << 5 << ' ';
            cout << 4 << ' ';
            for (int i = 2; i <= n; i += 2)
            {
                if (i == 4)
                {
                    continue;
                }
                cout << i << ' ';
            }
            cout << endl;
        }
    }
}

D. Sharky Surfing

time limit per test

3 seconds

memory limit per test

256 megabytes

Mualani loves surfing on her sharky surfboard!

Mualani’s surf path can be modeled by a number line. She starts at position 11, and the path ends at position LL. When she is at position xx with a jump power of kk, she can jump to any integer position in the interval [x,x+k][x,x+k]. Initially, her jump power is 11.

However, her surf path isn’t completely smooth. There are nn hurdles on her path. Each hurdle is represented by an interval [l,r][l,r], meaning she cannot jump to any position in the interval [l,r][l,r].

There are also mm power-ups at certain positions on the path. Power-up ii is located at position xixi and has a value of vivi. When Mualani is at position xixi, she has the option to collect the power-up to increase her jump power by vivi. There may be multiple power-ups at the same position. When she is at a position with some power-ups, she may choose to take or ignore each individual power-up. No power-up is in the interval of any hurdle.

What is the minimum number of power-ups she must collect to reach position LL to finish the path? If it is not possible to finish the surf path, output −1−1.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains three integers nn, mm, and LL (1≤n,m≤2⋅105,3≤L≤1091≤n,m≤2⋅105,3≤L≤109) — the number of hurdles, the number of power-ups, and the position of the end.

The following nn lines contain two integers lili and riri (2≤li≤ri≤L−12≤li≤ri≤L−1) — the bounds of the interval for the ii’th hurdle. It is guaranteed that ri+1

The following mm lines contain two integers xixi and vivi (1≤xi,vi≤L1≤xi,vi≤L) — the position and the value for the ii’th power-up. There may be multiple power-ups with the same xx. It is guaranteed that xi≤xi+1xi≤xi+1 for all 1≤i

It is guaranteed the sum of nn and the sum of mm over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output the minimum number of power-ups she must collect to reach position LL. If it is not possible, output −1−1.

Example

Input

Copy

42 5 507 1430 402 23 13 518 222 324 3 504 615 1820 2634 381 28 210 21 4 1710 141 61 21 216 91 2 105 92 32 2

Output

Copy

4
-1
1
2

Note

In the first test case, she can collect power-ups 11, 22, 33, and 55 to clear all hurdles.

In the second test case, she cannot jump over the first hurdle.

In the fourth test case, by collecting both power-ups, she can jump over the hurdle.

题目描述

Mualani 要从起点跳到终点 LLL,途中有若干障碍和能量增强道具。每次跳跃范围由当前能量决定。问她至少需要收集多少个道具才能跳到终点,如果无法跳到终点,输出 -1。

解题思路
  1. 贪心优先收集道具

    • 使用优先队列保存当前可收集道具的能量值,按照需求补充跳跃距离。
    • 逐步遍历障碍,确保能量足以越过当前障碍。
  2. 优先队列操作

    • 在每个障碍前,收集尽可能多的道具,直到能量满足跳跃需求。
    • 若队列为空且能量不足,则输出 -1。

C++代码实现:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define endl '\n'
#define int long long
#define Max(a, b) (((a) > (b)) ? (a) : (b))
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define BoBoowen ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;

void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<pair<int, int>> a(n + 1);
    vector<pair<int, int>> b(m + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].first >> a[i].second;
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> b[i].first >> b[i].second;
    }

    priority_queue<int> q;
    int l = 1;
    int ans = 0;
    int sum = 0;
    for (int i = 1; i <= n; i++)
    {
        while (l <= m && b[l].first < a[i].first)
        {
            q.push(b[l].second);
            l++;
        }

        int len = a[i].second - a[i].first + 1;

        while (sum < len)
        {
            if (q.empty())
            {
                cout << "-1" << endl;
                return;
            }
            sum += q.top();
            q.pop();
            ans++;
        }
    }
    cout << ans << endl;
}

signed main()
{
    BoBoowen;

    int T;
    cin >> T;

    while (T--)
    {
        solve();
    }
}

E. Kachina’s Favorite Binary String

time limit per test

2 seconds

memory limit per test

256 megabytes

This is an interactive problem.

Kachina challenges you to guess her favorite binary string∗∗ ss of length nn. She defines f(l,r)f(l,r) as the number of subsequences†† of 0101 in slsl+1…srslsl+1…sr. Two subsequences are considered different if they are formed by deleting characters from different positions in the original string, even if the resulting subsequences consist of the same characters.

To determine ss, you can ask her some questions. In each question, you can choose two indices ll and rr (1≤l

Determine and output ss after asking Kachina no more than nn questions. However, it may be the case that ss is impossible to be determined. In this case, you would need to report IMPOSSIBLEIMPOSSIBLE instead.

Formally, ss is impossible to be determined if after asking nn questions, there are always multiple possible strings for ss, regardless of what questions are asked. Note that if you report IMPOSSIBLEIMPOSSIBLE when there exists a sequence of at most nn queries that will uniquely determine the binary string, you will get the Wrong Answer verdict.

∗∗A binary string only contains characters 00 and 11.

††A sequence aa is a subsequence of a sequence bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) elements. For example, subsequences of 10111011011101 are 00, 11, 1111111111, 01110111, but not 000000 nor 1110011100.

Input

The first line of input contains a single integer tt (1≤t≤1031≤t≤103) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤1042≤n≤104) — the length of ss.

It is guaranteed that the sum of nn over all test cases does not exceed 104104.

Interaction

To ask a question, output a line in the following format (do not include quotes)

  • “? l r? l r” (1≤l

The jury will return an integer f(l,r)f(l,r)

When you are ready to print the answer, output a single line in the following format

  • If ss is impossible to be determined, output “! IMPOSSIBLE! IMPOSSIBLE”
  • Otherwise, output “! s! s”

After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query.

The interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn’t depend on the queries asked by the participant.

If your program makes more than nn queries for one test case, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get Idleness limit exceeded verdict. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages.

Hacks

To make a hack, use the following format.

The first line should contain a single integer tt (1≤t≤1031≤t≤103) – the number of test cases.

The first line of each test case should contain an integer nn (2≤n≤1042≤n≤104) — the length of ss.

The following line should contain ss, a binary string of length nn.

The sum of nn over all test cases should not exceed 104104.

Example

Input

Copy

2
5

4

0

1

2

2

0

Output

Copy

? 1 5

? 2 4

? 4 5

? 3 5

! 01001

? 1 2

! IMPOSSIBLE

Note

In the first test case:

In the first query, you ask Kachina for the value of f(1,5)f(1,5), and she responds with 44 in the input stream.

In the second query, you ask Kachina for the value of f(2,4)f(2,4). Because there are no subsequences of 0101 in the string 100100, she responds with 00 in the input stream.

After asking 44 questions, you report 0100101001 as ss, and it is correct.

In the second test case:

In the first query, you ask Kachina for the value of f(1,2)f(1,2), and she responds with 00 in the input stream. Notice that this is the only distinct question you can ask.

However, notice that the strings 0000 and 1111 both have an answer of 00, and it is impossible to differentiate between the two. Therefore, we report IMPOSSIBLE.

Please note that this example only serves to demonstrate the interaction format. It is not guaranteed the queries provided are optimal or uniquely determine the answer. However, it can be shown there exists a sequence of at most 55 queries that does uniquely determine sample test case 11.

题目描述

通过询问字符串子区间中 “0101” 的出现次数,猜测一个二进制字符串。如果无法唯一确定该字符串,输出 IMPOSSIBLE

解题思路
  1. 问题分析

    • 每次询问提供的信息有限,但可以通过巧妙设计问题逐步缩小可能的字符串范围。
    • 若信息不足以唯一确定结果,则输出 IMPOSSIBLE
  2. 询问设计

    • 根据问题性质,逐步询问以确定每位可能的取值。
    • 若存在某些区间中 “0101” 的数量变化,可以推断具体值。
  3. 贪心推断

    • 使用累积答案的方式逐步填充未知位置的值。

C++代码实现:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define endl '\n'
#define int long long
#define Max(a, b) (((a) > (b)) ? (a) : (b))
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define BoBoowen ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;

int query(int l, int r)
{
    cout << "? " << l << ' ' << r << endl;
    cin >> l;
    return l;
}

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 2; i <= n; i++)
    {
        a[i] = query(1, i);
    }
    if (a[n] == 0)
    {
        cout << "! IMPOSSIBLE" << endl;
        return;
    }
    vector<int> ans(n + 1, -1);
    int flag = 0;
    for (int i = 2; i <= n; i++)
    {
        if (!flag && a[i])
        {
            for (int j = 1; j <= a[i]; j++)
            {
                ans[i - j] = 0;
            }
            flag = 1;
        }
        if (flag && (a[i] == a[i - 1]))
        {
            ans[i] = 0;
        }
    }
    cout << "! ";
    for (int i = 1; i <= n; i++)
    {
        if (ans[i] == -1)
        {
            ans[i] = 1;
        }
        cout << ans[i];
    }
    cout << endl;
}

signed main()
{
    //BoBoowen;

    int T;
    cin >> T;

    while (T--)
    {
        solve();
    }
}

F. Ardent Flames

time limit per test

4 seconds

memory limit per test

256 megabytes

You have obtained the new limited event character Xilonen. You decide to use her in combat.

There are nn enemies in a line. The ii’th enemy from the left has health hihi and is currently at position xixi. Xilonen has an attack damage of mm, and you are ready to defeat the enemies with her.

Xilonen has a powerful “ground stomp” attack. Before you perform any attacks, you select an integer pp and position Xilonen there (pp can be any integer position, including a position with an enemy currently). Afterwards, for each attack, she deals mm damage to an enemy at position pp (if there are any), m−1m−1 damage to enemies at positions p−1p−1 and p+1p+1, m−2m−2 damage to enemies at positions p−2p−2 and p+2p+2, and so on. Enemies that are at least a distance of mm away from Xilonen take no damage from attacks.

Formally, if there is an enemy at position xx, she will deal max(0,m−|p−x|)max(0,m−|p−x|) damage to that enemy each hit. Note that you may not choose a different pp for different attacks.

Over all possible pp, output the minimum number of attacks Xilonen must perform to defeat at least kk enemies. If it is impossible to find a pp such that eventually at least kk enemies will be defeated, output −1−1 instead. Note that an enemy is considered to be defeated if its health reaches 00 or below.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) – the number of test cases.

The first line of each test case contains three integers nn, mm, and kk (1≤k≤n≤1051≤k≤n≤105, 1≤m≤1091≤m≤109).

The following line contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109).

The last line of each testcase contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤1091≤xi≤109, xi

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case, output an integer on a new line, the minimum number of attacks that must be performed to defeat at least kk enemies. If it is impossible to find a pp such that eventually at least kk enemies will be defeated, output −1−1 instead.

Example

Input

Copy

65 5 37 7 7 7 71 2 3 4 59 5 92 4 6 8 10 8 6 4 21 2 3 4 5 6 7 8 92 10 21 11 202 10 169696969 4204204201 202 10 210 151 192 2 21000000000 11 3

Output

Copy

2
2
-1
6969697
15
1000000000

Note

In the first testcase, it is optimal to select p=2p=2. Each attack, the first enemy takes 5−|2−1|=45−|2−1|=4 damage, the second enemy takes 55 damage, the third enemy takes 44 damage, the fourth enemy takes 33 damage, and the fifth enemy takes 22 damage. After 22 attacks, the first three enemies will be defeated. It can be shown that it is impossible to defeat 33 enemies in less than 22 attacks, no matter which pp is selected.

In the second testcase, we must kill all 99 enemies. By selecting p=5p=5, all nine enemies will be defeated in 22 attacks.

In the third testcase, we must kill both enemies. However, it can be shown that no pp selected will damage both enemies at the same time, so the answer is −1−1.

In the fourth testcase, selecting p=1p=1 will enable us to defeat the first enemy in 69696976969697 attacks.

In the fifth testcase, selecting p=10p=10 will make each enemy take 11 damage per attack. Both enemies will be defeated in 1515 attacks.

题目描述

Xilonen 对敌人使用范围攻击,确定一个固定位置 ppp 使得至少能击败 kkk 个敌人,输出最少的攻击次数。如果无法击败,输出 -1。

解题思路
  1. 攻击范围分析

    • 敌人受到的伤害与位置和攻击距离 mmm 相关。
    • 对每个 ppp,需要统计其范围内能击败的敌人数。
  2. 二分答案

    • 假设需要的攻击次数为 numnumnum,逐步尝试 numnumnum 是否满足条件。
    • 使用差分数组快速统计某个攻击次数能击败的敌人数。
  3. 贪心优化

    • 尝试攻击次数从小到大,尽快找到满足条件的最小值。

C++代码实现:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define endl '\n'
#define int long long
#define Max(a, b) (((a) > (b)) ? (a) : (b))
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define BoBoowen ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;

int n, m, k;
int h[100005], x[100005];

int check(int num)
{
    map<int, int> cf;
    for (int i = 1; i <= n; i++)
    {
        int least = (h[i] + num - 1) / num;
        if (least > m)
        {
            continue;
        }
        int L = x[i] - (m - least);
        int R = x[i] + (m - least);
        cf[L]++;
        cf[R + 1]--;
    }
    int nw = 0;

    for (auto [x, y] : cf)
    {
        nw += y;
        if (nw >= k)
        {
            return 1;
        }
    }
    return 0;
}

signed main()
{
    // BoBoowen;

    int T;
    cin >> T;

    while (T--)
    {
        cin >> n >> m >> k;
        for (int i = 1; i <= n; i++)
        {
            cin >> h[i];
        }
        for (int i = 1; i <= n; i++)
        {
            cin >> x[i];
        }
        int l = 1, r = 1e9, ans = -1;
        while (l <= r)
        {
            int mid = l + r >> 1;
            if (check(mid))
            {
                ans = mid;
                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }
        }
        cout << ans << endl;
    }
}
    • G. Natlan Exploring

      time limit per test

      4 seconds

      memory limit per test

      256 megabytes

      You are exploring the stunning region of Natlan! This region consists of nn cities, and each city is rated with an attractiveness aiai. A directed edge exists from City ii to City jj if and only if i

      Starting from City 11, your task is to determine the total number of distinct paths you can take to reach City nn, modulo 998244353998244353. Two paths are different if and only if the set of cities visited is different.

      Input

      The first line contains an integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of cities.

      The second line contains nn integers a1,a2,…,ana1,a2,…,an (2≤ai≤1062≤ai≤106) — the attractiveness of each city.

      Output

      Output the total number of distinct paths you can take to reach City nn, modulo 998244353998244353.

      Examples

      Input

      Copy

      52 6 3 4 6
      

      Output

      Copy

      5
      

      Input

      Copy

      54 196 2662 2197 121
      

      Output

      Copy

      2
      

      Input

      Copy

      73 6 8 9 11 12 20
      

      Output

      Copy

      7
      

      Input

      Copy

      22 3
      

      Output

      Copy

      0
      

      Note

      In the first example, the five paths are the following:

      • City 1→1→ City 55
      • City 1→1→ City 2→2→ City 55
      • City 1→1→ City 2→2→ City 3→3→ City 55
      • City 1→1→ City 2→2→ City 4→4→ City 55
      • City 1→1→ City 4→4→ City 55

      In the second example, the two paths are the following:

      • City 1→1→ City 3→3→ City 55
      • City 1→1→ City 2→2→ City 3→3→ City 5
      题目描述

      从城市 1 开始,找到所有可能到达城市 nnn 的路径,路径需满足城市之间的吸引力满足 GCD 非 1 的条件。

      解题思路
      1. 分解质因数

        • 将每个城市的吸引力分解为质因数,用以构建连接关系。
      2. 路径计数

        • 从城市 1 开始,通过动态规划统计所有可能路径数量。
      3. 模运算优化

        • 由于结果可能很大,对每次路径计数取模 998244353998244353998244353。
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define endl '\n'
#define int long long
#define Max(a, b) (((a) > (b)) ? (a) : (b))
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define BoBoowen ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;

const int N = 1e3 + 100;
const int M = 1e6 + 10;
const int mod = 998244353;
int isprime[N];
int ton[M];
int pri[N];
int cnt;
vector e[M];

void isPrime()
{
    for (int i = 2; i <= 1000; i++)
    {
        if (!isprime[i])
        {
            pri[++cnt] = i;
        }
        for (int j = 1; j <= cnt; j++)
        {
            if (pri[j] * i > 1000)
            {
                break;
            }
            isprime[pri[j] * i] = 1;
            if (i % pri[j] == 0)
            {
                break;
            }
        }
    }
}
signed main()
{
    BoBoowen;
    isPrime();

    for (int i = 1; i <= M; i++)
    {
        int now = i;
        for (int j = 1; j <= cnt && now >= pri[j]; j++)
        {

            if (now % pri[j] == 0)
            {

                while (now % pri[j] == 0)
                {
                    now /= pri[j];
                }
                e[i].push_back(pri[j]);
            }
        }
        if (now != 1)
        {
            e[i].push_back(now);
        }
    }

    int n;
    cin >> n;
    vector a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    int fi = 1;
    int ans = 1;
    for (int i = 1; i <= n; i++)
    {
        int len = e[a[i]].size();

        for (int j = 1; j < (1 << len); j++)
        {
            int now = 1;

            for (int k = 0; k < len; k++)
            {
                if (j & (1 << k))
                {
                    now *= e[a[i]][k];
                }
            }
            fi = (fi + ton[now]) % mod;
        }
        for (int j = 1; j < (1 << len); j++)
        {
            int now = 1;

            int sum = __builtin_popcount(j);
            int flag = (sum & 1) ? 1 : -1;

            for (int k = 0; k < len; k++)
            {
                if (j & (1 << k))
                {
                    now *= e[a[i]][k];
                }
            }

            ton[now] = (fi * flag + ton[now]) % mod;
        }
        ans = fi;

        fi = 0;
    }
    ans = ((ans + mod) % mod + mod) % mod;

    cout << ans << endl;
}

你可能感兴趣的:(codeforces,笔记)