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。
观察性质
:
构造方法
:
如果 n<5n < 5n<5,无法避免质数和,输出 -1。
对于
n≥5n \geq 5n≥5
,按如下顺序构造:
#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;
}
}
}
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 Output Copy 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。 贪心优先收集道具 : 优先队列操作 : 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) 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 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: 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 Output Copy 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” 的出现次数,猜测一个二进制字符串。如果无法唯一确定该字符串,输出 问题分析 : 询问设计 : 贪心推断 : 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 Output Copy 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。 攻击范围分析 : 二分答案 : 贪心优化 : 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 Output Copy Input Copy Output Copy Input Copy Output Copy Input Copy Output Copy Note In the first example, the five paths are the following: In the second example, the two paths are the following: 从城市 1 开始,找到所有可能到达城市 nnn 的路径,路径需满足城市之间的吸引力满足 GCD 非 1 的条件。 分解质因数 : 路径计数 : 模运算优化 :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
4
-1
1
2
题目描述
解题思路
C++代码实现:
#include
#include
E. Kachina’s Favorite Binary String
2
5
4
0
1
2
2
0
? 1 5
? 2 4
? 4 5
? 3 5
! 01001
? 1 2
! IMPOSSIBLE
题目描述
IMPOSSIBLE
。解题思路
IMPOSSIBLE
。
C++代码实现:
#include
#include
F. Ardent Flames
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
2
2
-1
6969697
15
1000000000
题目描述
解题思路
C++代码实现:
#include
#include
52 6 3 4 6
5
54 196 2662 2197 121
2
73 6 8 9 11 12 20
7
22 3
0
题目描述
解题思路
#include