Codeforces Round #747 (Div. 2)

A. Consecutive Sum Riddle

思路分析:

  • 一开始想起了那个公式\(l + (l + 1) + … + (r − 1) + r = (l + r)(r - l + 1) / 2\)。
  • 然后一看令\(l + r = 1\)最合适,那么就有\(l = r - 1\),一代入就得到\(r = n, l = -n + 1\)。
  • 没想通为什么没有一眼看出来。

代码

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. int main()
  5. {
  6. int t;
  7. cin >> t;
  8. while (t--)
  9. {
  10. ll n;
  11. cin >> n;
  12. cout << -n + 1 << ' ' << n << endl;
  13. }
  14. return 0;
  15. }

B. Special Numbers

思路分析

  • 这题也是想久了,其实列一下规律一下就出来了(当然不排除大佬一眼看出来。
  • 我们列一下前几项吧。
  • \(k = 1,2,3,4,5\),我们分别选的是\(n ^ 0\),\(n ^ 1\),\(n ^ 0 + n ^ 1\),\(n ^ 2\),\(n ^ 0 + n ^ 2\)。
  • 然后我们就可以得出一个规律,那就是我们把\(k\)变成二进制,如果当前二进制位为\(1\)的话我们就加上\(n ^ x\),\(x\)是指该二进制位是第几位,然后注意longlong 和 取模即可。

代码

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. const ll mod = 1e9 + 7;
  5. int main()
  6. {
  7. ios::sync_with_stdio(0);
  8. cin.tie(0);
  9. cout.tie(0);
  10. int t;
  11. cin >> t;
  12. while (t--)
  13. {
  14. ll n, k;
  15. cin >> n >> k;
  16. ll ans = 0;
  17. ll p = 1;
  18. for (int j = 0; j <= 31; j++)
  19. {
  20. if (k & (1 << j))
  21. {
  22. ans = (ans + p) % mod;
  23. }
  24. p *= n;
  25. p %= mod;
  26. }
  27. cout << ans << endl;
  28. }
  29. return 0;
  30. }

C. Make Them Equal

思路分析

  • 这题也挺简单的,很容易想到最多需要两次操作,因为\(1 <= x <= n\),所以我们只要选\(n - 1\)和 \(n\)必然能完成任务,因为选\(n\)就把除\(n\)这个位置以外的位置全部弄好了,然后就是\(n-1\)必然不会被\(n\)整除。
  • 所以我们就要思考一下只要一次操作和0次操作的情况。
  • 看下题目要求的时间,试试暴力(乌鱼子,我还想是不是质因数分解然后拿最小的质因数和\(n\)比大小,不知道有同学这样试了没)。
  • 暴力的时候注意一下,\(o(n^2)\)是过不了这题的,所以我们以\(x\)为第一层循环,这样能优化时间。因为这样的话我们下标就不用一个一个遍历,只需要加上\(x\)即可。

代码

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. int main()
  5. {
  6. ios::sync_with_stdio(0);
  7. cin.tie(0);
  8. cout.tie(0);
  9. int t;
  10. cin >> t;
  11. while (t--)
  12. {
  13. vector<int> ans;
  14. bool ok = true;
  15. int n;
  16. cin >> n;
  17. char ch;
  18. cin >> ch;
  19. string s;
  20. cin >> s;
  21. for (int i = 0; i < s.size(); i++)
  22. {
  23. if (s[i] != ch)
  24. {
  25. ok = false;
  26. }
  27. }
  28. if (!ok)
  29. {
  30. for (int i = 1; i <= n; i++)
  31. {
  32. ok = true;
  33. for (int j = i; j <= n; j++)
  34. {
  35. ok &= (s[j - 1] == ch);
  36. j += i - 1;
  37. }
  38. if (ok)
  39. {
  40. ans.push_back(i);
  41. break;
  42. }
  43. }
  44. }
  45. if (!ok)
  46. {
  47. ans.push_back(n);
  48. ans.push_back(n - 1);
  49. }
  50. cout << ans.size() << endl;
  51. for (int x : ans)
  52. {
  53. cout << x << ' ';
  54. }
  55. cout << endl;
  56. }
  57. return 0;
  58. }

D. The Number of Imposters

思路分析

  • 我们可以把\(imposter\)表示为相反关系,即如果我认为他说的是假话,那么如果我说的是真的,他就是假的,我如果是假的,他就是真的,\(crewmate\)刚好相反。
  • 我们考虑用带权并查集解决这个问题,我们维护几个根节点,因为题目所给的点必定能形成几颗树。
  • 我们共要维护两个值,一个是与根节点相同关系的节点个数,一个是与根节点相反关系的节点的个数。
  • 这样的话答案就是对于每一个根节点,取两种类型中最大的值。
  • 那么我们如何来维护这个个数或者说如何构造出这两种节点呢?
  • 首先,我们维护一种关系,1表示相反,0表示相同。那么这个点与根节点相同和相反就和中间点的过程有关了。具体看代码。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 2e5 + 10;
  4. int p[maxn], dis[maxn];
  5. int cnt[maxn][2];
  6. int find(int x)
  7. {
  8. if (x != p[x])
  9. {
  10. int root = find(p[x]);
  11. dis[x] ^= dis[p[x]];
  12. //dis[p[x]],所以其实就是判断x和它的根节点是否关系相同
  13. p[x] = root;
  14. }
  15. return p[x];
  16. //找到父节点并更新dis
  17. }
  18. int main()
  19. {
  20. ios::sync_with_stdio(0);
  21. cin.tie(0);
  22. cout.tie(0);
  23. int t;
  24. cin >> t;
  25. while (t--)
  26. {
  27. int n, m;
  28. cin >> n >> m;
  29. for (int i = 1; i <= n; i++)
  30. {
  31. p[i] = i;
  32. dis[i] = 0;
  33. cnt[i][0] = 1;
  34. cnt[i][1] = 0;
  35. //重置
  36. }
  37. bool flag = 1;
  38. for (int i = 1; i <= m; i++)
  39. {
  40. int u, v;
  41. string s;
  42. cin >> u >> v >> s;
  43. bool val = s[0] == 'i' ? 1 : 0;
  44. //当前两个点的关系
  45. int fu = find(u), fv = find(v);
  46. if (fu == fv)
  47. {
  48. if ((dis[u] ^ dis[v]) != val)
  49. {
  50. flag = 0;
  51. }
  52. //如果两个点已经在同一棵子树了,如果这两个点与根节点的关系异或出来不是输入的关系时矛盾
  53. }
  54. else
  55. {
  56. p[fv] = fu;
  57. dis[fv] = dis[u] ^ dis[v] ^ val;
  58. //把这两个点的父节点连起来,那么父节点的关系应该变成这个个节点异或起来再和当前关系异或即可
  59. cnt[fu][1] += cnt[fv][dis[fv] ^ 1];
  60. //1表示与根节点相反
  61. cnt[fu][0] += cnt[fv][dis[fv]];
  62. //0表示与根节点相同
  63. }
  64. }
  65. if (!flag)
  66. {
  67. cout << -1 << endl;
  68. }
  69. else
  70. {
  71. int ans = 0;
  72. for (int i = 1; i <= n; i++)
  73. {
  74. if (find(i) == i)
  75. {
  76. ans += max(cnt[i][0], cnt[i][1]);
  77. }
  78. }
  79. cout << ans << endl;
  80. }
  81. }
  82. return 0;
  83. }

E1. Rubik's Cube Coloring (easy version)

思路分析

  • 这题太水了吧,直接第一个节点能选六个,其他节点只能选四种颜色,所以答案就是\(6\times4^{2^k - 2}\)。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. const ll mod = 1e9 + 7;
  5. ll qpow(ll a, ll b)
  6. {
  7. ll ans = 1;
  8. while (b)
  9. {
  10. if (b & 1)
  11. ans = ans * a % mod;
  12. a = a * a % mod;
  13. b >>= 1;
  14. }
  15. return ans;
  16. }
  17. int main()
  18. {
  19. ios::sync_with_stdio(0);
  20. cin.tie(0);
  21. cout.tie(0);
  22. int k;
  23. cin >> k;
  24. ll ans = qpow(4, (1ll << k) - 2) % mod * 6 % mod;
  25. cout << ans << endl;
  26. return 0;
  27. }

Codeforces Round #747 (Div. 2) Editorial的更多相关文章

  1. Codeforces Round #590 (Div. 3) Editorial

    Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...

  2. Codeforces Round #544 (Div. 3) Editorial C. Balanced Team

    http://codeforces.com/contest/1133/problem/Ctime limit per test 2 secondsmemory limit per test 256 m ...

  3. Codeforces Round #710 (Div. 3) Editorial 1506A - Strange Table

    题目链接 https://codeforces.com/contest/1506/problem/A 原题 1506A - Strange Table Example input 5 1 1 1 2 ...

  4. Codeforces Round #453 ( Div. 2) Editorial ABCD

    A. Visiting a Friend time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #448(Div.2) Editorial ABC

    被B的0的情况从头卡到尾.导致没看C,心情炸裂又掉分了. A. Pizza Separation time limit per test 1 second memory limit per test ...

  6. Codeforces Round #747 (Div. 2)

    比赛地址 A(水题) 题目链接 题目: 给出指定\(n\),求解出一段区间\([l,r]\)使得\(\sum\limits_{i=l}^ri=n\) 解析: 从点0,1两点作为起点分别向左右延伸长度, ...

  7. Codeforces Round #747 (Div. 2)题解

    谢天谢地,还好没掉分,还加了8分,(8分再小也是加啊)前期刚开始有点卡,不过在尽力的调整状态之后,还是顺利的将前面的水题过完了,剩下的E2和F题就过不去了,估计是能力问题,自己还是得认真补题啦. E2 ...

  8. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. 一、自动化监控利器-Zabbix

    目录 1. 监控的作用 1.1 为何需要监控系统 1.2 监控系统的实现 1.3 常用的监控软件 2. Zabbix简介 2.1 选择Zabbix的理由 2.2 Zabbix的功能特性 3. Zabb ...

  2. Linux基础命令(基于CentOS7)

    1.帮助相关命令 man 查看普通命令的帮助 --help 只能查看内置命令 info 查看一个命令的更多信息 type 查看是否为内置命令 2.关机重启 shutdown -h 关机 -r 重启 - ...

  3. 关于electron-vue打包后静态视频文件无法正常加载的问题解决方法

    最近在使用electron-builder构建vue项目的时候发现在生产模式下视频可以正常加载并显示,但是一旦打包到开发环境下,视频就读取不出来了,控制台也并没有报错 一开始博主以为是路径问题,在将路 ...

  4. GO安装golang.org/x/net扩展库

    在学习golang过程中,有部分示例代码使用到了非标准库golang.org/x/net/html相关的库函数,但是标准代码库中没有该库,因此需要自己安装: 我这里使用git下载源码进行的安装. 为了 ...

  5. try catch处理流的异常

    1.try catch处理异常 try{} catch(Exception e){} finally{ 必然执行的代码,一般是释放资源 } 2.流使用try catch处理异常 其中,变量作用域只在当 ...

  6. 添加class和删除class以及判断是否含有class

      addClass(document.body, 'showRightPanel') removeClass(document.body, 'showRightPanel') /**  * Add  ...

  7. 判断输入框中输入的日期格式为yyyy-mm-dd和正确的日期

    判断输入框中输入的日期格式为yyyy-mm-dd和正确的日期   function IsDate(str) { //如果是正确的日期格式返回true,否则返回false var regExp; reg ...

  8. logstash-input-jdbc配置说明

    Logstash由三个组件构造成,分别是input.filter以及output.我们可以吧Logstash三个组件的工作流理解为:input收集数据,filter处理数据,output输出数据.至于 ...

  9. (未完)Java集合框架梳理(基于JDK1.8)

    Java集合类主要由两个接口Collection和Map派生出来的,Collection派生出了三个子接口:List.Set.Queue(Java5新增的队列),因此Java集合大致也可分成List. ...

  10. 个人作业--体温上报APP

    第一阶段目标: 1.要求增加用户注册功能,用户注册信息包括用户ID(学号).用户名(姓名),手机号码,用户单位(班级),用户班级四项基本信息,用户第一次注册后,用户姓名不用每次输入 . 2.体温上报界 ...