写一个c++程序解决下面的问题
B. Alice's Adventures in Permuting
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Alice mixed up the words transmutation and permutation! She has an array a specified via three integers n, b, c: the array a has length and is given via for . For example, if , , and , then .
Assistant
n
ai=b⋅(i−1)+c
1≤i≤n
n=3
b=2
c=1
a=[2⋅0+1,2⋅1+1,2⋅2+1]=[1,3,5]
Now, Alice really enjoys permutations of [0, \ldots, n-1]$$^{\text{∗}} and would like to transform a into a permutation. In one operation, Alice replaces the maximum element of a with the \operatorname{MEX}$$^{\text{†}} of a. If there are multiple maximum elements in a, Alice chooses the leftmost one to replace.
Can you help Alice figure out how many operations she has to do for a to become a permutation for the first time? If it is impossible, you should report it.
∗A permutation of length n is an array consisting of n distinct integers from 0 to n−1 in arbitrary order. Please note, this is slightly different from the usual definition of a permutation. For example, [1,2,0,4,3] is a permutation, but [0,1,1] is not a permutation (1 appears twice in the array), and [0,2,3] is also not a permutation (n=3 but there is 3 in the array).
†The MEX of an array is the smallest non-negative integer that does not belong to the array. For example, the MEX of [0,3,1,3] is 2 and the MEX of [5] is 0.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105). The description of the test cases follows.
The only line of each test case contains three integers n, b, c (1≤n≤1018; 0≤b, c≤1018) — the parameters of the array.
Output
For each test case, if the array can never become a permutation, output −1. Otherwise, output the minimum number of operations for the array to become a permutation.
In the first test case, the array is already [0,1,…,9], so no operations are required.
In the third test case, the starting array is [1,3,5,…,199]. After the first operation, the 199 gets transformed into a 0. In the second operation, the 197 gets transformed into a 2. If we continue this, it will take exactly 50 operations to get the array [0,1,2,3,…,99].
In the fourth test case, two operations are needed: [1,1,1]→[0,1,1]→[0,2,1].
In the fifth test case, the process is [0,0,0]→[1,0,0]→[2,0,0]→[1,0,0]→[2,0,0]. This process repeats forever, so the array is never a permutation and the answer is −1.
To solve this problem, we'll consider the special conditions of the array and devise an efficient algorithm to find the minimum number of operations required to transform the array into a permutation or determine if it's impossible.
Here's the plan:
Compute MEX(a), the smallest non-negative integer not in the array.
Compute max(a), the maximum element in the array.
Compute the number of elements in the array that are within the range [0, n-1], denoted as num_present.
Determine the number of missing elements, num_missing = n - num_present.
Check the conditions:
If num_missing > 0 and MEX(a) > max(a), then it's impossible to form the permutation (output -1).
Otherwise, the minimum number of operations required is num_missing.
Edge Cases:
When b is 0, all elements in the array are equal to c. Compute MEX(a) and max(a) accordingly.
When b is not 0, the array forms an arithmetic progression. Compute MEX(a) based on the residue modulo b.
Now, we implement this logic in code, carefully handling large numbers and avoiding overflows. We use integer divisions and modular arithmetic to calculate MEX(a) and num_present efficiently without iterating over large ranges.
Here's the complete C++ program:
cpp
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
// Function to compute floor division
ll floor_div(ll a, ll b) {
if (b < 0) {
a = -a;
b = -b;
}
if (a >= 0) return a / b;
else return -((-a + b - 1) / b);
}
// Function to compute ceiling division
ll ceil_div(ll a, ll b) {
if (b < 0) {
a = -a;
b = -b;
}
if (a >= 0) return (a + b - 1) / b;
else return -(-a / b);
}
int main() {
int t;
cin >> t;
while (t--) {
ll n, b, c;
cin >> n >> b >> c;
ll MEX_a;
ll max_a;
ll num_present = 0;
if (b == 0) {
// All elements equal to c
max_a = c;
if ((c >= 0) && (c <= n - 1))
num_present = 1;
else
num_present = 0;
if (c != 0)
MEX_a = 0;
else
MEX_a = 1;
} else {
// Elements are c + b*k for k = 0 to n -1
max_a = c + b * (n - 1);
ll r = (c % b + b) % b;
if ((0 % b + b) % b != r)
MEX_a = 0;
else
MEX_a = b;
// Compute k_start and k_end for k such that c + b * k ∈ [0, n -1]
ll k_start = ceil_div(-c, b);
ll k_end = floor_div((n - 1 - c), b);
k_start = max(k_start, 0LL);
k_end = min(k_end, n - 1);
if (k_start <= k_end)
num_present = k_end - k_start + 1;
else
num_present = 0;
}
ll num_missing = n - num_present;
if (num_missing > 0 && MEX_a > max_a) {
cout << -1 << endl;
} else {
cout << num_missing << endl;
}
}
return 0;
}
Key Takeaways:
Dealing with large numbers requires careful implementation to prevent overflows.
Understanding the properties of arithmetic progressions and modular arithmetic helps in efficiently solving problems that might seem to require iteration over large ranges.
Always handle special cases, such as when b is 0, separately to avoid division errors.