Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting
思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦。
终判被卡了MLE。。。。。 需要每次清空一下子树的map。。。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int> > using namespace std; const int N = 2e5 + ;
const int M = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, a[N];
LL cnt[N];
vector<int> edge[N];
map<int, LL> mp[N]; int gcd(int a, int b) {
return !b ? a : gcd(b, a % b);
} void dfs(int u, int p) {
mp[u][a[u]]++;
for(int v : edge[u]) {
if(v == p) continue;
dfs(v, u);
map<int, LL> :: iterator itu, itv;
for(itu = mp[u].begin(); itu != mp[u].end(); itu++) {
for(itv = mp[v].begin(); itv != mp[v].end(); itv++) {
cnt[gcd(itu -> first, itv -> first)] += itu -> second * itv -> second;
}
} for(itv = mp[v].begin(); itv != mp[v].end(); itv++) {
mp[u][gcd(a[u], itv -> first)] += itv -> second;
}
}
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
cnt[a[i]]++;
} for(int i = ; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
} dfs(, ); for(int i = ; i < N; i++) {
if(cnt[i]) {
printf("%d %lld\n", i, cnt[i]);
}
}
return ;
}
/*
*/
通过代码
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int> > using namespace std; const int N = 2e5 + ;
const int M = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, a[N];
LL cnt[N];
vector<int> edge[N];
map<int, LL> mp[N]; int gcd(int a, int b) {
return !b ? a : gcd(b, a % b);
} void dfs(int u, int p) {
mp[u][a[u]]++;
for(int v : edge[u]) {
if(v == p) continue;
dfs(v, u);
map<int, LL> :: iterator itu, itv;
for(itu = mp[u].begin(); itu != mp[u].end(); itu++) {
for(itv = mp[v].begin(); itv != mp[v].end(); itv++) {
cnt[gcd(itu -> first, itv -> first)] += itu -> second * itv -> second;
}
} for(itv = mp[v].begin(); itv != mp[v].end(); itv++) {
mp[u][gcd(a[u], itv -> first)] += itv -> second;
}
mp[v].clear();
}
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
cnt[a[i]]++;
} for(int i = ; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
} dfs(, ); for(int i = ; i < N; i++) {
if(cnt[i]) {
printf("%d %lld\n", i, cnt[i]);
}
}
return ;
}
/*
*/
Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting的更多相关文章
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 45 (Rated for Div. 2) C、D
C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 ...
- Educational Codeforces Round 45 (Rated for Div. 2) E - Post Lamps
E - Post Lamps 思路:一开始看错题,以为一个地方不能重复覆盖,我一想值这不是sb题吗,直接每个power check一下就好....复杂度nlogn 然后发现不是,这样的话,对于每个po ...
- Educational Codeforces Round 45 (Rated for Div. 2) F - Flow Control
F - Flow Control 给你一个有向图,要求你给每条边设置流量,使得所有点的流量符合题目给出的要求. 思路:只有在所有点的流量和为0时有解,因为增加一条边的值不会改变所有点的总流量和, 所以 ...
- Educational Codeforces Round 45 (Rated for Div. 2)
A bracket sequence is a string containing only characters "(" and ")". A regular ...
- Educational Codeforces Round 58 (Rated for Div. 2) G 线性基
https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...
- Educational Codeforces Round 37 (Rated for Div. 2) G
G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem
题意:给串s,每次询问k个数a,l个数b,问a和b作为后缀的lcp的综合 题解:和bzoj3879类似,反向sam日神仙...lcp就是fail树上的lca.把点抠出来建虚树,然后在上面dp即可.(感 ...
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
随机推荐
- 转:iOS7导航栏遮盖问题的解决
在开发iOS7的界面的时候,有时候你会发现UIViewController里的subView有时会被导航栏遮盖,原因是iOS7鼓励全屏布局,UIViewController中的self.view的坐标 ...
- 2 Kafka Broker
Log的读写.删除流程---日志管理器(log manager)负责创建日志.获取日志.清理日志.所有的日志读写操作都交给具体的日志实例来完成. KafkaServer启动的时候,初始化三个类: Lo ...
- Typora 自定义主题 修改左右间距
打开偏好设置,打开主题文件夹 比如要修改night主题中的间距,编辑night.css文件,修改#write样式即可. 修改其他样式类试.
- scrapy爬取天气数据
看了scrapy,打算构建自己的天气数据,目标源:就是你了,中国天气网! 仔细点两下这个网站,发现可以由各个省.直辖市到省市所属的地级市,再到各县,页面在这: 点开就可以看到中国所有的省.直辖市,但港 ...
- 转载http中302与301的区别
http://blog.csdn.net/qmhball/article/details/7838989 一.官方说法301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于 ...
- ETL测试基本知识
转载自: https://www.cnblogs.com/clarke157/p/6383024.html 一.ETL测试的重要性: ETL(Extract-Transform-Load的缩写,即数据 ...
- C语言实现线性表
#include <stdio.h> #include <stdlib.h> //提供malloc()原型 /* 线性表需要的方法: 1. List MakeEmpty():初 ...
- EOJ Monthly 2019.2 (based on February Selection) F.方差
题目链接: https://acm.ecnu.edu.cn/contest/140/problem/F/ 题目: 思路: 因为方差是用来评估数据的离散程度的,因此最优的m个数一定是排序后连续的,所以我 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 1005 HDU 6154 CaoHaha's staff (找规律)
题目链接 Problem Description "You shall not pass!" After shouted out that,the Force Staff appe ...
- post请求远程url 报错“基础连接已经关闭...Authentication.AuthenticationException...远程证书无效”解决方案
当我们有时用代码编写post请求url远程地址会报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系. ---> System.Security.Authentication.A ...