2025.1.16——1200
Q1. 1200
You are given \(3\) integers — \(n\), \(x\), \(y\). Let's call the score of a permutation\(^\dagger\) \(p_1, \ldots, p_n\) the following value:
\[(p_{1 \cdot x} + p_{2 \cdot x} + \ldots + p_{\lfloor \frac{n}{x} \rfloor \cdot x}) - (p_{1 \cdot y} + p_{2 \cdot y} + \ldots + p_{\lfloor \frac{n}{y} \rfloor \cdot y}) \]In other words, the score of a permutation is the sum of \(p_i\) for all indices \(i\) divisible by \(x\), minus the sum of \(p_i\) for all indices \(i\) divisible by \(y\).
You need to find the maximum possible score among all permutations of length \(n\).
For example, if \(n = 7\), \(x = 2\), \(y = 3\), the maximum score is achieved by the permutation \([2,\color{red}{\underline{\color{black}{6}}},\color{blue}{\underline{\color{black}{1}}},\color{red}{\underline{\color{black}{7}}},5,\color{blue}{\underline{\color{red}{\underline{\color{black}{4}}}}},3]\) and is equal to \((6 + 7 + 4) - (1 + 4) = 17 - 5 = 12\).
\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in any order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (the number \(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\), but the array contains \(4\)).
Input
The only line of each test case description contains \(3\) integers \(n\), \(x\), \(y\) (\(1 \le n \le 10^9\), \(1 \le x, y \le n\)).
Q2. 1200
You are given two arrays of integers — \(a_1, \ldots, a_n\) of length \(n\), and \(b_1, \ldots, b_m\) of length \(m\). You can choose any element \(b_j\) from array \(b\) (\(1 \leq j \leq m\)), and for all \(1 \leq i \leq n\) perform \(a_i = a_i | b_j\). You can perform any number of such operations.
After all the operations, the value of \(x = a_1 \oplus a_2 \oplus \ldots \oplus a_n\) will be calculated. Find the minimum and maximum values of \(x\) that could be obtained after performing any set of operations.
Above, \(|\) is the bitwise OR operation, and \(\oplus\) is the bitwise XOR operation.
Input
The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 2 \cdot 10^5\)) — the sizes of arrays \(a\) and \(b\).
The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\)) — the array \(a\).
The third line of each test case contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(0 \leq b_i \leq 10^9\)) — the array \(b\).
------------------------思考------------------------
-
数学/结论+位运算
A1
- 发现结论:除了共有的位置,计算个数分别分配最大值与最小值。
- 共有的位置是最小公倍数占的位置。
A2
- 位运算:保存 \(b\) 数组的每一位,相当于将原异或和值的某一位进行 \(0/1\) 互换。
- 奇数时可使原值变大,偶数时可使原值减小。
------------------------代码------------------------
A1
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n, x, y;
cin >> n >> x >> y;
int hasx = n / x, hasy = n / y;
int hasxy = n / (x * y / __gcd(x, y));
hasx -= hasxy;
hasy -= hasxy;
// bug3(hasx, hasy, hasxy);
auto get = [](int st, int ed)
{
int cnt = ed - st + 1;
return (st + ed) * cnt / 2;
};
cout << get(n - hasx + 1, n) - get(1, hasy) << endl;
}
A2
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n, m;
cin >> n >> m;
int _xor = 0;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
_xor ^= x;
}
map<int, bool> has_bit;
for (int i = 0; i < m; i++)
{
int x;
cin >> x;
for (int j = 0; x >> j; j++)
if (x >> j & 1) // wa
has_bit[j] = 1;
}
int mx = _xor, mn = _xor;
if (n & 1)
{
for (auto [bit, has] : has_bit)
{
if (mx >> bit & 1)
;
else
mx += 1ll << bit; //, bug(bit);
// bug(mx);
}
}
else
{
for (auto [bit, has] : has_bit)
if (mn >> bit & 1)
mn -= 1ll << bit;
}
cout << mn << ' ' << mx << endl;
}
标签:integers,2025.1,16,int,cdot,1200,leq,ldots,color From: https://www.cnblogs.com/jkkk/p/18677538