2025.1.16——1200

2025.1.16——1200


Q1. 1200

You are given 3 integers — n, x, y. Let's call the score of a permutation p1,,pn the following value:

(p1x+p2x++pnxx)(p1y+p2y++pnyy)

In other words, the score of a permutation is the sum of pi for all indices i divisible by x, minus the sum of pi 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,6,1,7,5,4,3] and is equal to (6+7+4)(1+4)=175=12.

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 (1n109, 1x,yn).


Q2. 1200

You are given two arrays of integers — a1,,an of length n, and b1,,bm of length m. You can choose any element bj from array b (1jm), and for all 1in perform ai=ai|bj. You can perform any number of such operations.

After all the operations, the value of x=a1a2an 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 is the bitwise XOR operation.
Input

The first line of each test case contains two integers n and m (1n,m2105) — the sizes of arrays a and b.

The second line of each test case contains n integers a1,a2,,an (0ai109) — the array a.

The third line of each test case contains m integers b1,b2,,bm (0bi109) — the array b.


------------------------思考------------------------

  • 数学/结论+位运算


A1

  1. 发现结论:除了共有的位置,计算个数分别分配最大值与最小值。
  2. 共有的位置是最小公倍数占的位置。

A2

  1. 位运算:保存 b 数组的每一位,相当于将原异或和值的某一位进行 0/1 互换。
  2. 奇数时可使原值变大,偶数时可使原值减小。

------------------------代码------------------------

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;
}

posted @   Jkke  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示