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\)。
  • 没想通为什么没有一眼看出来。

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
ll n;
cin >> n;
cout << -n + 1 << ' ' << n << endl;
}
return 0;
}

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 和 取模即可。

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
ll n, k;
cin >> n >> k;
ll ans = 0;
ll p = 1;
for (int j = 0; j <= 31; j++)
{
if (k & (1 << j))
{
ans = (ans + p) % mod;
}
p *= n;
p %= mod;
}
cout << ans << endl;
}
return 0;
}

C. Make Them Equal

思路分析

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

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
vector<int> ans;
bool ok = true;
int n;
cin >> n;
char ch;
cin >> ch;
string s;
cin >> s;
for (int i = 0; i < s.size(); i++)
{
if (s[i] != ch)
{
ok = false;
}
}
if (!ok)
{
for (int i = 1; i <= n; i++)
{
ok = true;
for (int j = i; j <= n; j++)
{
ok &= (s[j - 1] == ch);
j += i - 1;
}
if (ok)
{
ans.push_back(i);
break;
}
}
}
if (!ok)
{
ans.push_back(n);
ans.push_back(n - 1);
}
cout << ans.size() << endl;
for (int x : ans)
{
cout << x << ' ';
}
cout << endl;
}
return 0;
}

D. The Number of Imposters

思路分析

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

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int p[maxn], dis[maxn];
int cnt[maxn][2];
int find(int x)
{
if (x != p[x])
{
int root = find(p[x]);
dis[x] ^= dis[p[x]];
//dis[p[x]],所以其实就是判断x和它的根节点是否关系相同
p[x] = root;
}
return p[x];
//找到父节点并更新dis
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
p[i] = i;
dis[i] = 0;
cnt[i][0] = 1;
cnt[i][1] = 0;
//重置
}
bool flag = 1;
for (int i = 1; i <= m; i++)
{
int u, v;
string s;
cin >> u >> v >> s;
bool val = s[0] == 'i' ? 1 : 0;
//当前两个点的关系
int fu = find(u), fv = find(v);
if (fu == fv)
{
if ((dis[u] ^ dis[v]) != val)
{
flag = 0;
}
//如果两个点已经在同一棵子树了,如果这两个点与根节点的关系异或出来不是输入的关系时矛盾
}
else
{
p[fv] = fu;
dis[fv] = dis[u] ^ dis[v] ^ val;
//把这两个点的父节点连起来,那么父节点的关系应该变成这个个节点异或起来再和当前关系异或即可
cnt[fu][1] += cnt[fv][dis[fv] ^ 1];
//1表示与根节点相反
cnt[fu][0] += cnt[fv][dis[fv]];
//0表示与根节点相同
}
}
if (!flag)
{
cout << -1 << endl;
}
else
{
int ans = 0;
for (int i = 1; i <= n; i++)
{
if (find(i) == i)
{
ans += max(cnt[i][0], cnt[i][1]);
}
}
cout << ans << endl;
}
}
return 0;
}

E1. Rubik's Cube Coloring (easy version)

思路分析

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

代码

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
ll qpow(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k;
cin >> k;
ll ans = qpow(4, (1ll << k) - 2) % mod * 6 % mod;
cout << ans << endl;
return 0;
}

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. Redis的安装、基本使用以及与SpringBoot的整合

    1.概述 Redis 是现在很流行的一个 NoSql 数据库,每秒读取可以达到10万次,能够将数据持久化,支持多种数据结构,容灾性强,易扩展,常用于项目的缓存中间件. 今天我们就来聊聊关于Redis的 ...

  2. JS013. 重写toFixed( )方法,toFixed()原理 - 四舍五入?银行家舍入法?No!六舍七允许四舍五入√!

    以下为场景实测与原理分析,需要重写函数请直接滚动至页尾!!! 语法 - Number.prototype.toFixed( ) // toFixed()方法 使用定点表示法来格式化一个数值. numO ...

  3. Linux残留的EFI启动项删除后又恢复的问题

    电脑Windows + Fedora双系统,UEFI启动,共用同一个EFI分区.现在删除了Fedora系统,那么应该将EFI分区中的Fedora启动项也删除之. 按照网上的办法,在Windows上,尝 ...

  4. utittest和pytest中mock的使用详细介绍

    头号玩家 模拟世界 单元测试库介绍 mock Mock是Python中一个用于支持单元测试的库,它的主要功能是使用mock对象替代掉指定的Python对象,以达到模拟对象的行为. python3.3 ...

  5. python中字典按键、值进行排序

    看到排序,就不禁想起python中的sort和sorted sort是列表中的方法,用于对列表进行排序(改变的是原列表,不返回新列表) 用法: list.sort(key=None,reverse=T ...

  6. 【PHP数据结构】顺序表(数组)的相关逻辑操作

    在定义好了物理结构,也就是存储结构之后,我们就需要对这个存储结构进行一系列的逻辑操作.在这里,我们就从顺序表入手,因为这个结构非常简单,就是我们最常用的数组.那么针对数组,我们通常都会有哪些操作呢? ...

  7. PHP中PDO关闭连接的问题

    在之前我们手写 mysql 的连接操作时,一般都会使用 mysql_close() 来进行关闭数据库连接的操作.不过在现代化的开发中,一般使用框架都会让我们忽视了底层的这些封装,而且大部分框架都已经默 ...

  8. switchery插件:多个按钮,用jquery进行切换

    单个按钮可以参照这个链接https://blog.csdn.net/u012233776/article/details/53305846 多个按钮时, html中其中想操作这个按钮开启与关闭 < ...

  9. 执行:vim /etc/profile,提示:Command 'vim' not found, but can be installed with:

    root@uni-virtual-machine:/# vim /etc/profile Command 'vim' not found, but can be installed with: apt ...

  10. P3971-[TJOI2014]Alice and Bob【贪心】

    正题 题目链接:https://www.luogu.com.cn/problem/P3971 题目大意 一个\(1\sim n\)的一个排列,设\(a_i\)表示以\(i\)结尾的最长上升子序列长度, ...