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的更多相关文章

  1. 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 < ...

  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 ...

  3. Educational Codeforces Round 45 (Rated for Div. 2) E - Post Lamps

    E - Post Lamps 思路:一开始看错题,以为一个地方不能重复覆盖,我一想值这不是sb题吗,直接每个power check一下就好....复杂度nlogn 然后发现不是,这样的话,对于每个po ...

  4. Educational Codeforces Round 45 (Rated for Div. 2) F - Flow Control

    F - Flow Control 给你一个有向图,要求你给每条边设置流量,使得所有点的流量符合题目给出的要求. 思路:只有在所有点的流量和为0时有解,因为增加一条边的值不会改变所有点的总流量和, 所以 ...

  5. Educational Codeforces Round 45 (Rated for Div. 2)

    A bracket sequence is a string containing only characters "(" and ")". A regular ...

  6. Educational Codeforces Round 58 (Rated for Div. 2) G 线性基

    https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...

  7. 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 ...

  8. 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即可.(感 ...

  9. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

随机推荐

  1. N制和PAL制区别

  2. vue ESLint自动修复

    在package.json文件中的"lint"里面加入--fix 然后终端里输入:npm run lint 1. vue用命令直接修复ESLint

  3. React.js基础入门

    本文主要是针对React的一些demo教程.参考了菜鸟教程中的react教程,做了一些总结.Demo的下载链接是 https://github.com/RealAndMe/react-demo 下面要 ...

  4. 动态规划:POJ No 2385 Apple Catching

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...

  5. Github遇到Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.

    警告:为IP地址192.30.252.128的主机(RSA连接的)持久添加到hosts文件中,那就来添加吧! 解决办法: vim /etc/hosts 添加一行:52.74.223.119 githu ...

  6. maven本地库更新失败

    当我们在项目中遇到有些依赖在第三方仓库特别是maven仓库里面没有的时候我们会怎么办? 答案1.通过私服,上传到公司的一个私服上然后进行下载 答案2.通过本地安装,这样非常方面进行使用,今天我们就采用 ...

  7. [转载]Javascript 同步异步加载详解

    http://handyxuefeng.blog.163.com/blog/static/4545217220131125022640/ 本文总结一下浏览器在 javascript 的加载方式. 关键 ...

  8. golang sql.DB

    数据库 sql.DB连接池需知: sql.DB内置连接池,连接不足时会自动创建新连接,新创建的连接使用sql.Open()时传入的dsn来构造. sql.DBClose时只会关闭连接池中的连接,未归还 ...

  9. C - A Plug for UNIX (又是建图坑)

    题目链接:https://cn.vjudge.net/contest/68128#problem/C 没理解好题意真的麻烦,一上午就这么过去了..... 具体思路:按照 源点 ->插座-> ...

  10. ubuntu16.04 eclipse+pydev 配置

    参考:http://blog.csdn.net/bluish_white/article/details/56509446,http://blog.csdn.net/qing101hua/articl ...