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,所以 ...
随机推荐
- appium--每次启动会重新安装的问题(没试过)
有人说加这个 最后 大神说 在appium哪里就可以设置了 对,第一个不勾选就不会安装了[经理][Java]大连●Messi_Z(726862194) 15:54:10把这些东西全去掉就好了
- [C#]泛型约束在窗体定义上的使用
查相关资料查的一团乱,自己瞎写了几次以后误打误撞成功了: namespace Test1 { public partial class Form1<T> : Form { // ... F ...
- 关于json_encode()的使用注意
json_encode($json_str,true)在一般情况下可以返回一个数组,但当$json_str的字符编码是GBK或其它时,返回的是一个 空数组,必须用iconv(‘gbk’,‘ut8//I ...
- ECCV 2018 | UBC&腾讯AI Lab提出首个模块化GAN架构,搞定任意图像PS组合
通常的图像转换模型(如 StarGAN.CycleGAN.IcGAN)无法实现同时训练,不同的转换配对也不能组合.在本文中,英属哥伦比亚大学(UBC)与腾讯 AI Lab 共同提出了一种新型的模块化多 ...
- push(),pop(),unshift(),shift()
1.push() 往数组末尾添加一个或多个元素,返回新的长度 2.pop() 删除数组末尾元素,数组长度减1,返回被删除的值 3.unshift() 往数组开头添加一个或多个元素,返回新的长度 4.s ...
- Flutter main future mirotask 的执行顺序
下面这段代码的输出是什么? import 'dart:async'; main() { print('main #1 of 2'); scheduleMicrotask(() => print( ...
- MPI Hello World
▶<并行程序设计导论>第三章(用 MPI 进行分布式内存编程)的第一个程序样例. ● 代码 #include <stdio.h> #include <string.h& ...
- MOCK 基本使用例子
package com.icil.esolution.orders; import static org.springframework.test.web.servlet.request.MockMv ...
- 数据库中查询json 样式的值的sql语句
参考:http://www.lnmp.cn/mysql-57-new-features-json.html 方式一: 可以查到json中的Key:value SELECT * FROM EDI.edi ...
- redis详解(二)-- 数据类型详解
Redis常用数据类型详解 1,Redis最为常用的数据类型主要有以下: String Hash List Set Sorted set pub/sub Transactions 在具体描述这几种数据 ...