CodeForces Round 941 div2 个人题解(A~D)

嵌入式系统开发笔记112:通过有人云测试MQTT 项目中使用到“有人”公司的WH-LTE-7S0模块,本文讲解通过有人云进行MQTT测试的方法。 阅读详情

CodeForces Round 941(div2)题解

A. Card Exchange

题目翻译

你有一手 n 张牌,每张牌上都写着一个数字,还有一个固定整数 k。你可以多次进行下面的运算:

  • 从手中的 k 张牌中选择任意一张数字相同的牌。
  • 将这些牌换成 k-1 张牌,每张牌上可以有你选择的任何数字(包括你刚刚换的牌上写的数字)。

在这个过程结束时,你手中最少有多少张牌?

输入

输入的第一行包含一个整数 t( 1 ≤ t ≤ 500 1 ≤ t ≤ 500 1≤t≤500) - 测试用例的数量。

每个测试用例的第一行包含两个整数 n 和 k( 1 ≤ n ≤ 100 1 ≤ n ≤ 100 1≤n≤100, 2 ≤ k ≤ 100 2 ≤ k ≤ 100 2≤k≤100)–分别是您拥有的纸牌数量,以及您在每次操作中交换的纸牌数量。

每个测试用例的下一行包含 n 个整数 c1,c2,…,cn( 1 ≤ c i ≤ 100 1 ≤ ci ≤ 100 1≤ci≤100)- 写在卡片上的数字。

输出

对于每个测试用例,输出一个整数,即经过任意次数的操作后,手中所剩纸牌的最少数量。

示例

输入

7
5 3
4 1 1 4 4
1 10
7
7 2
4 2 1 100 5 2 3
10 4
1 1 1 1 1 1 1 1 1 1
5 2
3 8 1 48 7
6 2
10 20 30 10 20 40
6 3
10 20 30 10 20 40

输出

2
1
1
3
5
1
6

解题思路

由于 k k k张牌可以换成 k − 1 k-1 k−1张任意牌,所以只有有一种牌的数量大于等于 k k k,我们就可以把所有牌换成任意牌,所以最后会剩下 k − 1 k-1 k−1张。

题解

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <list>
#include <bitset>
#include <cmath>
#include <numeric>

#define endl '\n'

#define ft first
#define sd second

#define yes cout << "yes\n"
#define no cout << "no\n"

#define Yes cout << "Yes\n"
#define No cout << "No\n"

#define YES cout << "YES\n"
#define NO cout << "NO\n"

#define pb push_back
#define eb emplace_back

#define all(x) x.begin(), x.end()
#define unq_all(x) x.erase(unique(all(x)), x.end())
#define sort_all(x) sort(all(x))
#define reverse_all(x) reverse(all(x))

#define INF 0x7fffffff
#define INFLL 0x7fffffffffffffffLL

#define RED cout << "\033[91m"
#define GREEN cout << "\033[92m"
#define YELLOW cout << "\033[93m"
#define BLUE cout << "\033[94m"
#define MAGENTA cout << "\033[95m"
#define CYAN cout << "\033[96m"
#define RESET cout << "\033[0m"

// 红色
#define DEBUG1(x)                     \
    RED;                              \
    cout << #x << " : " << x << endl; \
    RESET;

// 绿色
#define DEBUG2(x)                     \
    GREEN;                            \
    cout << #x << " : " << x << endl; \
    RESET;

// 蓝色
#define DEBUG3(x)                     \
    BLUE;                             \
    cout << #x << " : " << x << endl; \
    RESET;

// 品红
#define DEBUG4(x)                     \
    MAGENTA;                          \
    cout << #x << " : " << x << endl; \
    RESET;

// 青色
#define DEBUG5(x)                     \
    CYAN;                             \
    cout << #x << " : " << x << endl; \
    RESET;

// 黄色
#define DEBUG6(x)                     \
    YELLOW;                           \
    cout << #x << " : " << x << endl; \
    RESET;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<string, ll> psl;

typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pss> vpss;

typedef vector<vi> vvi;
typedef vector<vl> vvl;

typedef queue<int> qi;
typedef queue<ll> ql;
typedef queue<pii> qpii;
typedef queue<pll> qpll;
typedef queue<psi> qpsi;
typedef queue<psl> qpsl;

typedef priority_queue<int> pqi;
typedef priority_queue<ll> pql;
typedef priority_queue<string> pqs;
typedef priority_queue<pii> pqpii;
typedef priority_queue<psi> pqpsi;
typedef priority_queue<pll> pqpl;
typedef priority_queue<psi> pqpsl;

typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<ll, ll> mll;
typedef map<ll, bool> mlb;
typedef map<char, int> mci;
typedef map<char, ll> mcl;
typedef map<char, bool> mcb;
typedef map<string, int> msi;
typedef map<string, ll> msl;
typedef map<int, bool> mib;

typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> uml;
typedef unordered_map<char, int> umci;
typedef unordered_map<char, ll> umcl;
typedef unordered_map<string, int> umsi;
typedef unordered_map<string, ll> umsl;

template <typename T>
inline void read(T &x)
{
    T f = 1;
    x = 0;
    char ch = getchar();
    while (0 == isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (0 != isdigit(ch))
        x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
    x *= f;
}

template <typename T>
inline void write(T x)
{
    if (x < 0)
    {
        x = ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

/*#####################################BEGIN#####################################*/

void solve()
{
    int n, k;
    cin >> n >> k;
    vi a(n);
    mii mp;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
    }
    bool flag = false;
    for (auto x : mp)
    {
        if (x.sd >= k)
        {
            flag = true;
            break;
        }
    }
    if (flag)
        cout << k - 1 << endl;
    else
        cout << n << endl;
}

int main()
{
    ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    int _ = 1;
    std::cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

/*######################################END######################################*/
// 链接:

B. Rectangle Filling

题目翻译

有一个由白色和黑色方格组成的 n × m n \times m n×m 网格。在一次操作中,您可以选择任意两个相同颜色的方格,并将它们之间的子矩形中的所有方格都染成相同的颜色。

具体来说,如果您选择了位置 ( x 1 , y 1 ) (x_1,y_1) (x1​,y1​) 和 ( x 2 , y 2 ) (x_2,y_2) (x2​,y2​),这两个位置当前的颜色都是 c c c,那么就可以将 min ⁡ ( x 1 , x 2 ) ≤ x ≤ max ⁡ ( x 1 , x 2 ) \min(x_1,x_2) \leq x \leq \max(x_1,x_2) min(x1​,x2​)≤x≤max(x1​,x2​) 和 min ⁡ ( y 1 , y 2 ) ≤ y ≤ max ⁡ ( y 1 , y 2 ) \min(y_1,y_2) \leq y \leq \max(y_1,y_2) min(y1​,y2​)≤y≤max(y1​,y2​) 所在的所有 ( x , y ) (x,y) (x,y) 的颜色设置为 c c c。

在进行任意次数(可能为零)的运算后,网格中所有方格的颜色是否可能相同?

输入

输入的第一行包含一个整数 t t t( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104) - 测试用例的数量。

每个测试用例的第一行包含两个整数 n n n 和 m m m( 1 ≤ n , m ≤ 500 1 \leq n,m \leq 500 1≤n,m≤500) - 分别是网格的行数和列数。

接下来的每行 n n n 都包含 m m m 个字符 ‘W’ 和 ‘B’ - 网格方格的初始颜色。

输出

对于每个测试用例,如果可以使网格中所有方格的颜色相同,则打印 “YES”,否则打印 “NO”。

您可以用任何大小写(大写或小写)输出答案。例如,字符串 “yEs”、“yes”、“Yes” 和 “YES” 将被识别为肯定回答。

示例

输入

8
2 1
W
B
6 6
WWWWBW
WBWWWW
BBBWWW
BWWWBB
WWBWBB
BBBWBW
1 1
W
2 2
BB
BB
3 4
BWBW
WBWB
BWBW
4 2
BB
BB
WW
WW
4 4
WWBW
BBWB
WWBB
BBBB
1 5
WBBWB

输出

NO
YES
YES
YES
YES
NO
YES
NO
注

在第一个示例中,无论如何操作,都无法改变任何一个方格的颜色,因此输出 NO。

第二个示例是上面图片中的情况。如图所示,经过两次操作后,所有方格都可能变成白色,因此输出 YES。

在第三个和第四个示例中,所有方格的颜色已经是相同的,因此输出 YES。

在第五个示例中,我们可以在两次操作中完成所有操作。首先,选择位置 ( 2 , 1 ) (2,1) (2,1) 和 ( 1 , 4 ) (1,4) (1,4),将所有满足 1 ≤ x ≤ 2 1 \leq x \leq 2 1≤x≤2 和 1 ≤ y ≤ 4 1 \leq y \leq 4 1≤y≤4 的方格染成白色。然后,选择位置 ( 2 , 1 ) (2,1) (2,1) 和 ( 3 , 4 ) (3,4) (3,4),将所有满足 2 ≤ x ≤ 3 2 \leq x \leq 3 2≤x≤3 和 1 ≤ y ≤ 4 1 \leq y \leq 4 1≤y≤4 的方格染成白色。经过这两次操作后,所有方格都是白色的。

解题思路

如果要把矩阵全部变为同一个颜色,贪心的想肯定是选取四个角上的点。所以先取四个角上的点,判断是否存在对角线上的两个角相同,如果存在,则可以全部染色。在判断在一条边界上的两个角是否相同,如果相同,则看对边上是否存在存在相同的点,如果存在则可以全部染色。判断完四条边之后如果都不能,则输出"NO"。

题解

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <list>
#include <bitset>
#include <cmath>
#include <numeric>

#define endl '\n'

#define ft first
#define sd second

#define yes cout << "yes\n"
#define no cout << "no\n"

#define Yes cout << "Yes\n"
#define No cout << "No\n"

#define YES cout << "YES\n"
#define NO cout << "NO\n"

#define pb push_back
#define eb emplace_back

#define all(x) x.begin(), x.end()
#define unq_all(x) x.erase(unique(all(x)), x.end())
#define sort_all(x) sort(all(x))
#define reverse_all(x) reverse(all(x))

#define INF 0x7fffffff
#define INFLL 0x7fffffffffffffffLL

#define RED cout << "\033[91m"
#define GREEN cout << "\033[92m"
#define YELLOW cout << "\033[93m"
#define BLUE cout << "\033[94m"
#define MAGENTA cout << "\033[95m"
#define CYAN cout << "\033[96m"
#define RESET cout << "\033[0m"

// 红色
#define DEBUG1(x)                     \
    RED;                              \
    cout << #x << " : " << x << endl; \
    RESET;

// 绿色
#define DEBUG2(x)                     \
    GREEN;                            \
    cout << #x << " : " << x << endl; \
    RESET;

// 蓝色
#define DEBUG3(x)                     \
    BLUE;                             \
    cout << #x << " : " << x << endl; \
    RESET;

// 品红
#define DEBUG4(x)                     \
    MAGENTA;                          \
    cout << #x << " : " << x << endl; \
    RESET;

// 青色
#define DEBUG5(x)                     \
    CYAN;                             \
    cout << #x << " : " << x << endl; \
    RESET;

// 黄色
#define DEBUG6(x)                     \
    YELLOW;                           \
    cout << #x << " : " << x << endl; \
    RESET;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<string, ll> psl;

typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pss> vpss;

typedef vector<vi> vvi;
typedef vector<vl> vvl;

typedef queue<int> qi;
typedef queue<ll> ql;
typedef queue<pii> qpii;
typedef queue<pll> qpll;
typedef queue<psi> qpsi;
typedef queue<psl> qpsl;

typedef priority_queue<int> pqi;
typedef priority_queue<ll> pql;
typedef priority_queue<string> pqs;
typedef priority_queue<pii> pqpii;
typedef priority_queue<psi> pqpsi;
typedef priority_queue<pll> pqpl;
typedef priority_queue<psi> pqpsl;

typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<ll, ll> mll;
typedef map<ll, bool> mlb;
typedef map<char, int> mci;
typedef map<char, ll> mcl;
typedef map<char, bool> mcb;
typedef map<string, int> msi;
typedef map<string, ll> msl;
typedef map<int, bool> mib;

typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> uml;
typedef unordered_map<char, int> umci;
typedef unordered_map<char, ll> umcl;
typedef unordered_map<string, int> umsi;
typedef unordered_map<string, ll> umsl;

template <typename T>
inline void read(T &x)
{
    T f = 1;
    x = 0;
    char ch = getchar();
    while (0 == isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (0 != isdigit(ch))
        x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
    x *= f;
}

template <typename T>
inline void write(T x)
{
    if (x < 0)
    {
        x = ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

/*#####################################BEGIN#####################################*/

void solve()
{
    int n, m;
    cin >> n >> m;
    vvi a(n + 1, vi(m + 1, 0));
    char t;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> t;
            if (t == 'B')
                a[i][j] = 1;
        }
    }
    int lu = a[1][1];
    int ld = a[n][1];
    int ru = a[1][m];
    int rd = a[n][m];
    if (n == 1)
    {
        if (lu == ru)
            YES;
        else
            NO;
        return;
    }
    if (m == 1)
    {
        if (lu == ld)
            YES;
        else
            NO;
        return;
    }
    bool flag = false;
    if (lu == rd || ld == ru)
    {
        YES;
        return;
    }
    if (lu == ru)
    {
        for (int i = 1; i <= m; i++)
        {
            if (a[n][i] == lu)
            {
                YES;
                return;
            }
        }
    }
    if (ld == rd)
    {
        for (int i = 1; i <= m; i++)
        {
            if (a[1][i] == ld)
            {
                YES;
                return;
            }
        }
    }
    if (lu == ld)
    {
        for (int i = 1; i <= n; i++)
        {
            if (a[i][m] == lu)
            {
                YES;
                return;
            }
        }
    }
    if (ru == rd)
    {
        for (int i = 1; i <= n; i++)
        {
            if (a[i][1] == ru)
            {
                YES;
                return;
            }
        }
    }
    NO;
}

int main()
{
    ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    int _ = 1;
    std::cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

/*######################################END######################################*/
// 链接:

C. Everything Nim

题目翻译

爱丽丝和鲍勃正在玩一个关于 n n n 堆石子的游戏。轮到每个玩家时,他们都会选择一个正整数 k k k,这个正整数最多等于最小的非空堆的大小,然后从每个非空堆中一次性取出 k k k 个石子。第一个无法下棋的棋手(因为所有堆都是空的)就算输。

既然爱丽丝先下,那么如果双方都以最佳方式下棋,谁会赢呢?

输入

输入的第一行包含一个整数 t t t( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104)-- 测试用例的数量。

每个测试用例的第一行都包含一个整数 n n n( 1 ≤ n ≤ 2 × 1 0 5 1 \leq n \leq 2 \times 10^5 1≤n≤2×105) - 游戏中的堆数。

每个测试用例的下一行包含 n n n 个整数 a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1​,a2​,…,an​( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1≤ai​≤109),其中 a i a_i ai​ 是第 i i i 堆中的初始石子数量。

保证所有测试用例的 n n n 之和不超过 2 × 1 0 5 2 \times 10^5 2×105。

输出

对于每个测试用例,打印一行,写上获胜者的名字(假设双方都发挥出最佳水平)。如果 Alice 获胜,则打印 “Alice”,否则打印 “Bob”(不带引号)。

示例

输入

7
5
3 3 3 3 3
2
1 7
7
1 3 9 7 4 2 100
3
1 2 3
6
2 1 3 4 2 4
8
5 7 2 9 6 3 3 2
1
1000000000

输出

Alice
Bob
Alice
Alice
Bob
Alice
Alice
注

在第一个测试案例中,爱丽丝只要在第一轮选择 k = 3 k=3 k=3 ,就能一次性清空所有牌堆,从而获胜。

在第二个测试案例中,爱丽丝必须在第一轮选择 k = 1 k=1 k=1 ,因为有一堆大小为1的牌。

因此鲍勃可以在下一轮选择 k = 6 k=6 k=6 获胜。

解题思路

由于对一堆进行取操作等同于对所有堆进行取操作,所以如果一堆取完,那么相当于同等数量的所有堆取完,所有数量相同的堆可以合并。

观察发现,如果堆的数量存在逐级 + 1 +1 +1的递增序列,在这个递增序列取完之前,两个人都无法自由行动。

当递增序列取完,这个游戏就变成了一个普通的 N I M NIM NIM游戏。

所以,只要先对堆进行去重操作,然后求一下 m e x mex mex,然后判断一下奇偶性,看递增序列取完之后是谁先手。如果剩下的堆的数量大于等于 1 1 1,则先手必胜,否则必败

题解

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <list>
#include <bitset>
#include <cmath>
#include <numeric>

#define endl '\n'

#define ft first
#define sd second

#define yes cout << "yes\n"
#define no cout << "no\n"

#define Yes cout << "Yes\n"
#define No cout << "No\n"

#define YES cout << "YES\n"
#define NO cout << "NO\n"

#define pb push_back
#define eb emplace_back

#define all(x) x.begin(), x.end()
#define unq_all(x) x.erase(unique(all(x)), x.end())
#define sort_all(x) sort(all(x))
#define reverse_all(x) reverse(all(x))

#define INF 0x7fffffff
#define INFLL 0x7fffffffffffffffLL

#define RED cout << "\033[91m"
#define GREEN cout << "\033[92m"
#define YELLOW cout << "\033[93m"
#define BLUE cout << "\033[94m"
#define MAGENTA cout << "\033[95m"
#define CYAN cout << "\033[96m"
#define RESET cout << "\033[0m"

// 红色
#define DEBUG1(x)                     \
    RED;                              \
    cout << #x << " : " << x << endl; \
    RESET;

// 绿色
#define DEBUG2(x)                     \
    GREEN;                            \
    cout << #x << " : " << x << endl; \
    RESET;

// 蓝色
#define DEBUG3(x)                     \
    BLUE;                             \
    cout << #x << " : " << x << endl; \
    RESET;

// 品红
#define DEBUG4(x)                     \
    MAGENTA;                          \
    cout << #x << " : " << x << endl; \
    RESET;

// 青色
#define DEBUG5(x)                     \
    CYAN;                             \
    cout << #x << " : " << x << endl; \
    RESET;

// 黄色
#define DEBUG6(x)                     \
    YELLOW;                           \
    cout << #x << " : " << x << endl; \
    RESET;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<string, ll> psl;

typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pss> vpss;

typedef vector<vi> vvi;
typedef vector<vl> vvl;

typedef queue<int> qi;
typedef queue<ll> ql;
typedef queue<pii> qpii;
typedef queue<pll> qpll;
typedef queue<psi> qpsi;
typedef queue<psl> qpsl;

typedef priority_queue<int> pqi;
typedef priority_queue<ll> pql;
typedef priority_queue<string> pqs;
typedef priority_queue<pii> pqpii;
typedef priority_queue<psi> pqpsi;
typedef priority_queue<pll> pqpl;
typedef priority_queue<psi> pqpsl;

typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<ll, ll> mll;
typedef map<ll, bool> mlb;
typedef map<char, int> mci;
typedef map<char, ll> mcl;
typedef map<char, bool> mcb;
typedef map<string, int> msi;
typedef map<string, ll> msl;
typedef map<int, bool> mib;

typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> uml;
typedef unordered_map<char, int> umci;
typedef unordered_map<char, ll> umcl;
typedef unordered_map<string, int> umsi;
typedef unordered_map<string, ll> umsl;

template <typename T>
inline void read(T &x)
{
    T f = 1;
    x = 0;
    char ch = getchar();
    while (0 == isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (0 != isdigit(ch))
        x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
    x *= f;
}

template <typename T>
inline void write(T x)
{
    if (x < 0)
    {
        x = ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

/*#####################################BEGIN#####################################*/

const string t1 = "Alice";
const string t2 = "Bob";

void solve()
{
    int n;
    cin >> n;
    vi a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    sort(all(a));
    a.erase(unique(all(a)), a.begin());
    int m = a.size();
    int mex = 0;
    int i = 1;
    while (i < m && a[i] == a[i - 1] + 1)
    {
        i++;
        mex++;
    }
    if (i == m)
    {
        if (mex & 1)
            cout << t1 << endl;
        else
            cout << t2 << endl;
        return;
    }
    if (mex & 1)
    {
        if (a[i] - mex > 1)
            cout << t2 << endl;
        else
            cout << t1 << endl;
    }
    else
    {
        if (a[i] - mex > 1)
            cout << t1 << endl;
        else
            cout << t2 << endl;
    }
}

int main()
{
    ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    int _ = 1;
    std::cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

/*######################################END######################################*/
// 链接:

D. Missing Subsequence Sum

题目翻译

给定两个整数 n n n 和 k k k。找到一个大小最多为 25 的非负整数序列 a a a,使得满足以下条件:

  1. 没有和为 k k k 的 a a a 的子序列。
  2. 对于 v ≠ k v \neq k v​=k 所有 1 ≤ v ≤ n 1 \leq v \leq n 1≤v≤n,存在一个和为 v v v 的 a a a 的子序列。
输入

第一行包含一个整数 t t t( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1≤t≤1000)-- 测试用例的数量。

每个测试用例由一行组成,包含两个整数 n n n 和 k k k( 2 ≤ n ≤ 1 0 6 2 \leq n \leq 10^6 2≤n≤106, 1 ≤ k ≤ n 1 \leq k \leq n 1≤k≤n)-- 上述参数。

保证所有测试用例中 n n n 的总和不超过 1 0 7 10^7 107。

输出

每个测试用例的第一行输出应包含一个整数 m m m( 1 ≤ m ≤ 25 1 \leq m \leq 25 1≤m≤25)- 您所选序列的大小。

每个测试用例的第二行输出应包含 m m m 个整数 a i a_i ai​( 0 ≤ a i ≤ 1 0 9 0 \leq a_i \leq 10^9 0≤ai​≤109)- 您所选序列的元素。

如果有多个解决方案,请打印任意一个。

示例

输入

5
2 2
6 1
8 8
9 3
10 7

输出

1
1
5
2 3 4 5 6
7
1 1 1 1 1 1 1
4
7 1 4 1
4
1 2 8 3
注

在第一个例子中,我们只需要一个与 1 相加的子序列,而不需要一个与 2 相加的子序列。因此,数组 a = [ 1 ] a=[1] a=[1] 就足够了。

在第二个例子中,所有的元素都大于 k = 1 k=1 k=1,所以没有一个子序列能与 1 相加。数组中存在 1 和 n n n 之间的所有其他整数,因此有大小为 1 的子序列与这些数字相加。

解题思路

对于一个数 x x x,如果我们要获得一个长度为 m m m的序列 a a a,使得 ∑ i = 1 m a \sum _{i=1}^{m}a ∑i=1m​a,那么我们可以将 x x x转化成二进制形式, x x x上二进制位为 1 1 1的 2 2 2的幂,就是我们所需要的序列。

例如 x = 37 x=37 x=37,它的二进制序列为 100101 100101 100101,那么我们所需要的序列就是 1 ( 000001 ) , 4 ( 000100 ) , 32 ( 100000 ) 1(000001),4(000100),32(100000) 1(000001),4(000100),32(100000)。

综上,对于序列 a = 2 0 , 2 1 , 2 2 … 2 n a=2^0,2^1,2^2\dots2^n a=20,21,22…2n,我们可以拼凑出 1 ∼ 2 n + 1 − 1 1\sim 2^{n+1}-1 1∼2n+1−1的所有数。

因此,我们可以可以很容易的构造出元素和范围 [ 1 , 2 i − 1 ] , 2 i < = k [1,2^i-1],2^i<=k [1,2i−1],2i<=k​的序列。

对于元素和范围为 [ 2 i , k − 1 ] [2^i, k-1] [2i,k−1]的序列,我们只需要把 k − 2 i − 1 k-2^{i-1} k−2i−1添加进答案中即可,因为我们已经可以构造出元素和 [ 1 , 2 i − 1 − 1 ] [1,2^{i-1}-1] [1,2i−1−1]的所有数,给这些数加上 k − 2 i − 1 k-2^{i-1} k−2i−1,我们就可以构造出元素和 [ k − 2 i − 1 + 1 , k − 1 ] [k-2^{i-1}+1,k-1] [k−2i−1+1,k−1]的所有数,且任意子序列之后不会为 k k k。

对于元素和范围为 [ k + 1 , n ] [k+1, n] [k+1,n]的序列,我们可以使用 b i t s e t bitset bitset存储已经构造出元素和的数,如果这个数不存在,则把它添加进答案中,并更新 b i t s e t bitset bitset。

由于序列长度 m ≤ 25 m\leq25 m≤25,所有我们只需跟新不超过 m m m次,时间复杂度为 O ( m n ) O(mn) O(mn),不会超时。

题解

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <list>
#include <bitset>
#include <cmath>
#include <numeric>

#define endl '\n'

#define ft first
#define sd second

#define yes cout << "yes\n"
#define no cout << "no\n"

#define Yes cout << "Yes\n"
#define No cout << "No\n"

#define YES cout << "YES\n"
#define NO cout << "NO\n"

#define pb push_back
#define eb emplace_back

#define all(x) x.begin(), x.end()
#define unq_all(x) x.erase(unique(all(x)), x.end())
#define sort_all(x) sort(all(x))
#define reverse_all(x) reverse(all(x))

#define INF 0x7fffffff
#define INFLL 0x7fffffffffffffffLL

#define RED cout << "\033[91m"
#define GREEN cout << "\033[92m"
#define YELLOW cout << "\033[93m"
#define BLUE cout << "\033[94m"
#define MAGENTA cout << "\033[95m"
#define CYAN cout << "\033[96m"
#define RESET cout << "\033[0m"

// 红色
#define DEBUG1(x)                     \
    RED;                              \
    cout << #x << " : " << x << endl; \
    RESET;

// 绿色
#define DEBUG2(x)                     \
    GREEN;                            \
    cout << #x << " : " << x << endl; \
    RESET;

// 蓝色
#define DEBUG3(x)                     \
    BLUE;                             \
    cout << #x << " : " << x << endl; \
    RESET;

// 品红
#define DEBUG4(x)                     \
    MAGENTA;                          \
    cout << #x << " : " << x << endl; \
    RESET;

// 青色
#define DEBUG5(x)                     \
    CYAN;                             \
    cout << #x << " : " << x << endl; \
    RESET;

// 黄色
#define DEBUG6(x)                     \
    YELLOW;                           \
    cout << #x << " : " << x << endl; \
    RESET;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<string, ll> psl;

typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pss> vpss;

typedef vector<vi> vvi;
typedef vector<vl> vvl;

typedef queue<int> qi;
typedef queue<ll> ql;
typedef queue<pii> qpii;
typedef queue<pll> qpll;
typedef queue<psi> qpsi;
typedef queue<psl> qpsl;

typedef priority_queue<int> pqi;
typedef priority_queue<ll> pql;
typedef priority_queue<string> pqs;
typedef priority_queue<pii> pqpii;
typedef priority_queue<psi> pqpsi;
typedef priority_queue<pll> pqpl;
typedef priority_queue<psi> pqpsl;

typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<ll, ll> mll;
typedef map<ll, bool> mlb;
typedef map<char, int> mci;
typedef map<char, ll> mcl;
typedef map<char, bool> mcb;
typedef map<string, int> msi;
typedef map<string, ll> msl;
typedef map<int, bool> mib;

typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> uml;
typedef unordered_map<char, int> umci;
typedef unordered_map<char, ll> umcl;
typedef unordered_map<string, int> umsi;
typedef unordered_map<string, ll> umsl;

template <typename T>
inline void read(T &x)
{
    T f = 1;
    x = 0;
    char ch = getchar();
    while (0 == isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (0 != isdigit(ch))
        x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
    x *= f;
}

template <typename T>
inline void write(T x)
{
    if (x < 0)
    {
        x = ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

/*#####################################BEGIN#####################################*/

const int N = 2e6 + 5;
bitset<N> vis;

void init()
{
    vis.reset();
    vis[0] = 1;
}

void solve()
{
    init();
    int n, k;
    cin >> n >> k;
    vi ans;
    int bit = 1;
    while (bit * 2 - 1 < k)
    {
        ans.eb(bit);
        for (int i = n; i >= bit; i--)
        {
            vis.set(i, vis[i] || vis[i - bit]);
        }
        bit <<= 1;
    }
    int t = k - bit;
    if (t > 0)
    {
        ans.eb(t);
        for (int i = n; i >= t; i--)
        {
            vis.set(i, vis[i] || vis[i - t]);
        }
    }
    for (bit = k + 1; bit <= n; bit++)
    {
        if (!vis[bit])
        {
            ans.eb(bit);
            for (int i = n; i >= bit; i--)
            {
                vis.set(i, vis[i] || vis[i - bit]);
            }
        }
    }
    cout << ans.size() << endl;
    for (auto i : ans)
        cout << i << " ";
    cout << endl;
}

int main()
{
    ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    int _ = 1;
    std::cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

/*######################################END######################################*/
// 链接:
多周期路径约束(set_multicycle_path ) 1.由慢到快的多周期约束 如下图所示,发射时钟周期是接受时钟周期的4倍。默认情况下,按照1T原则来进行静态时序分析和布局布线的。即建立时间是在发射沿后的第一个捕获沿检查,而保持时间是同一时刻下源时钟和捕获时钟的触发边沿下检查的(建立时间检查边沿的前一个触发沿)。 但是这样导致的问题就是该路径过于苛刻,为了满足这条路径的要求,不得不放弃其他路径。或者根本没办法保证这个默认的约束。总之最终造成... 阅读详情

相关推荐

RTX 4060笔记本也能跑!保姆级教程:用Ollama在Windows上部署DeepSeek-R1-8B模型

本文详细介绍了如何在RTX 4060笔记本上通过Ollama本地部署DeepSeek-R1-8B模型,提供从环境配置到性能调优的全流程优化指南。针对笔记本用户的显存、散热等问题,给出了量化模型选择、启动参数优化及散热解决方案,帮助用户在消费级硬件上高效运行大语言模型。

weixin_28745067的博客 526

Codeforces Round 941 (Div. 2) (A~D)

Codeforces Round 941 (Div. 2) (A~D)题目大意 + 思路 + AC代码

weixin_74292291的博客 1245

从URDF到Gazebo:一个完整的ROS2机械臂仿真项目实战与避坑指南

本文提供了一个完整的ROS2机械臂仿真项目实战指南,详细讲解了从URDF建模、ROS2 Control配置、MoveIt2集成到Gazebo仿真的全流程。文章重点分享了URDF惯性参数设置、Gazebo插件选择、MoveIt2配置修正等关键步骤的避坑经验,帮助开发者快速搭建可用的六轴机械臂仿真环境,实现从静态模型到物理仿真的跨越。

weixin_29279153的博客 275

Codeforces Round 941 (Div. 2) ------ A ---- F (题解)

前情提要:这场全是思维题,一点算法没有,所以脑子好就能上分。但是个人认为这种场没有啥意义,所以t宝不会出题,下次别出了。(狗头保命)

weixin_73936404的博客 1159

移远EC20模块USB模式切换的底层逻辑与驱动适配探秘

本文深入探讨了移远EC20模块USB模式切换的底层逻辑与驱动适配机制,详细解析了AT命令控制下的USB0与WWAN0模式差异。通过分析USB协议栈、内核驱动框架及网络子系统的交互,提供了模式切换的完整操作流程和常见问题解决方案,助力开发者优化4G通信模块在嵌入式设备中的稳定连接。

cc789的博客 845

Cesium 相机操作

Cesium 提供了三种方式对相机的位置进行摆放。

虎康的博客 2590

Codeforces Round 941 (Div. 2)(A-D)

给定n张牌,每次选k张相同的牌,把他们变成k-1张任意的牌,求最后手中最少能有几张牌。直接判断这n张牌当中有没有k张一样的牌,如果有就一定能变成最后只剩k-1张牌。如果没有k张一模一样的牌,说明一次变化都做不了,最后只能有n张牌。

m0_74183164的博客 1047

损失函数大汇总(三十八)(AUCPR Loss附公式推导与代码)

本文系统介绍了AUCPR Loss的原理与应用。针对不平衡分类任务中AUC指标不够敏感的问题,提出通过优化PR曲线面积(AUCPR)来提升模型性能。文章详细推导了AUCPR的可微近似实现:首先用sigmoid函数软化阈值判断,构造可微的TP/FP/FN计数;然后基于软Precision和Recall计算近似PR曲线面积;最后通过数值积分得到可优化的损失函数。特别讨论了温度参数τ对训练稳定性的影响,并比较了AUCPR与AUC、AP等指标的异同。该方法适用于医学筛查、欺诈检测等极端不平衡场景,能更准确反映模型对

悦学共鸣,温柔以待,汇聚光芒,共同成长 441

【codeforces】Codeforces Round #291 (Div. 2) 题解

这个寒假懒到爆啦~题目做了也都没写题解,今天补一个~这套题今天自己开了虚拟比赛模拟了下,只做出四题,感觉自己弱爆了T T,E题是吃饭的时候突然想到解法的。 传送门:Codeforces Round #291 (Div. 2) 514A. Chewbaсca and Number 模拟就好了,注意开头数字为9不能变。 #include #include #includ

poursoul 1125

基于深度学习的心律失常的自动分类

基于深度学习的心律失常的自动分类 作者:禅与计算机程序设计艺术 1. 背景介绍 1.1 心律失常的危害与检测的重要性 心律失常是一种常见的心血管疾病,严重威胁着人们的健康和生命。据统计,全球每年有数百万人因心律失

AI架构师小马 1146

华为OD机试 - 去除多余空格(Python/JS/C/C++ 2024 E卷 100分)

本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。去除文本多余空格,但不去除配对单引号之间的多余空格。给出关键词的起始和结束下标,去除多余空格后刷新关键词的起始和结束下标。条件约束:输入为两行字符串:第一行:待去除多余空格的文本,用例保证如果有单引号,则单引号成对出现,且单引号可能有多对。 第二

学Java,找哪吒 1216

Codeforces Round 941 (Div. 2)

但是如果先手遇到前后差值为1的,那么先手会处于被动,先手就不能有效控制后手了,所以先手可以选择拿光,变成后手,下一步又变成先手,也可以现在控制局势 ,下一步变成后手 ,所以仍可以控制局势,所以谁最先开始控制局势谁赢,或者都控制不了局势,那么直接模拟最终结果。题意:对于一个子矩阵, 如果两个角颜色相同,那么可以将整个矩阵颜色变得和 两个角一样,问能否使得整个矩阵颜色相同。先看左上角和右下角,如果不一样 ,就看右下角那一列和那一行上是否都有左上角,同理 ,看左上角。其实,只要让整个矩阵的两个角颜色相同即可。

m0_74087709的博客 725

目前固态激光雷达在机器人领域的应用难点汇总

固态激光雷达在机器人领域面临性能与成本的双重挑战:探测性能上,动态范围不足(60-80dB)导致高/低反射物体识别困难,近距盲区(30-50cm)与长距精度(±3cm)矛盾突出;结构上视场角与点云密度难以平衡,多雷达拼接又带来同步误差。成本方面,核心元件(SPAD/OPA芯片)良率低(<50%)、校准工序复杂,单机成本超200美元。环境适应性上,粉尘附着、振动偏移(±2°)和光衰(1万小时衰减15%)制约可靠性。

start_up_go的博客 482

Codeforces Round 941

Codeforces Round 941

青少年趣味编程 744

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Codeforces Round 941 (Div. 2 ABCDE题) 视频讲解

Codeforces Round 941 (Div. 2) A、B、C、D、E题视频讲解

Edward Shi的博客 1899

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Codeforces Round 945

Codeforces Round 945

青少年趣味编程 1064

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Codeforces Round 941 (Div. 2)(A-D)

补题

Vcoistnt的博客 820

Codeforces Round 971

Codeforces Round 971

青少年趣味编程 1514

Codeforces Round 960

Codeforces Round 960

青少年趣味编程 705

Codeforces Round 967

Codeforces Round 967

青少年趣味编程 1004

Codeforces Round 963

Codeforces Round 963

青少年趣味编程 696

Codeforces Round 948

Codeforces Round 948

青少年趣味编程 908

Codeforces Round 950

Codeforces Round 950

青少年趣味编程 966

Codeforces Round 941 (Div. 2) ABC

每次操作是对所有堆都拿的(那相同大小的堆就没有意义,统计一次即可),最大为当前最小堆的值,最少拿一个。看最开始谁先手,如果开局不是1,那么就是Alice先手,不然就同上,偶数个是Alice。如果连续是奇数个,就失去了。从大到小先一直变,像“招募”一样,每个位置,损失一个,然后全部招募下来,然后就是看连续的情况了,只要不连续,我就可以一直掌握“主动权”。首先先明确,如果现在都一样比如n个,可以一直操作直到 k-1。不管中间怎么变,最后肯定要把边上的,死角的给改变。直到不能招募,把总招募的压缩为k-1即可。

JK01WYX的博客 689

Codeforces Round 941 (Div. 2) (A~D)

然后考虑若此时存在两个堆,那么对于Alice而言,其操作可以使得小的那个堆只剩下1,下一步Bob必须选1,然后Alice就胜利了。相反,若小的那个堆一开始就是1,那么Alice就输了。接下来推广到若干堆的情况:对于任意一方而言,若最小堆不是1,那么便可以控制胜利,于是我们只需要枚举到最小堆不是1的情况即可,然后看是谁在操作就是谁赢。思路:若要使得所有方格同色,关键在于四个角上的颜色,若对角上的颜色一样就能直接将所有格子变为同色。思路:首先处理 < k 的部分,我们可以通过二进制的思想来构造整数,例如。

weixin_61825750的博客 1217

Codeforces Round 941 (Div.1) & (Div. 2) A~E

你有n张牌,每张牌上都写着一个数字,还有一个固定整数k。kk−1在这个过程结束时,你手中最少有多少张牌?

Code_Shark的博客 1472
上一篇: 20240427codeforce刷题题解
下一篇: 2024ICPC武汉邀请赛个人题解
ExtractStars
博客等级 码龄4年 1053粉丝 48原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值