Codeforces Round #833 (Div. 2) A-D.md
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\) :
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的更多相关文章
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...
- Codeforces Round #633 (Div. 2)
Codeforces Round #633(Div.2) \(A.Filling\ Diamonds\) 答案就是构成的六边形数量+1 //#pragma GCC optimize("O3& ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
随机推荐
- Think PHP框架基础安装6.0
第一步:点击基础安装tp框架composer create-project topthink/think tp 第二步:点击架构多应用模式 拓展composer require topthink/th ...
- JavaScript 设计模式及代码实现——代理模式
代理模式 1 定义 为其他对象提供一种代理以控制对这个对象的访问 在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 2 应用举例 2.1 缓 ...
- mysql_唯一索引数据重复问题总结
CREATE TABLE `tt_transfer_assemble_diffuse_plan_info` ( `id` bigint(20) unsigned NOT NULL AUTO_INCRE ...
- bat查找文件
举例如下 @dir > 1.txt /s /a /b d:\*.mp4 以后再作解释
- Python数据科学手册-机器学习:朴素贝叶斯分类
朴素贝叶斯模型 朴素贝叶斯模型是一组非常简单快速的分类方法,通常适用于维度非常高的数据集.因为运行速度快,可调参数少.是一个快速粗糙的分类基本方案. naive Bayes classifiers 贝 ...
- Django 之路由层
一.Django 请求周期生命流程图 首先,用户在浏览器中输入URL,发送一个GET 或 POST 方法的request 请求. Django 中封装了socket 的WSGI 服务器,监听端口接受这 ...
- ProxySQL 全局变量详解
转载自:https://www.jianshu.com/p/b9d2a09d80e2 全局变量概述 ProxySQL的行为可以通过全局变量来调整.有两种配置方式: 在runtime下,使用admin结 ...
- AlertManager 何时报警
转载自:https://www.qikqiak.com/post/alertmanager-when-alert/ 在使用 Prometheus 进行监控的时候,通过 AlertManager 来进行 ...
- 使用容器运行的minio配置https(TLS)访问
使用certgen生成证书 下载地址:https://github.com/minio/certgen/releases/tag/v0.0.2 下载地址:https://files.cnblogs.c ...
- X-Pack:创建阈值检查警报
简单的事情应该简单(Simple things should be simple),这是Elastic {ON} '17的主题之一,Elastics收到了许多关于使用简单易用的UI创建警报的请求.事实 ...