2025.1.22——1300

2025.1.22——1300


A 1300

Your friends have an array of n elements, calculated its array of prefix sums and passed it to you, accidentally losing one element during the transfer. Your task is to find out if the given array can matches permutation.

A permutation of n elements is an array of n numbers from 1 to n such that each number occurs exactly one times in it.

The array of prefix sums of the array a — is such an array b that bi=j=1iaj,1in.

For example, the original permutation was [1,5,2,4,3]. Its array of prefix sums — [1,6,8,12,15]. Having lost one element, you can get, for example, arrays [6,8,12,15] or [1,6,8,15].

It can also be shown that the array [1,2,100] does not correspond to any permutation.
Input

The first line contains a positive number t (1t104) — the number of test cases. The description of the test cases follows.

The first line of the description of each test case contains a positive number n (2n2105) — the size of the initial array.

The second line of the description of each test case contains n1 positive number ai (1ai1018), ai1<ai — elements of the array of prefix sums.

It is guaranteed that the sum of n over all test cases does not exceed 2105.


B 1300

You are given an integer x. Your task is to reduce x to 1.

To do that, you can do the following operation:

  • select a divisor d of x, then change x to xd, i.e. reduce x by d. (We say that d is a divisor of x if d is an positive integer and there exists an integer q such that x=dq.)

There is an additional constraint: you cannot select the same value of d more than twice.

For example, for x=5, the following scheme is invalid because 1 is selected more than twice: 514131211. The following scheme is however a valid one: 5142211.

Output any scheme which reduces x to 1 with at most 1000 operations. It can be proved that such a scheme always exists.
Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t1000). The description of the test cases follows.

The only line of each test case contains a single integer x (2x109).


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

  • 差分前缀/结论+二进制/因子/lowbit/数学


A

  1. 差分一下,分析下两种情况即可。

B

  1. 发现策略:lowbit(x)x 的一个因子。

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

A

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
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
#define bugv(VEC, ST)                         \
    {                                         \
        for (int I = ST; I < VEC.size(); I++) \
            cout << VEC[I] << ' ';            \
        el;                                   \
    }

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;
    cin >> n;
    int las = 0;
    map<int, int> cnt;
    for (int i = 1; i < n; i++)
    {
        int x;
        cin >> x;
        cnt[x - las]++;
        las = x;
    }
    int ned = 0;
    int f = 0;
    for (auto [v, ct] : cnt)
        if (ct > 1 || v > n)
            f = v;

    int nohas = 0;
    for (int i = 1; i <= n; i++)
        if (!cnt[i])
            nohas++, ned += i;
    cout << ((nohas == 1 || (nohas == 2 && f == ned)) ? "YES" : "NO");
    el;
}

B

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
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
#define bugv(VEC, ST)                         \
    {                                         \
        for (int I = ST; I < VEC.size(); I++) \
            cout << VEC[I] << ' ';            \
        el;                                   \
    }

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;
    cin >> n;
    auto lowbit = [](int x)
    {
        return x & -x;
    };
    vector<int> res{n};
    for (;;)
    {
        int bit = lowbit(n);
        if (bit == n)
            break;
        n -= bit;
        res.push_back(n);
    }
    while (n > 1)
    {
        int bit = lowbit(n) >> 1;
        n -= bit;
        res.push_back(n);
    }
    cout << res.size();
    el;
    bugv(res, 0);
}
// void _()
// {
//     int x;
//     cin >> x;
//     auto get = [](int n)
//     {
//         vector<int> t{1};
//         for (int i = 2; i <= n / i; i++)
//             if (n % i == 0)
//             {
//                 t.push_back(i);
//                 if (i - n % i)
//                     t.push_back(n % i);
//             }
//         return t;
//     };
//     map<int, int> cnt;
//     set<int> res{x, 1};
//     bool has = 0;
//     function<void(int)> dfs = [&](int n)
//     {
//         if (n == 1)
//         {
//             if (!has)
//             {
//                 has = 1;
//                 cout << res.size();
//                 el;
//                 for (auto it = res.rbegin(); it != res.rend(); it++)
//                     cout << *it << ' ';
//                 el;
//             }
//             return;
//         }
//         // bug(n);
//         // for (auto it = res.rbegin(); it != res.rend(); it++)
//         //     cout << *it << ' ';
//         // el;
//         auto t = get(n);
//         sort(t.rbegin(), t.rend());
//         // bug(t.size());
//         for (auto v : t)
//             if (cnt[v] < 2 && n - v > 0)
//             {
//                 cnt[v]++;
//                 res.insert(n - v);
//                 dfs(n - v);
//                 cnt[v]--;
//                 res.erase(n - v);
//             }
//     };
//     dfs(x);
// }

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