• Codeforces Round #804 (Div. 2)


    目录

    官方题解

    A. The Third Three Number Problem

    B. Almost Ternary Matrix

    C. The Third Problem


    官方题解

    点击跳转:官方题解

    A. The Third Three Number Problem

    A. The Third Three Number Problem

    思路

    首先,⊕(异或) 又称不进位加法,所以:对于 (a⊕b)+(b⊕c)+(a⊕c) 的最后的一位而言

    (a⊕b)+(b⊕c)+(a⊕c) = a + b + b + c + a + c = 2*(a + b + c) 必为偶数

    故:当 n 为奇数时,无解

    当 n 为偶数时:

    知:a⊕0 = a ,所以,令 a = 0, b = n / 2, c = n / 2;

    此时:a⊕b = n/2,a⊕c = n/2,b⊕c = 0,结果为 n 成立

    代码如下

    1. #include <bits/stdc++.h>
    2. #define fast ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
    3. using namespace std;
    4. typedef long long LL;
    5. typedef pair<int, int> PII;
    6. const int N = 2e5 + 10, mod = 1e9 + 7;
    7. int T;
    8. int lowbit(int x)
    9. {
    10. return x & -x;
    11. }
    12. void solve()
    13. {
    14. int n, c;
    15. scanf("%d", &n);
    16. if(n%2)
    17. {
    18. puts("-1");
    19. return ;
    20. }
    21. printf("%d %d %d\n", 0, n/2, n/2);
    22. //printf("%d\n", res);
    23. }
    24. int main()
    25. {
    26. //fast;
    27. //cin >> T;
    28. scanf("%d", &T);
    29. while(T -- )
    30. solve();
    31. return 0;
    32. }

    B. Almost Ternary Matrix

    B. Almost Ternary Matrix

    思路

    类似:

    10011001

    01100110

    01100110

    10011001

    模拟构造即可

    代码如下

    1. #include <bits/stdc++.h>
    2. #define fast ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
    3. using namespace std;
    4. typedef long long LL;
    5. typedef pair<int, int> PII;
    6. const int N = 110, mod = 1e9 + 7;
    7. int T;
    8. int lowbit(int x)
    9. {
    10. return x & -x;
    11. }
    12. void solve()
    13. {
    14. int n, m;
    15. scanf("%d %d", &n, &m);
    16. string r1, r2;
    17. while(true)
    18. {
    19. if(r1.size() < 2 * m) r1 += "1 0 ";
    20. else break;
    21. if(r1.size() < 2 * m) r1 += "0 1 ";
    22. else break;
    23. }
    24. while(true)
    25. {
    26. if(r2.size() < 2 * m) r2 += "0 1 ";
    27. else break;
    28. if(r2.size() < 2 * m) r2 += "1 0 ";
    29. else break;
    30. }
    31. int a[N][N];
    32. for(int i = 1; i <= n; i ++ )
    33. {
    34. if(i % 4 == 1 || i % 4 == 0) cout << r1 << endl;
    35. else cout << r2 << endl;
    36. }
    37. //printf("%d\n", res);
    38. }
    39. int main()
    40. {
    41. //fast;
    42. //cin >> T;
    43. scanf("%d", &T);
    44. while(T -- )
    45. solve();
    46. return 0;
    47. }

    此段代码也有类似效果: 

    1. void solve()
    2. {
    3. int n, m;
    4. scanf("%d %d", &n, &m);
    5. for(int i = 1; i <= n; i ++ )
    6. {
    7. for(int j = 1; j <= m; j ++ )
    8. cout << ((i % 4 <= 1)==(j % 4 <= 1)) << " ";
    9. puts("");
    10. }
    11. //printf("%d\n", res);
    12. }

    C. The Third Problem

    C. The Third Problem

    思路

    res = 每个数能变换的范围

    分析:

    01的位置不能变;

    对于其余的数,小于本数的数在本数的一侧,则本数也不能动;

    其余的数能动,能动的范围为比本数小的数组成的区间;

    代码如下

    1. #include <bits/stdc++.h>
    2. #define fast ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
    3. using namespace std;
    4. typedef long long LL;
    5. typedef pair<int, int> PII;
    6. const int N = 1e5 + 10, mod = 1e9 + 7;
    7. int T;
    8. void solve()
    9. {
    10. int n, m;
    11. scanf("%d", &n);
    12. int a[N] = {0}, p[N] = {0};
    13. for(int i = 0; i < n; i ++ )
    14. {
    15. scanf("%d", &a[i]);
    16. p[a[i]] = i;
    17. }
    18. int res = 1;
    19. int l = p[0], r = p[0];
    20. for(int i = 1; i < n; i ++ )
    21. {
    22. if(p[i] < l) l = p[i];
    23. else if(p[i] > r) r = p[i];
    24. else res = (LL)res*(r - l + 1 - i) % mod;
    25. //cout << res << endl;
    26. }
    27. printf("%d\n", res);
    28. //printf("%d\n", res);
    29. }
    30. int main()
    31. {
    32. //fast;
    33. //cin >> T;
    34. scanf("%d", &T);
    35. while(T -- )
    36. solve();
    37. return 0;
    38. }

  • 相关阅读:
    D如何支持写障碍
    一文搞懂 Python 的模块和包,在实战中的最佳实践
    服务器风波始末-暨给臭hs道歉
    Mac | 崩溃分析
    Python 算法高级篇:深入理解复杂度分析-时间复杂度与空间复杂度
    嵌入式Linux入门-手把手教你初始化SDRAM(附代码)
    C++对象模型剖析(六)一一Data语义学(三)
    微信小程序和微信公众号的区别和优势
    web:[HCTF 2018]WarmUp
    IDC第一的背后,阿里云在打造怎样的一朵“视频云”?
  • 原文地址:https://blog.csdn.net/m0_61409183/article/details/125615254