比赛链接

A

题解

知识点:数学。

注意到 \(n\) 为奇数时,不考虑连续性,一共有 \(\lceil \frac{n}{2} \rceil ^2\) 个格子,接下来证明一定能凑成方块。

从下往上从大到小摆,第 \(1\) 层摆 \(1 \times \lceil \frac{n}{2} \rceil\) 的矩形,第 \(i\geq 2\) 层显然可以成对摆放 \(1 \times \lceil \frac{n-i}{2} \rceil\) 和 \(1\times (\lceil \frac{n}{2} \rceil -\lceil \frac{n-i}{2} \rceil)\) 的矩形。

\(n\) 为偶数时,总数最多构成 \(\lceil \frac{n}{2} \rceil ^2\) 大小的方形,和奇数情况一样,但会最后多一个最长的矩形。

时间复杂度 \(O(1)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n;
cin >> n;
cout << (n + 1) / 2 << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

B

题解

知识点:枚举。

显然根据鸽巢原理,合法的串长度不会超过 \(100\) ,对每位向前枚举 \(100\) 位即可。

时间复杂度 \(O(100\cdot n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int a[100007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
char ch;
cin >> ch;
a[i] = ch - '0';
}
ll ans = 0;
for (int i = 1;i <= n;i++) {
vector<int> cnt(10);
int vis = 0, mx = 0;
for (int j = i;j >= 1 && i - j + 1 <= 100;j--) {
if (cnt[a[j]] == 0) vis++;
cnt[a[j]]++;
mx = max(mx, cnt[a[j]]);
if (mx <= vis) ans++;
}
}
cout << ans << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

C

题解

知识点:贪心,枚举。

一个 \(0\) 能被修改成任意数字,它能影响到自己到下一个 \(0\) 之前的所有前缀和的贡献(下一个 \(0\) 能继续调整,所以不纳入这个 \(0\) )。

我们统计两个 \(0\) 中间(包括左边的 \(0\) ,但不包括右边的)前缀和种类,然后把 \(0\) 调整成能将最多数量的一种前缀和变为 \(0\) 即可。

从后往前枚举,最左边一段左侧没有 \(0\) 因此只有前缀和为 \(0\) 的才有贡献。

时间复杂度 \(O(n \log n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int a[200007];
ll sum[200007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i], sum[i] = sum[i - 1] + a[i];
int ans = 0, mx = 0;
map<ll, int> mp;
for (int i = n;i >= 1;i--) {
mp[sum[i]]++;
mx = max(mp[sum[i]], mx);
if (a[i] == 0) {
ans += mx;
mx = 0;
mp.clear();
}
}
ans += mp[0];
cout << ans << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

D

题解

方法一

知识点:构造。

首先设 \(d\) 的尾 \(0\) 数为 \(k\) ,如果 \(a\) 或 \(b\) 的尾 \(0\) 数小于 \(k\) ,那么一定无解。因为 \(d\) 的因子包括 \(2^k\) ,而 \(a\) 或 \(b\) 的因子或以后也不会包括 \(2^k\) ,因为尾部有 \(1\) 。

如果有解,我们考虑用 \(x\) 把 \(a\) 和 \(b\) 同步,又要保证能被 \(d\) 整除。因此我们可以从第 \(k\) 位开始到第 \(29\) 位,如果 \(x\) 第 \(i\) 位为 \(0\) 则用 \(d\) 的第一个 \(1\) 通过加法填充这位 ,即 \(x + d \cdot 2^{i-k}\) ,这只会影响第 \(i\) 位之后的位,之前的不会影响。

于是我们把 \(a,b\) 前 \(30\) 位同步,于是一定有 \(a|x = b|x = x\) ,且或以后能被 \(d\) 整除 。

时间复杂度 \(O(1)\)

空间复杂度 \(O(1)\)

方法二

知识点:数论,构造。

方法一得到的结论是: \(x\) 前 \(30\) 位除去 \(k\) 个尾 \(0\) 都用 \(d\) 加成 \(1\) 。设后 \(30\) 位为 \(p\) 于是 \(x = p\cdot 2^{30} + (2^{30} - 2^k)\) 。

我们尝试直接求出这个 \(p\) :

\[\begin{aligned}
p\cdot 2^{30} + (2^{30} - 2^k) &\equiv 0 & &\pmod d\\
p\cdot 2^{30-k} + (2^{30-k} - 1) &\equiv 0 & &\pmod{\frac{d}{2^k}}\\
p &\equiv \bigg(\frac{1}{2} \bigg)^{30-k} - 1 & &\pmod{\frac{d}{2^k}}\\
p &\equiv \bigg(\frac{\frac{d}{2^k}+1}{2} \bigg)^{30-k} - 1 + \frac{d}{2^k} & &\pmod{\frac{d}{2^k}}\\
\end{aligned}
\]

时间复杂度 \(O(1)\)

空间复杂度 \(O(1)\)

代码

方法一

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int a, b, d;
cin >> a >> b >> d;
int k = __builtin_ctz(d);
if (k > __builtin_ctz(a) || k > __builtin_ctz(b)) return false;
ll x = 0;
for (int i = k;i < 30;i++) if (!(x & (1 << i))) x += (ll)d << (i - k);
cout << x << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

方法二

#include <bits/stdc++.h>
#define ll long long using namespace std; int qpow(int a, int k, int P) {
int ans = 1;
while (k) {
if (k & 1) ans = 1LL * ans * a % P;
k >>= 1;
a = 1LL * a * a % P;
}
return ans;
} bool solve() {
int a, b, d;
cin >> a >> b >> d;
int k = __builtin_ctz(d);
if (k > __builtin_ctz(a) || k > __builtin_ctz(b)) return false;
d >>= k;
ll x = (qpow((d + 1) / 2, 30 - k, d) - 1 + d) % d * (1LL << 30) + (1 << 30) - (1 << k);
cout << x << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

Codeforces Round #833 (Div. 2) A-D.md的更多相关文章

  1. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  2. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...

  3. Codeforces Round #633 (Div. 2)

    Codeforces Round #633(Div.2) \(A.Filling\ Diamonds\) 答案就是构成的六边形数量+1 //#pragma GCC optimize("O3& ...

  4. 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 ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  7. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  8. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  9. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

随机推荐

  1. 在DispatcherServlet.class中的,获取方法值中获取到的都是arg值

    原先java虚拟机中在编译是不带着方法的形参的名称,但是在jdk8中是可以带了 只需要在Settings中设置以下: -parameters 再把out文件中的production文件删到在查询bui ...

  2. django_day04

    django_day04 路由 from django.conf.urls import url urlpatterns = [ url(正则表达式, views视图,参数,别名), ] 正则表达式 ...

  3. Java开发学习(三十一)----Maven属性与版本管理

    一.属性 1.1 问题分析 如下图所示 你会发现,如果现在想更新Spring的版本,你会发现依然需要更新多个jar包的版本,这样的话还是有可能出现漏改导致程序出问题,而且改起来也是比较麻烦. 问题清楚 ...

  4. 在vm中安装centos7

    步骤: 1.打开VMware Worktation,点击"创建新的虚拟机": 2.一般选择"典型(推荐)",之后下一步. 3.选择"稍后安装操作系统& ...

  5. ProxySQL 全局变量详解

    转载自:https://www.jianshu.com/p/b9d2a09d80e2 全局变量概述 ProxySQL的行为可以通过全局变量来调整.有两种配置方式: 在runtime下,使用admin结 ...

  6. Elastic App Search 入门

    官方文档地址:https://swiftype.com/documentation/app-search/getting-started Elastic App Search 架构图: 它的特点是帮助 ...

  7. 阿里云SLB的http强制转https

    公司的要求:要求强制http转https 我的环境是: 域名<--->slb的ip<-->源服务器nginx 具体做法是: 第一步:证书放到slb的https上,通过443端口 ...

  8. Elasticsearch基础但非常有用的功能之一:别名

    文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484454&idx=1&sn=43e95a2 ...

  9. Elasticsearch:理解 mapping 中的 null_value

    转载自:https://elasticstack.blog.csdn.net/article/details/114266732 null 不能被索引或搜索. 当字段设置为 null(或空数组或 所有 ...

  10. C#-13 泛型

    一 泛型 泛型提供了一种更优雅的方式,可以让多个类型共享一组代码.泛型允许我们声明类型参数化的代码,可以用不同的类型进行实例化. 也就是说,我们可以用"类型占位符"来写代码,然后在 ...