[CF1454] Codeforces Round #686 (Div. 3) solution
标签(空格分隔): 经验 题解
时量 : 2h
概括 :
\text{10min t2 (hacked)}\\
\text{30min t3 }\\
\text{over 1h t4(false) }\\
\text{rating down : 30pts}\\
\]
- t1、t2、t3、t4都是一眼题
- 最大的收获是在cf不要随意使用memset(t2 hack)
- t4 错在想当然的把第一个质因子当作最多的了。。。
A
求一个排列\(p_i\),使 \(p_i \ne i\)
\(answer : p_i = i \% n + 1\)
B
找一个数,仅仅出现过一次且最小
$answer : $ 桶子维护即可
C
给出一个序列\(\{a_n\}\)
求一个数\(b\), 两个\(b\)之间删一次,问最小删除次数
\(answer:unique\)一发,求除首尾最小出现个数即可。
D
给一个数\(n\), 求一个序列\(a_i\)
使得\(a_i | a_{i + 1} \&\& \prod\limits_{i = 1} ^ n a_i = n\)
\(answer:\) 找出最多的质因子\(p\)
\(a_{1 \sim n-1}=p,a_n = n / p^{n - 1}\)
E
给定一颗基环树,求树上总简单路径个数(一个路径与它的逆路径只算一次)
\(answer:\)先撸环,对于每个环上点的子树中的路径一定是单一的,其他路径经过环,所以有两个
因此,\(ans=C_n^2 - \sum\limits_{x \in rope} C_{son[x]}^2\)
F
把\({a_n}\)分成三段,第一段的最大值等于第二段的最小值等于第三段的最大值
\(answer:\)因为有三段所以有两个分界点。枚举第一个,二分查找第二个。因为最小值和最大值都有单调性,所以可以二分,具体来说就是如果第二段最小值小了就往右,否则往左,相等时再看第三段最大值,大右小左,没有成立的就输出no。
A
/*************************************************
Copyright: 724033213@qq.com
Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) {
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
il void write(int x) {
char c[33] = {0}, tot = 0;
if(x == 0) {putchar('0'); return;}
while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
while(tot) {putchar(c[tot --]);}
return ;
}
int s, t, n, m;
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
cin >> t;
while(t --) {
n = read();
for(int i = 1; i <= n; i ++) {
printf("%d ", i % n + 1);
}
printf("\n");
}
return 0;
}
B
/*************************************************
Copyright: 724033213@qq.com
Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) {
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
il void write(int x) {
char c[33] = {0}, tot = 0;
if(x == 0) {putchar('0'); return;}
while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
while(tot) {putchar(c[tot --]);}
return ;
}
const int maxn = 2e5 + 5;
int s, t, n, m;
int a[maxn], vis[maxn];
void work() {
n = read(); m = 1e9; s = -1;
for(int i = 1; i <= n; i ++) vis[(a[i] = read())] = 0;
for(int i = 1; i <= n; i ++) vis[a[i]] ++;
for(int i = 1; i <= n; i ++) if(vis[a[i]] == 1 && a[i] < m) m = a[i], s = i;
printf("%d\n", s);
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
t = read();
while(t --) work();
return 0;
}
C
/*************************************************
Copyright: 724033213@qq.com
Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) {
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
il void write(int x) {
char c[33] = {0}, tot = 0;
if(x == 0) {putchar('0'); return;}
while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
while(tot) {putchar(c[tot --]);}
return ;
}
const int maxn = 2e5 + 5;
int s, t, n, m, a[maxn], box[maxn];
void work() {
int ans = 1e9;
n = read();
memset(box, 0, sizeof(box));
for(int i = 1; i <= n; i ++) a[i] = read();
n = unique(a + 1, a + 1 + n) - (a + 1);
if(n == 1) {puts("0"); return ;}
for(int i = 2; i < n; i ++) box[a[i]] ++;
for(int i = 1; i <= n; i ++) chkmin(ans, box[a[i]] + 1);
printf("%d\n", ans);
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
t = read();
while(t --) work();
return 0;
}
D
/*************************************************
Copyright: 724033213@qq.com
Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define int long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) {
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
il void write(int x) {
char c[33] = {0}, tot = 0;
if(x == 0) {putchar('0'); return;}
while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
while(tot) {putchar(c[tot --]);}
return ;
}
const int maxn = 2e5 + 5;
int s, t, n, m;
int p[maxn], c[maxn], ans[maxn], top;
int pr[maxn], vis[maxn], isp[maxn], cnt;
void euler(int n) {
for(int i = 2; i <= n; i ++) {
if(vis[i]) continue;
pr[++ cnt] = i;
for(int j = i; j * i <= n; j ++)
vis[i * j] = 1;
}
}
void work() {
int lb = 0;
m = n = read(); top = 1;
memset(c, 0, sizeof(c));
for(int i = 1; pr[i] * pr[i] <= n; i ++) {
if(n % pr[i] == 0) {
while(m % pr[i] == 0) {
c[i] ++;
if(c[i] > c[lb]) lb = i;
m /= pr[i];
}
}
}
if(lb == 0) {
printf("1\n%lld\n", m);
return ;
}
cout << c[lb] << "\n";
for(int i = 1; i < c[lb]; i ++) {
printf("%lld ", pr[lb]); top *= pr[lb];
}
printf("%lld\n", n / top);
return ;
}
signed main() {
t = read();
euler(2e5);
while(t --) work();
return 0;
}
E
/*************************************************
Copyright: 724033213@qq.com
Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) {
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
il void write(int x) {
char c[33] = {0}, tot = 0;
if(x == 0) {putchar('0'); return;}
while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
while(tot) {putchar(c[tot --]);}
return ;
}
int s, t, n, m;
const int maxn = 2e5 + 5;
struct node {
int to, nxt;
} e[maxn << 1];
int head[maxn], tot;
//int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void add(int x, int y) {
e[++ tot] = {y, head[x]}; head[x] = tot;
}
int dep[maxn], num[maxn], scc[maxn], cnt;
int ring;
void rope(int B, int E) {
if(dep[B] < dep[E]) swap(B, E);
for( ; dep[B] > dep[E]; )
scc[B] = 1, B = num[B];
for( ; B ^ E; )
scc[B] = scc[E] = 1, B = num[B], E = num[E];
scc[B] = 1;
ring = 1;
}
void fnd(int x, int fa) {
// cerr << x << "sadads asd\n";
if(ring == 1) return ;
num[x] = fa; dep[x] = dep[fa] + 1;
for(int i = head[x]; i ; i = e[i].nxt) {
int y = e[i].to;
if(y ^ fa) {
// cout << x << "+" << y << " " << num[y] << "\n";
if(dep[y]) {
// cerr << "sda " << x << " " << y << "\n";
rope(x, y);
ring = 1;
return ;
} else {
fnd(y, x);
}
}
}
}
int son[maxn];
void dfs(int x, int fa) {
son[x] = 1;
for(int i = head[x]; i ; i = e[i].nxt) {
int y = e[i].to;
if((y ^ fa) && !scc[y]) {
dfs(y, x);
son[x] += son[y];
}
}
}
void work() {
LL ans = 0; tot = 0; ring = 0;
n = read();
memset(head, 0, sizeof(head));
for(int i = 1, x, y; i <= n; i ++) {
dep[i] = num[i] = scc[i] = 0;
x = read(), y = read();
add(x, y); add(y, x);
}
ans = 1ll * n * (n - 1);
fnd(1, 0);
for(int i = 1; i <= n; i ++) if(scc[i] == 1) dfs(i, 0), ans -= 1ll * son[i] * (son[i] - 1) / 2;
// for(int i = 1; i <= n; i ++) cout << scc[i] << " "; cout << "\n";
printf("%lld\n", ans);
return ;
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
t = read();
while(t --) work();
return 0;
}
F
/*************************************************
Copyright: 724033213@qq.com
Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) {
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
il void write(int x) {
char c[33] = {0}, tot = 0;
if(x == 0) {putchar('0'); return;}
while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
while(tot) {putchar(c[tot --]);}
return ;
}
int s, t, n, m;
const int maxn = 2e5 + 5;
struct node {
int to, nxt;
} e[maxn << 1];
int head[maxn], tot;
//int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void add(int x, int y) {
e[++ tot] = {y, head[x]}; head[x] = tot;
}
int dep[maxn], num[maxn], scc[maxn], cnt;
int ring;
void rope(int B, int E) {
if(dep[B] < dep[E]) swap(B, E);
for( ; dep[B] > dep[E]; )
scc[B] = 1, B = num[B];
for( ; B ^ E; )
scc[B] = scc[E] = 1, B = num[B], E = num[E];
scc[B] = 1;
ring = 1;
}
void fnd(int x, int fa) {
// cerr << x << "sadads asd\n";
if(ring == 1) return ;
num[x] = fa; dep[x] = dep[fa] + 1;
for(int i = head[x]; i ; i = e[i].nxt) {
int y = e[i].to;
if(y ^ fa) {
// cout << x << "+" << y << " " << num[y] << "\n";
if(dep[y]) {
// cerr << "sda " << x << " " << y << "\n";
rope(x, y);
ring = 1;
return ;
} else {
fnd(y, x);
}
}
}
}
int son[maxn];
void dfs(int x, int fa) {
son[x] = 1;
for(int i = head[x]; i ; i = e[i].nxt) {
int y = e[i].to;
if((y ^ fa) && !scc[y]) {
dfs(y, x);
son[x] += son[y];
}
}
}
void work() {
LL ans = 0; tot = 0; ring = 0;
n = read();
memset(head, 0, sizeof(head));
for(int i = 1, x, y; i <= n; i ++) {
dep[i] = num[i] = scc[i] = 0;
x = read(), y = read();
add(x, y); add(y, x);
}
ans = 1ll * n * (n - 1);
fnd(1, 0);
for(int i = 1; i <= n; i ++) if(scc[i] == 1) dfs(i, 0), ans -= 1ll * son[i] * (son[i] - 1) / 2;
// for(int i = 1; i <= n; i ++) cout << scc[i] << " "; cout << "\n";
printf("%lld\n", ans);
return ;
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
t = read();
while(t --) work();
return 0;
}
[CF1454] Codeforces Round #686 (Div. 3) solution的更多相关文章
- Codeforces Round #466 (Div. 2) Solution
从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...
- 老年OIer的Python实践记—— Codeforces Round #555 (Div. 3) solution
对没错下面的代码全部是python 3(除了E的那个multiset) 题目链接:https://codeforces.com/contest/1157 A. Reachable Numbers 按位 ...
- Codeforces Round #545 (Div. 1) Solution
人生第一场Div. 1 结果因为想D想太久不晓得Floyd判环法.C不会拆点.E想了个奇奇怪怪的set+堆+一堆乱七八糟的标记的贼难写的做法滚粗了qwq靠手速上分qwqqq A. Skyscraper ...
- Codeforces Round 500 (Div 2) Solution
从这里开始 题目地址 瞎扯 Problem A Piles With Stones Problem B And Problem C Photo of The Sky Problem D Chemica ...
- Codeforces Round #607 (Div. 1) Solution
从这里开始 比赛目录 我又不太会 div 1 A? 我菜爆了... Problem A Cut and Paste 暴力模拟一下. Code #include <bits/stdc++.h> ...
- Codeforces Round #578 (Div. 2) Solution
Problem A Hotelier 直接模拟即可~~ 复杂度是$O(10 \times n)$ # include<bits/stdc++.h> using namespace std; ...
- Codeforces Round #525 (Div. 2) Solution
A. Ehab and another construction problem Water. #include <bits/stdc++.h> using namespace std; ...
- Codeforces Round #520 (Div. 2) Solution
A. A Prank Solved. 题意: 给出一串数字,每个数字的范围是$[1, 1000]$,并且这个序列是递增的,求最多擦除掉多少个数字,使得别人一看就知道缺的数字是什么. 思路: 显然,如果 ...
- Codeforces Round #523 (Div. 2) Solution
A. Coins Water. #include <bits/stdc++.h> using namespace std; int n, s; int main() { while (sc ...
随机推荐
- short i =1; i=i+1与short i=1; i+=1的区别
很典型的一到JAVA 基础面试题,上次面试遇到的,现在记录一下. short i =1; i=i+1;short i=1;i+=1;这两有什么区别呢 ?对两个容量不一样的数据类型的变量进行算术运算时, ...
- 【2】TensorFlow光速入门-数据预处理(得到数据集)
本文地址:https://www.cnblogs.com/tujia/p/13862351.html 系列文章: [0]TensorFlow光速入门-序 [1]TensorFlow光速入门-tenso ...
- JDK---00Linux上编译openjdk8
Centos 7 编译自定义jdk8 1. 安装所需的依赖 yum install alsa-lib-devel cups-devel libX* gcc gcc-c++ freetype-devel ...
- 想买保时捷的运维李先生学Java性能之 垃圾收集算法
前言 从原来只知道-Xms.-Xmx是设置内存的,到现在稍微理解了一些堆内存等Java虚拟机的一些知识.明白了技术这一个东西还是得要有输入才能实践,原理与实践要相辅相成,后续把JVM的监控好好总结一下 ...
- 彻底搞明白this
this是我们在书写代码时最常用的关键词之一,即使如此,它也是JavaScript最容易被最头疼的关键词.那么this到底是什么呢? 如果你了解执行上下文,那么你就会知道,其实this是执行上下文对象 ...
- windows搭建ftp
控制面板 此时输入电脑用户名和密码可在自己电脑访问,但是其它电脑不能访问 接下来防火墙允许的应用将FTP服务器打钩 控制面板\系统和安全\Windows Defender ...
- [Luogu P2257] YY的GCD (莫比乌斯函数)
题面 传送门:洛咕 Solution 推到自闭,我好菜啊 显然,这题让我们求: \(\large \sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)\in prime]\) 根 ...
- P2868 [USACO07DEC]Sightseeing Cows G
题意描述 Sightseeing Cows G 给定一张有向图,图中每个点都有点权 \(a_i\),每条边都有边权 \(e_i\). 求图中一个环,使 "环上个点权之和" 除以 & ...
- 关于Java中泛型、反射和注解的扫盲篇
泛型 泛型概念 泛型是在JDK1.5之后引入的,旨在让我们写出更加通用化,更加灵活的代码.通用化的手段在于让数据类型变得参数化,定义泛型时,对应的数据类型是不确定的,泛型方法被调用时,会指定具体类 ...
- Python Tkinter小实例——模拟掷骰子
什么是Tkinter? Tkinter 是 Python 的标准 GUI 库.Python 使用 Tkinter 可以快速的创建 GUI 应用程序. 由于 Tkinter 是内置到 python 的安 ...