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

A - Lucky 7

#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
cout << (to_string(n).find("7") != -1 ? "Yes" : "No");
}

B - FizzBuzz Sum

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ll n; cin >> n;
ll ans = 0;
for (ll i = 1; i <= n; i++) {
if (i%3 && i%5) ans += i;
}
cout << ans;
}

C - Sum of gcd of Tuples (Easy)

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ll k; cin >> k;
ll ans = 0;
for (ll a = 1; a <=k ; a++) {
for (ll b = 1; b <= k; b++) {
for (ll c = 1; c <=k; c++) {
ans += __gcd(__gcd(a,b),c);
}
}
}
cout << ans;
}

D - RGB Triplets

题意

一字符串由 $RBG$ 组成,找出三元组 $(i, j, k)$ 满足:

  • $i<j<k$
  • $s[i]!=s[j]\ \&\&\ s[i]!=s[k]\ \&\&\ s[j]!=s[k]$
  • $j-i\ !=\  k-j$

思路一

存储 $3$ 个字母的位置,枚举 $9$ 种前后情况,二分查找满足 $j>i,k>j$ 的下标。

代码一

#include <bits/stdc++.h>
using ll = long long;
using namespace std; int n; string s;
vector<int> v[3]; inline int idx(int b, int a, int i) { //在 v[b] 中寻找第一个不小于 v[a][i] 的下标
return upper_bound(v[b].begin(), v[b].end(), v[a][i]) - v[b].begin();
} ll cal(int a, int b, int c) {
ll ret = 0;
for (int i = 0; i < v[a].size(); i++) {
for (int j = idx(b, a, i); j < v[b].size(); j++) {
for (int k = idx(c, b, j); k < v[c].size(); k++) {
if (v[b][j] - v[a][i] != v[c][k] - v[b][j]) { //等价于条件中的 j - i != k - j
++ret;
}
}
}
}
return ret;
} int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) { //存储不同字母的位置
if (s[i] == 'R') v[0].push_back(i);
else if (s[i] == 'G') v[1].push_back(i);
else v[2].push_back(i);
}
ll ans = 0;
for (int i = 0; i < 3; i++) { //枚举RGB的前后情况
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if (i != j && i !=k && j != k) {
ans += cal(i, j, k);
}
}
}
}
cout << ans;
}

思路二

求出总的情况数减去不满足条件的情况。

代码二

#include <bits/stdc++.h>
#define c1 s[i]
#define c2 s[i + d]
#define c3 s[i + d + d]
using ll = long long;
using namespace std;
int main() {
int n; cin >> n;
string s; cin >> s;
int a[3] = {};
for (char c : s) {
if (c == 'R') ++a[0];
else if (c == 'G') ++a[1];
else ++a[2];
}
ll ans = 1LL * a[0] * a[1] * a[2];
for (int i = 0; i < n - 2; i++) {
for (int d = 1; i + d + d < n; d++) {
if (c1 != c2 and c1 != c3 and c2 != c3)
--ans;
}
}
cout << ans;
}

E - Sum of gcd of Tuples (Hard)

题意

有 $n$ 个大小为 $1{\sim}k$ 的数,找出所有情况中的 $gcd(a_1,a_2,...,a_n)$ 之和。

思路

设 $gcd_i$ 为 $gcd(a_1,a_2,...,a_n)=i$ 的排列个数,答案即 $\sum_{i=1}^{k}i{\times}gcd_i$。

$k$ 中 $i$ 的倍数有 $\lfloor \frac{k}{i} \rfloor$ 个,所以 $gcd=i$ 的情况共有 ${\lfloor \frac{k}{i} \rfloor}^n$ 种,但这其中有重复的情况,比如当计算 $gcd=2$ 时,同时也会得到 $gcd=4、6、8$ 等等的排列,所以我们需要从 $\frac{k}{2}$ 起反向遍历减去重复的情况。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std; const ll mod = 1e9 + 7; ll fpow(ll a, ll b) { ll ret = 1; while (b) { if (b & 1) ret = ret * a %mod; a = a * a % mod; b >>= 1; } return ret; } int main() {
ll n, k; cin >> n >> k;
ll gcd[k+1] = {};
for (int i = 1; i <= k; i++)
gcd[i] = fpow(k / i, n);
for (int i = k / 2; i >= 1; i--)
for (int j = i + i; j <= k; j += i)
gcd[i] -= gcd[j];
for (int i = 1; i <= k; i++)
gcd[i] = i * gcd[i] % mod;
cout << accumulate(gcd + 1, gcd + 1 + k, 0LL) % mod;
}

F - Select Half

题意

有 $n$ 个数,求取 $\lfloor\frac{n}{2}\rfloor$ 个两两不相邻的数的最大和。

思路

设 $dp[i]$ 为长为 $i$ 时取 $\lfloor\frac{i}{2}\rfloor$ 个数的最大和。

当 $i$ 为奇数时,如果取 $a_i$,$dp_i=dp_{i-2}+a_i$,否则 $dp_i=dp_{i-1}$。

当 $i$ 为偶数时,如果取 $a_i$,$dp_i=dp_{i-2}+a_i$,否则 $dp_i=pre\_sum_{i-1}$。

比如当 $i=4$ 时,如果不取 $a_4$,要想在之前的 $a_1\ a_2\ a_3$ 中取够 $2$ 个数,只能取 $a_1\ a_3$,即从  $a_1$ 起间隔为 $2$ 取数作奇数位的前缀和。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n; cin >> n;
int a[n + 1]; for (int i = 1; i <= n; i++) cin >> a[i]; ll pre_sum[n + 1] = {};
pre_sum[1] = a[1];
for (int i = 3; i <= n; i += 2)
pre_sum[i] = pre_sum[i - 2] + a[i]; ll dp[n + 1] = {};
for (int i = 2; i <= n; i++) {
if (i & 1) {
dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
} else {
dp[i] = max(pre_sum[i - 1], dp[i - 2] + a[i]);
}
}
cout << dp[n];
}

AtCoder Beginner Contest 162的更多相关文章

  1. AtCoder Beginner Contest 100 2018/06/16

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

  2. AtCoder Beginner Contest 052

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

  3. AtCoder Beginner Contest 053 ABCD题

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

  4. AtCoder Beginner Contest 136

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

  5. AtCoder Beginner Contest 137 F

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

  6. AtCoder Beginner Contest 076

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

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

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

  8. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

  9. AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle【暴力】

    AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle 我要崩溃,当时还以为是需要什么离散化的,原来是暴力,特么五层循环....我自己写怎么都 ...

随机推荐

  1. 基础篇-http协议《http 简介、url详解、request》

    目录 一.http 简介 二.url 详解 三.request 1.get 和 post 2.请求方法 3.request 组成 4.请求头 5.get 请求参数 6.post 请求参数 7.post ...

  2. 【Linux】Linux系统dev/目录下的tty

    终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备.tty是Teletype的缩写.Teletype是最早出现的一种终端设备,很象电传打字机(或者说就是),是由Teletyp ...

  3. Pandas的数据分组-aggregate聚合

    在对数据进行分组之后,可以对分组后的数据进行聚合处理统计. agg函数,agg的形参是一个函数会对分组后每列都应用这个函数. import pandas as pd import numpy as n ...

  4. oracle RAC和RACOneNode之间的转换

    Convert RAC TO RACOneNode 1.查看资源状态 [grid@rac01 ~]$ crsctl status res -t 从这里看到,数据库的名字叫racdb 2.查看实例 [o ...

  5. STM32延时函数的四种方法

    单片机编程过程中经常用到延时函数,最常用的莫过于微秒级延时delay_us()和毫秒级delay_ms().本文基于STM32F207介绍4种不同方式实现的延时函数. 1.普通延时 这种延时方式应该是 ...

  6. SAP demo包 示例程序

    在SAP的这个开发类中SABAPDEMOS,存放了N多的demo程序 有空的时候,可以看看.

  7. JAVA获取当前文件路径this.getClass().getResource方法详细讲解

    public class Test { public void run() { // TODO Auto-generated method stub System.out.println(" ...

  8. CentOS 7.2系统安装步骤

    CentOS 7.2系统安装步骤 1.把系统U盘插到服务器上,然后启动服务器进入BIOS界面选择U盘启动. 根据服务器的不同,进入BIOS界面的按钮也不一样,主流的有F10.F11.F12.F2.ES ...

  9. jQuery 多选与清除

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Bitter.Core系列二:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之数据库连接

    Bitter.Core NETCore 相当的简单易用,下面附上使用示例: 数据中连接:请在你的NETCORE 项目中 创建:Bitter.json 配置文件,然后追加如下配置内容: MSSQL 连接 ...