CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) A-D
A
题解
知识点:贪心。
注意到 \(a[1] \neq 1\) , \(1\) 永远不可能换到前面;\(a[1] = 1\) 可以交换后面任意元素。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[20];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
if (a[1] == 1) cout << "YES" << '\n';
else cout << "NO" << '\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
题解
知识点:贪心,枚举。
分两类,一种是纯 \(1\) 或 \(0\) ,另一种是杂合。
显然后者的情况中,把所有数字全选了是最优的;前者枚举一下所有纯子串即可。两种情况,取最大值。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int n;
cin >> n;
string s;
cin >> s;
s = "?" + s;
int cnt0 = 0, cnt1 = 0;
for (int i = 1;i <= n;i++) {
if (s[i] == '0') cnt0++;
else cnt1++;
}
ll mx = 1LL * cnt0 * cnt1;
int i = 1, j = 1;
while (i <= n) {
while (j <= n && s[j] == s[i]) j++;
mx = max(mx, 1LL * (j - i) * (j - i));
i = j;
}
cout << mx << '\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
题解
知识点:构造。
注意到,只有 \(a=b\) 或者 \(a\) 每位都不等于 \(b\) 的对应位才可行。
考虑先把 \(a\) 串的 \(1\) 一个一个消掉,然后发现 \(b\) 会出现全 \(0\) 全 \(1\) 的情况,接下来分类讨论:
- 如果 \(a = b\) ,那么 \(a\) 中 \(1\) 为偶数时得到的 \(b\) 是 \(0\) ,否则是 \(1\) 。
- 如果 \(a\) 每位都不等于 \(b\) 的对应位 ,那么消掉一个 \(1\) 以后又会回到情况1,因此和情况 \(1\) 相反。
全是 \(0\) 直接可以结束,全是 \(1\) 可以先把 \([1,n]\) 取反,然后选择 \([1,1],[2,n]\) 即可。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int n;
cin >> n;
string a, b;
cin >> a >> b;
a = "?" + a;
b = "?" + b;
int cnt = 0;
for (int i = 1;i <= n;i++) cnt += a[i] == b[i];
if (cnt != 0 && cnt != n) return 0;
bool flag = cnt == n ? 0 : 1;
vector<pair<int, int>> ans;
for (int i = 1;i <= n;i++) {
if (a[i] == '1') {
ans.push_back({ i, i });
flag ^= 1;
}
}
if (flag) {
ans.push_back({ 1,n });
ans.push_back({ 1,1 });
ans.push_back({ 2,n });
}
cout << "YES" << '\n';
cout << ans.size() << '\n';
for (auto [i, j] : ans) cout << i << ' ' << j << '\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 << "NO" << '\n';
}
return 0;
}
D
题解
知识点:质因数分解,容斥原理,数论。
题目要求我们每个 \(b_i\) 的方案数,然后得到总的方案数。
显然有 \(gcd(a_{i-1},b_i) = a_i\) ,注意到 \(a_i\) 必须是 \(a_{i-1}\) 的因子否则不可能得到答案,因此特判一下 \(a_{i} | a_{i-1}\) 。
于是,我们要找到所有的 \(b_i\) ,满足 \(gcd(\frac{a_{i-1}}{a_i},\frac{b_i}{a_i}) = 1\) 且 \(a_i | b_i\) ,其中 \(\frac{b_i}{a_i} \in [1,\frac{m}{a_i}]\) ,即我们从 \([1,\frac{m}{a_i}]\) 整数中找到和 \(\frac{a_{i-1}}{a_i}\) 互素的个数。
这是一个典型的容斥问题。先对 \(\frac{a_{i-1}}{a_i}\) 分解素因数,得到其素因子种类。我们先计算出区间中包含 \(\frac{a_{i-1}}{a_i}\) 因子的数的个数,注意奇加偶减,然后用总数 \(\frac{m}{a_i}\) 减去个数,即与之互素的数的个数,于是我们就得到了 \(b_i\) 的种类。
遍历每个 \(a_i\) 即可。
时间复杂度 \(O(n(\log a_i + 10\cdot 2^{10}))\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 998244353;
int a[200007];
bool vis[100007];
int prime[100007];
int cnt;
void euler_screen(int n) {
for (int i = 2;i <= n;i++) {
if (!vis[i]) prime[++cnt] = i;
for (int j = 1;j <= cnt && i * prime[j] <= n;j++) {
vis[i * prime[j]] = 1;
if (!(i % prime[j])) break;//如果到了i的最小质因子就不用继续,因为接下去的数x一定能被(i,x)之间的数筛掉
}
}
}///欧拉筛,O(n),每个合数只会被最小质因子筛掉
bool solve() {
int n, m;
cin >> n >> m;
for (int i = 1;i <= n;i++) cin >> a[i];
int ans = 1;
for (int i = 2;i <= n;i++) {
if (a[i - 1] % a[i]) {
ans = 0;
break;
}
int d = a[i - 1] / a[i];//不能出现的因子
int base = m / a[i];//包含a[i]的数个数
vector<int> ft;//对d分解因子种类
for (int j = 1;j <= cnt && prime[j] <= d / prime[j];j++) {
if (d % prime[j] == 0) ft.push_back(prime[j]);
while (d % prime[j] == 0) d /= prime[j];
}
if (d > 1) ft.push_back(d);
int sum = 0;//容斥原理,求[1,base]中没有d中因子的数个数
for (int j = 1; j < (1 << ft.size()); j++) {
int mul = 1, feat = 0;
for (int k = 0; k < ft.size(); k++) {
if (j & (1 << k)) {
mul *= ft[k];
feat++;
}
}
if (feat & 1) sum = (sum + 1LL * base / mul % mod) % mod;
else sum = (sum - 1LL * base / mul % mod + mod) % mod;
}
sum = (base - sum + mod) % mod;
ans = 1LL * ans * sum % mod;
}
cout << ans << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
euler_screen(100007);
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) A-D的更多相关文章
- Codeforces 1023 A.Single Wildcard Pattern Matching-匹配字符 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)
Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter ...
- CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort 暴力枚举,水 1.题意:n*m的数组, ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- Codeforces Round #438 (Div.1+Div.2) 总结
本来兴致勃勃的想乘着这一次上紫,于是很早很早的到了机房 但是好像并没有什么用,反而rating-=47 Codeforces Round #438(Div.1+Div.2) 今天就这样匆匆的总结一下, ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造 [Problem Descripti ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构 [Problem ...
- Codeforces Round #792 (Div. 1 + Div. 2) A-E
Codeforces Round #792 (Div. 1 + Div. 2) A-E A 题目 https://codeforces.com/contest/1684/problem/A 题解 思路 ...
- Codeforces Round #792 (Div. 1 + Div. 2) // C ~ E
比赛链接:Dashboard - Codeforces Round #792 (Div. 1 + Div. 2) - Codeforces C. Column Swapping 题意: 给定一个n*m ...
- 【codeforces】【比赛题解】#868 CF Round #438 (Div.1+Div.2)
这次是Div.1+Div.2,所以有7题. 因为时间较早,而且正好赶上训练,所以机房开黑做. 然而我们都只做了3题.:(. 链接. [A]声控解锁 题意: Arkady的宠物狗Mu-mu有一只手机.它 ...
随机推荐
- C#基础_枚举
一.在学习枚举之前,首先来听听枚举的优点. 1.枚举能够使代码更加清晰,它允许使用描述性的名称表示整数值. 2.枚举使代码更易于维护,有助于确保给变量指定合法的.期望的值. 3.枚举使代码更易输入. ...
- WAF对抗-安全狗(联合查询篇)
WAF对抗-安全狗(联合查询篇) 实验环境 网站安全狗APACHE版V4.0.靶场:dvwa 为了方便对比可以在这个在线靶场申请一个dvwa https://www.vsplate.com/ mysq ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
- ASP.NET Core 6框架揭秘实例演示[34]:缓存整个响应内容
我们利用ASP.NET开发的大部分API都是为了对外提供资源,对于不易变化的资源内容,针对某个维度对其实施缓存可以很好地提供应用的性能.<内存缓存与分布式缓存的使用>介绍的两种缓存框架(本 ...
- KingbaseES V8R6C6备份恢复案例之---单实例sys_baackup.sh备份
案例说明: KingbaseES V8R6C6中sys_backup.sh在通用机单实例环境,默认需要通过securecmdd工具以及kingbase和root用户之间的ssh互信,执行备份初始化(i ...
- KingbaseES 全局索引是否因为DDL操作而变为Unusable ?
前言 Oracle 在对分区做DDL操作时,会使分区全局索引失效,需要加上关键字update global indexes.KingbaseES 同样支持全局索引.那么,如果对分区表进行DDL操作,那 ...
- Zookeeper及基于Zookeeper的分布式锁总结
1. Zookeeper ZooKeeper 内部存储的数据结构 / +-- node1 +-- node2 | +-- sub_node21 -> "I am sub_node21& ...
- Sync包
sync同步包 Mutex互斥锁: 能够保证在同一时间段内仅有一个goroutine持有锁,有且仅有一个goroutine访问共享资源,其他申请锁的goroutine将会被阻塞直到锁被释放.然后重新争 ...
- 003-simonyanVeryDeepConvolutional2015(VGG)
Very Deep Convolutional Networks for Large-Scale Image Recognition #paper 1. paper-info 1.1 Metadata ...
- java~springboot(2022之后)~目录索引
回到占占推荐博客索引 最近写了不过关于java,spring,微服务的相关文章,今天把它整理一下,方便大家学习与参考. java~springboot(2022之前)~目录索引 java~spring ...