比赛链接:https://atcoder.jp/contests/abc179/tasks

A - Plural Form

题意

给出一个由小写字母组成的单词,如果单词以 $s$ 结尾,在单词的末尾加上 $es$,否则在单词的末尾加上 $s$ 。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
cout << s + (s.back() == 's' ? "es" : "s") << "\n";
return 0;
}

B - Go to Jail

题意

给出一对骰子投掷 $n$ 次的结果,问是否有连续三次两个骰子的点数相同。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> x(n), y(n);
for (int i = 0; i < n; i++)
cin >> x[i] >> y[i];
for (int i = 0; i + 2 < n; i++) {
if (x[i] == y[i] and x[i + 1] == y[i + 1] and x[i + 2] == y[i + 2]) {
cout << "Yes" << "\n";
return 0;
}
}
cout << "No" << "\n";
return 0;
}

C - A x B + C

题意

给出一个正整数 $n$,问有多少不同的三元组 $(a, b, c)$ 满足 $a,b,c > 0$ 且 $a \times b + c = n$ 。

题解

枚举 $a$ 的值,与之对应的 $b$ 的最大值为 $\lfloor \frac{n}{a} \rfloor$,然后判断是否都能取到即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
long long ans = 0;
for (int a = 1; a <= n; a++) {
ans += n / a - (n % a == 0);
}
cout << ans << "\n";
return 0;
}

D - Leaping Tak

题意

给出 $k$ 个区间,区间并集中的整数为每次可以选择行走的距离,问在数轴上从点 $1$ 走到点 $n$ 的路径数目。

题解

与上一场的D题类似,可以考虑如下代码:

dp[1] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < k; j++) {
for (int k = l[j]; k <= r[j]; k++) {
(dp[i + k] += dp[i]) %= MOD;
}
}
}

但是 $O_{(n^2k)}$ 的复杂度明显会超时。

注意到第三层循环为区间操作,所以可以考虑用差分或线段树降低复杂度。

代码一

差分,时间复杂度为 $O_{(nk)}$ 。

#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 998244353;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> l(k), r(k);
for (int i = 0; i < k; i++)
cin >> l[i] >> r[i];
vector<long long> dp(2 * n + 100);
dp[1] = 1;
dp[2] = -1;
for (int i = 1; i <= n; i++) {
dp[i] += dp[i - 1];
dp[i] = (dp[i] % MOD + MOD) % MOD;
for (int j = 0; j < k; j++) {
dp[i + l[j]] += dp[i];
dp[i + r[j] + 1] -= dp[i];
}
}
cout << dp[n] << "\n";
return 0;
}

代码二

线段树,时间复杂度为 $O_{(nlog_nk)}$ 。

#include <bits/stdc++.h>
#define lson i << 1
#define rson i << 1 | 1
#define mid ((l + r) >> 1)
using namespace std;
constexpr int N = 2e5 + 100;
constexpr int MOD = 998244353;
long long sum[N << 2], lazy[N << 2];
void build(int i, int l, int r) {
if (l == r) {
sum[i] = 0;
return;
}
build(lson, l, mid);
build(rson, mid + 1, r);
sum[i] = sum[lson] + sum[rson];
}
void push_down_lazy(int i, int l, int r) {
if (lazy[i] != 0) {
(sum[lson] += lazy[i] * (mid - l + 1)) %= MOD;
(sum[rson] += lazy[i] * (r - mid)) %= MOD;
(lazy[lson] += lazy[i]) %= MOD;
(lazy[rson] += lazy[i]) %= MOD;
lazy[i] = 0;
}
}
void update(int i, int l, int r, int L, int R, int val) {
if (L <= l and r <= R) {
(sum[i] += 1LL * val * (r - l + 1) % MOD) %= MOD;
(lazy[i] += val) %= MOD;
return;
}
push_down_lazy(i, l, r);
if (L <= mid) update(lson, l, mid, L, R, val);
if (R > mid) update(rson, mid + 1, r, L, R, val);
sum[i] = (sum[lson] + sum[rson]) % MOD;
}
long long query(int i, int l, int r, int L, int R) {
if (L <= l and r <= R) {
return sum[i];
}
push_down_lazy(i, l, r);
long long res = 0;
if (L <= mid) res += query(lson, l, mid, L, R);
if (R > mid) res += query(rson, mid + 1, r, L, R);
return res % MOD;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> l(k), r(k);
for (int i = 0; i < k; i++)
cin >> l[i] >> r[i];
build(1, 1, n);
update(1, 1, n, 1, 1, 1);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < k; j++) {
if (i + l[j] <= n) {
update(1, 1, n, i + l[j], min(n, i + r[j]), query(1, 1, n, i, i));
}
}
}
cout << query(1, 1, n, n, n) << "\n";
return 0;
}

E - Sequence Sum

题意

$a_1 = x,\ a_{n+1} = a_n^2\ %\ m$,计算 $\displaystyle{\sum_{i=1}^n a_i}$ 。

题解

找出循环节的起点和终点即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n, x, m;
cin >> n >> x >> m;
const int N = min(n + 1, m + 2);
vector<int> a(N);
vector<int> vis(m);
a[1] = x;
vis[x] = 1;
long long ans = 0;
for (int i = 2; i < N; i++) {
a[i] = a[i - 1] * a[i - 1] % m;
if (vis[a[i]]) {
long long sum1 = accumulate(a.begin(), a.begin() + vis[a[i]], 0LL);
vector<int> cycle;
for (int j = vis[a[i]]; j < i; j++) {
cycle.push_back(a[j]);
}
n -= vis[a[i]] - 1;
long long sum2 = accumulate(cycle.begin(), cycle.end(), 0LL);
long long sum3 = accumulate(cycle.begin(), cycle.begin() + n % cycle.size(), 0LL);
cout << sum1 + sum2 * (n / cycle.size()) + sum3 << "\n";
return 0;
} else {
vis[a[i]] = i;
}
}
ans = accumulate(a.begin(), a.end(), 0LL);
cout << ans << "\n";
return 0;
}

F - Simplified Reversi

题意

有一个 $n \times n$ 的棋盘,棋盘中间 $(n-2) \times (n-2)$ 的方阵中为黑子,棋盘的最右列和最下行为白子。

接下来有 $q$ 次操作:

  • $(1, x)$:在棋盘的第一行的 $x$ 列放置一枚白子,白字与该列最近的白子之间均变为白子
  • $(2, x)$:在棋盘的第一列的 $x$ 行放置一枚白子,白字与该行最近的白子之间均变为白子

问 $q$ 操作之后还有多少个黑子。

题解

更新最左列和最上行的同时存储移动过程中黑子个数固定的列和行。(也可以直接用二维线段树但我不会)

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
long long ans = 1LL * (n - 2) * (n - 2);
vector<int> row(n + 1), col(n + 1);
int pos_row = n, pos_col = n;
for (int i = 0; i < q; i++) {
int op, pos;
cin >> op >> pos;
if (op == 1) {
if (pos < pos_col) {
ans -= pos_row - 2;
while (pos_col > pos) col[pos_col--] = pos_row - 2;
} else {
ans -= col[pos];
}
} else {
if (pos < pos_row) {
ans -= pos_col - 2;
while (pos_row > pos) row[pos_row--] = pos_col - 2;
} else {
ans -= row[pos];
}
}
}
cout << ans << "\n";
return 0;
}

AtCoder Beginner Contest 179的更多相关文章

  1. AtCoder Beginner Contest 179 E - Sequence Sum (模拟)

    题意:\(f(x,m)\)表示\(x\ mod\ m\),\(A_{1}=1\),而\(A_{n+1}=f(A^{2}_{n},M)\),求\(\sum^{n}_{i=1}A_{i}\). 题解:多算 ...

  2. AtCoder Beginner Contest 179 D - Leaping Tak (DP)

    题意:给你一个数字\(n\)和\(k\)个区间,\(S\)表示所有区间的并的集合,你目前在\(1\),每次可以从集合中选择一个数字向右移动,问有多少种方法从\(1\)走到\(n\). 题解:我们从1开 ...

  3. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  4. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  5. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  6. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  7. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  8. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  9. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

随机推荐

  1. NOIP初赛篇——07信息编码表示

    一.基本概念 编码 ​ 计算机要处理的数据除了数值数据以外,还有各类符号.图形.图像和声音等非数值数据.而计算机只能识别两个数字0,1.要使计算机能处理这些信息,首先必须要将各类信息转换成0与1表示的 ...

  2. 使用SharePoint App-Only获得访问权限

    目前在开发SharePoint Online的过程中,主要使用通过Azure AD的方式获得应用的访问权限,但是SharePoint App-Only的方式依旧被保留了.使用这种方式进行CSOM开发比 ...

  3. 【SpringMVC】SpringMVC 异常处理

    SpringMVC 异常处理 文章源码 异常处理思路 系统中异常包括两类:预期异常和运行时异常,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生. Cont ...

  4. 【Dart】语言概述

    // 导入(import) // 导入核心库 //导入外部库 import 'package:test_api/test_api.dart'; // 导入文件 //import 'path/test. ...

  5. oracle坚决不挂2(SQLPLUS基础命令)

    继续复习!!SQLplus基础命令,其实这个应该是第一个要复习的.因为基础,你懂得..要想学会跑,你先得知道该怎么走吧. win+R 输入cmd ,我们开始启动SQLplus sqlplus user ...

  6. Java 基于mail.jar 和 activation.jar 封装的邮件发送工具类

    准备工作 发送邮件需要获得协议和支持! 开启服务 POP3/SMTP 服务 如何开启 POP3/SMTP 服务:https://www.cnblogs.com/pojo/p/14276637.html ...

  7. linux线程库

    linux 提供两个线程库,Linux Threads 和新的原生的POSIX线程库(NPTL),linux threads在某些情况下仍然使用,但现在的发行版已经切换到NPTL,并且大部分应用已经不 ...

  8. 【MySQL】1托2 ab复制 一个主机两个slave操作手册

    所有实验环境全部是新建的,如果不是新建的mysql一定要备份!!! 环境:CentOS release 6.8 x64 master:192.168.25.100 slave1: 192.168.25 ...

  9. CTFHub - Web(三)

    密码口令: 弱口令: 1.随意输入账号密码,抓包, 2.右击,"Send to Intruder",打开选项卡Intruder,点击position,椭圆框处软件已经自动为我们把要 ...

  10. C语言逗号运算符(C语言学习笔记)

    什么是逗号运算符 逗号运算符 逗号运算符是指在C语言中,多个表达式可以用逗号分开,其中用逗号分开的表达式的值分别结算,但整个表达式的值是最后一个表达式的值. 用法 多个变量赋值 原因:"=& ...