Educational Codeforces Round 58
D. GCD Counting
题意:
给出n个点的树,每个点有一个权值,找出一条最长的路径使得路径上所有的点的gcd>1
题解:
gcd>1的一定不会有很多。所以暴力搞一下就行,不需要点分治。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map> using namespace std;
const int maxn=2e5+;
int head[maxn],Next[*maxn],to[*maxn];
int a[maxn];
int n,sz;
void init(){
sz=;
memset(head,-,sizeof(head));
}
void add_edge(int a,int b){
++sz;
to[sz]=b;Next[sz]=head[a];head[a]=sz;
}
int gcd(int a,int b){
if(!b)return a;
return gcd(b,a%b);
}
map<int,int>mp[maxn];
int ans;
void dfs(int u,int fa){
for(int i=head[u];i!=-;i=Next[i]){
int v=to[i];
if(v==fa)continue;
dfs(v,u);
map<int,int>::iterator it,it2;
for(it=mp[v].begin();it!=mp[v].end();it++){
int g=gcd((*it).first,a[u]);
if(g<=)continue;
for(it2=mp[u].begin();it2!=mp[u].end();it2++){
int g2=gcd((*it2).first,g);
if(g2<=)continue;
ans=max(ans,(*it).second+(*it2).second+);
}
mp[u][(*it).first]=max(mp[u][(*it).first],(*it).second);
}
}
// printf("%d %d\n",u,ans);
mp[u].clear();
mp[u][a[u]]=;
for(int i=head[u];i!=-;i=Next[i]){
int v=to[i];
if(v==fa)continue;
map<int,int>::iterator it;
for(it=mp[v].begin();it!=mp[v].end();it++){
int g=gcd(a[u],(*it).first);
if(g<=)continue;
mp[u][g]=max(mp[u][g],(*it).second+);
ans=max(ans,(*it).second+);
}
}
} int main(){
scanf("%d",&n);
init();
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>)ans=;
} for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dfs(,);
printf("%d\n",ans);
return ;
}
F. Trucks and Cities
题意:
一条笔直公路上有n个城市,第i个城市在a[i]的位置。有m辆卡车要从一个城市去另一个城市,每一辆卡车有四个属性来描述:s,f,c,r.分别是开始的城市,结束的城市,油耗,可以加油的数量。当卡车到达一个城市的时候就可以加油,每次加油都加满,开始的时候所有的车油都是满的。请你找到最小的V使得所有的卡车都能到达目的地。
题解:
对于一辆从s到t的车,它有k次加油的机会。发现实际上是将s到t的路径以城市为端点最多划分为最大长度最小的k+1段。可以发现这样是最优的。然后就dp+单调队列优化。
代码留坑···
G. (Zero XOR Subset)-less
题意:
给出n个整数a1,a2,...,an。你的任务是将这n个整数分成最多段用下面的方法 1.每个元素都被一段包含 2.每一段都包含至少一个元素 3.每一个非空的段的子集,他们的xor不等于0.
输出最多能分成几段,如果没有合法的分段方法,输出-1.
题解:
当这n个数xor起来如果为0那么肯定是无解的。然后求线性基,线性基的大小r就是答案····
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream> using namespace std;
typedef long long LL;
const int maxn=2e5+;
LL a[maxn];
LL x[];
int n;
int main(){
scanf("%d",&n);
LL sum=;
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
sum^=a[i];
}
if(sum==){
printf("-1\n");
return ;
}
int r=;
for(int i=;i<=n;i++){
for(int j=;j>=;j--){
if(!(a[i]>>j))continue;
if(!x[j]){
x[j]=a[i];
r++;
break;
}
a[i]^=x[j];
}
}
printf("%d\n",r);
return ;
}
Educational Codeforces Round 58的更多相关文章
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理
https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...
- Educational Codeforces Round 58 (Rated for Div. 2) D 树形dp + 数学
https://codeforces.com/contest/1101/problem/D 题意 一颗n个点的树,找出一条gcd>1的最长链,输出长度 题解 容易想到从自底向长转移 因为只需要g ...
- Educational Codeforces Round 58 (Rated for Div. 2) G 线性基
https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...
- Educational Codeforces Round 58 A,B,C,D,E,G
A. Minimum Integer 链接:http://codeforces.com/contest/1101/problem/A 代码: #include<bits/stdc++.h> ...
- Educational Codeforces Round 58 Div. 2 自闭记
明明多个几秒就能场上AK了.自闭. A:签到. #include<iostream> #include<cstdio> #include<cmath> #inclu ...
- Educational Codeforces Round 58 Solution
A. Minimum Integer 签到. #include <bits/stdc++.h> using namespace std; #define ll long long ll l ...
- Educational Codeforces Round 58 (Rated for Div. 2)
A. Minimum Integer 水 #include<bits/stdc++.h> #define clr(a,b) memset(a,b,sizeof(a)) using name ...
- Educational Codeforces Round 58 (Rated for Div. 2) (前两题题解)
感慨 这次比较昏迷最近算法有点飘,都在玩pygame...做出第一题让人hack了,第二题还昏迷想错了 A Minimum Integer(数学) 水题,上来就能做出来但是让人hack成了tle,所以 ...
随机推荐
- python-appium520-2初步使用
1.录制自动化脚本 场景:启动雪球,点击我的,登陆雪球,选择手机及其他登陆,输入手机号 2.Appium客户端 客户端介绍:https://github.com/appium/appium/blob/ ...
- [UE4]机器人自动寻路
要让机器人能够自动寻路,需要画出自动寻路的范围,可以使用“Nav Mesh Bounds Volume”组件来自定寻路范围 通过“Delay”节点可以实现让AI执行Move To以后停顿1秒,然后继 ...
- volotile关键字的内存可见性及重排序
在理解volotile关键字的作用之前,先粗略解释下内存可见性与指令重排序. 1. 内存可见性 Java内存模型规定,对于多个线程共享的变量,存储在主内存当中,每个线程都有自己独立的工作内存,并且线程 ...
- Python Json序列化与反序列化
在python中,序列化可以理解为:把python的对象编码转换为json格式的字符串,反序列化可以理解为:把json格式字符串解码为python数据对象.在python的标准库中,专门提供了json ...
- java在注解中绑定方法参数的解决方案
我们有这样子的需求,需要记录用户操作某个方法的信息并记录到日志里面,例如,用户在保存和更新任务的时候,我们需要记录下用户的ip,具体是保存还是更新,调用的是哪个方法,保存和更新的任务名称以及操作是否成 ...
- 使border处于边框内
box-sizing需要指定高度,它在这个高度出现,不会增加额外的高度 .box{box-sizing: border-box;height: 64px;}
- Linux 各类设置、配置、使用技巧参考,Linux使用集锦
========== 参考格式 (新增记录时,复制粘贴在下)============= [日期]: <标题> 参考链接ref1: 参考链接ref2: 正文: ========== 参考格式 ...
- UVA-755-排序
奇怪,我怎么还有一个排序题目没过 题意如下: 公司喜欢有难忘的电话号码,一个让电话号码变得难忘的方式是有一个拼读起来难忘的单词,比如,你可以呼叫University of Waterloo通过拨打难忘 ...
- 35. oracle中instr在平台上的转换用法
//INSTR('15,17,29,3,30,4',a.femployee) var instrSql = fun.funHelper.charIndex('a.femployee',"'& ...
- JAVA SpringMVC + FormDate + Vue + file表单 ( 实现 js 单文件和多文件上传 )
JS 部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...