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\) ...
随机推荐
- 2017 10.25 NOIP模拟赛
期望得分:100+40+100=240 实际得分:50+40+20=110 T1 start取了min没有用,w(゚Д゚)w O(≧口≦)O T3 代码3个bug :数组开小了,一个细节没注意, ...
- 【转】(总结)Nginx配置文件nginx.conf中文详解
本文转载自:http://www.ha97.com/5194.html 定义Nginx运行的用户和用户组 user www www; nginx进程数,建议设置为等于CPU总核心数 worker_pr ...
- Linux学习-1进程
在Linux中,在一个程序的内部启动另外一个程序,从而创建一个新进程. 1.这个工作可以通过库函数system来完成. #include<stdlib.h> int system (con ...
- 20155336 2016-2017-2《JAVA程序设计》第五周学习总结
20155336 2016-2017-2<JAVA程序设计>第五周学习总结 教材学习内容总结 第八章 语法与继承构架 使用try.catch 特点: 使用try.catch语法,JVM会尝 ...
- 远程连接工具PuTTY和MTPuTTY
PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连接软件 官网 http://www.chiark.greenend.org.uk/~sgtatham/putty/ putty ...
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
- POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )
题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...
- sklearn评估模型的方法
一.acc.recall.F1.混淆矩阵.分类综合报告 1.准确率 第一种方式:accuracy_score # 准确率import numpy as np from sklearn.metrics ...
- Sublime遇见中文乱码问题?
今天在写demo的时候,突然发现html页面上的中文在浏览器上显示乱码~!!!!! 这时,我根据网上的提示安装了两个插件:converttoUtf-8,support Gbk ~~~然而,好像无济于事 ...
- 2015.07.15——prime素数
prime素数 1.素数也叫质数,定义是一个数只能被1和它自身整除. 素数从2开始,0,1都不是素数. 2.素数的判断(C++) 3.给定某个数,求小于这个数的所有素数 2.素数的判断(C++) bo ...