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,所以 ...
随机推荐
- PAT 乙级 1037 在霍格沃茨找零钱(20)C++版
1037. 在霍格沃茨找零钱(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 如果你是哈利·波特迷,你会知 ...
- 基于nginx和tengine的tcp反向代理,负载均衡 安装和配置
先下载nginx_tcp_proxy_module模块. wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master ...
- Python单例模式的4种实现方法
#-*- encoding=utf-8 -*- print '----------------------方法1--------------------------' #方法1,实现__new__方法 ...
- Hibernate hibernate.cfg.xml配置
数据库连接<required>: <property name="hibernate.connection.driver_class"> com.mysql ...
- Vuex 状态管理模式
Vuex 是一个专为 Vue.js 设计的状态管理模式 vuex解决了组件之间同一状态的共享问题.当我们的应用遇到多个组件共享状态时,会需要: 多个组件依赖于同一状态.传参的方法对于多层嵌套的组件将会 ...
- 基于Linux的Samba开源共享解决方案测试(三)
在极限写场景下,对于网关的网络监控如图: 在极限写场景下,对于网关的网络监控如图: 在极限混合读写场景下,对于网关的网络监控如图: 在极限混合读写场景下,对于客户端的网络监控如图: 双NAS网关100 ...
- socket.io带中文时客户端无法响应
记录坑了自己1个多小时的问题. 情况是: 服务端代码: var a = {username: new Date()}; socket.emit('updatePositionInfo',a); 前端代 ...
- JavaScript语句和异常
知识内容: 1.条件语句(分支语句) 2.循环语句 3.with语句 4.异常处理 5.本节练习 参考资料:<JavaScript高级程序设计> 1.条件语句 JavaScript中的条件 ...
- OpenCL 双调排序 GPU 版
▶ 参考书中的代码,写了 ● 代码,核函数文件包含三中算法 // kernel.cl __kernel void bitonicSort01(__global uint *data, const ui ...
- spring data jpa @query的用法
@Query注解的用法(Spring Data JPA) 参考文章:http://www.tuicool.com/articles/jQJBNv . 一个使用@Query注解的简单例子 @Query( ...