#P8931. [入门赛 #10] Hack Problem P
[入门赛 #10] Hack Problem P
Background
This is a hack problem. In this type of problem, you will be given several tasks and several pieces of code that are supposed to solve the corresponding tasks, but the given code cannot produce correct output for some inputs. Cases where the output is incorrect include:
- Producing a wrong result.
- Timing out.
- Triggering some runtime undefined behavior. Currently, the only undefined behavior that can be detected by the system is array out-of-bounds access.
For each task, you need to submit an input that satisfies the requirements so that the given code cannot produce correct output. You may directly use the “Submit Answer” feature, or submit a data generator written in any language.
Hint: If you use the “Submit Answer” feature, remember to change the language back to the one you use when submitting other problems.
Problem Description
The following are the statements for three tasks:
Task 1
Given two positive integers , it is guaranteed that their least common multiple () is not greater than . Find their least common multiple.
Task 2
Multiple test cases. Given an array of length , guaranteed to be strictly increasing. There are queries; each query gives an integer . For each query, find how many numbers in the array are less than or equal to .
Task 3
Multiple test cases. Given an array of length . There are queries; each query gives an integer . For each query, find how many numbers in the array are less than or equal to .
Note: Besides whether the array is guaranteed to be increasing, Task 2 and Task 3 also differ in Constraints. See below.
Input Format
Task 1
The input has only one line with two integers, and .
Task 2
The first line contains an integer , the number of test cases.
Then the input for each test case follows.
For each test case, the first line contains two integers, the array length and the number of queries .
The second line contains integers, .
The third line contains integers, the values for the queries.
Task 3
The first line contains an integer , the number of test cases.
Then the input for each test case follows.
For each test case, the first line contains two integers, the array length and the number of queries .
The second line contains integers, .
The third line contains integers, the values for the queries.
Output Format
Task 1
Output one line with one integer, the least common multiple of the two numbers.
Task 2
For each test case, output one line with integers, the answers to the queries in order.
Task 3
For each test case, output one line with integers, the answers to the queries in order.
3 4
12
2
3 3
5 7 9
7 7 7
3 3
1 2 3
2 2 2
2 2 2
2 2 2
2
3 3
4 4 5
4 5 6
3 3
1 1 3
1 2 3
2 3 3
2 2 3
Hint
Notes on samples and the actual input
The three samples correspond to the sample input/output of the three tasks.
If you directly use “Submit Answer”, please name the three input files 1.in, 2.in, 3.in respectively, and submit them as a zip file.
If you submit a data generator, your generator may read an integer from standard input, where , indicating the task id of the current test point, and then output the corresponding input data.
Obviously, your program should not read anything mentioned in the “Input Format” (instead, it should construct them), and it should not output anything mentioned in the “Sample Output” (instead, it should only output the input data you constructed). You should not use the samples to test your program; they are only descriptions of the samples for the three tasks.
Constraints requirements
The data you provide must satisfy the following requirements:
- It must fully follow the “Input Format”. There must be no extra input, but trailing spaces at line ends and a final newline are allowed.
- All numbers in the input must be integers.
- For Task 1, , and you must guarantee that as well.
- For Task 2, , , .
- For Task 3, , .
Target code
You need to hack the following code:
Task 1
#include <iostream>
#include <algorithm>
int main() {
int x, y;
std::cin >> x >> y;
int ans = x * y / std::__gcd(x, y);
std::cout << ans << std::endl;
}
Task 2
#include <iostream>
#include <algorithm>
const int maxn = 1000003;
int T;
int n, q;
int a[maxn], b[maxn];
int find(int x) {
int l = 1, r = n;
int ans = 0;
while (l < r) {
int mid = (l + r) >> 1;
if (a[mid] <= x) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
for (std::cin >> T; T; --T) {
std::cin >> n >> q;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
}
for (int x; q; --q) {
std::cin >> x;
std::cout << find(x) << " ";
}
std::cout << std::endl;
}
}
Task 3
#include <iostream>
const int maxn = 1000003;
int T;
int n, q;
int a[maxn], b[maxn];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
for (std::cin >> T; T; --T) {
std::cin >> n >> q;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
b[a[i]]++;
}
for (int i = 1; i < maxn; ++i) b[i] += b[i - 1];
for (int x; q; --q) {
std::cin >> x;
std::cout << b[x] << " \n"[q == 1];
}
}
}
The compilation options for the target code are -std=c++14 -fno-asm -O2. The compiler is the g++ provided by Luogu. You can choose the C++14 language in the “Online IDE” to get exactly the same compilation environment.
Scoring rules
This problem has three test points, corresponding to the three tasks. If you successfully hack a task, the corresponding test point returns accepted.
Input validation
Your data must fully satisfy the “Constraints requirements”; otherwise you will get an Unaccepted result.
Time limit judgment
After the program executes a certain number of instructions, we will check its running time once. We guarantee that the number of instructions between two checks is independent of the input size, meaning that a check happens every instructions. Also, the number of instructions between two checks is not too large, generally no more than C++ statements.
If the program’s running time exceeds , it will be judged as TLE, and the result will be accepted.
Wrong answer judgment
If the program finishes within the time limit and produces an output, we will compare it with the fully correct output. If they differ, the hack is considered successful, and the result will be accepted.
Undefined behavior judgment
Before each explicit access to an array element, we will check whether the index goes out of bounds. If it does, the hack is considered successful, and the result will be accepted.
That is, if you want to hack via undefined behavior, you can only hack explicit array element accesses.
Sample program
This is a sample program to help you understand what you need to output, but it cannot produce correct hack data. Submitting this program directly will not score.
#include <iostream>
using namespace std;
int main() {
int taskId;
cin >> taskId;
if (taskId == 1) {
cout << "" <<endl;
} else if (taskId == 2) {
cout << "" << endl;
} else if (taskId == 3) {
cout << "" << endl;
} else { // This else branch will not be executed.
cout << "QiHai Nanami" << endl;
}
}
If you use “Submit Answer”, be sure that after opening the zip file you can directly see, and only see, three .in files. That is, the file structure must be:
ans.zip
|---1.in
|---2.in
|---3.in
The three files should not be wrapped inside an extra folder. That is, the file structure must not be:
ans.zip
|---ans(folder)
|---1.in
|---2.in
|---3.in
Notes on judging information
If the hack succeeds, the corresponding test point shows accepted. If other information is returned, it means your program itself is wrong.
For example, if TLE is returned, it does not mean you successfully hacked the target program into timing out; instead, it means your data generator itself timed out, and the test point scores .
In particular, an UKE result may mean the data is invalid (extra content, missing content, or failing the constraints).
Translated by ChatGPT 5