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. Struts2 01---环境搭配

    开发工具:Eclipse   Struts版本:2.3.24  最近在学SSH框架,SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架. ...

  2. 洛谷P1456 Monkey King

    https://www.luogu.org/problemnew/show/1456 #include<cstdio> #include<iostream> #include& ...

  3. NYOJ 数独 DFS

    数独 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一 ...

  4. Visual Studio Code 教程之————入门篇

    Visual Studio代码是一个轻量级但功能强大的源代码编辑器,可在您的桌面上运行,适用于Windows,macOS和Linux.它内置对JavaScript,TypeScript和Node.js ...

  5. laravel二维数组手动分页显示

    示例:数组 $user 如下 $user: array (size=) 'sort' => array (size=) => float 0.028616622341171 => f ...

  6. sklearn_k邻近分类_KNeighborsClassifier

    # coding:utf-8 import numpy as np import matplotlib.pyplot as plt from sklearn.neighbors import KNei ...

  7. 线段树区间更新(set暴力)

    题目链接:https://cn.vjudge.net/contest/66989#problem/I 具体思路:使用栈存储村庄被损坏的顺序,然后set存的是被损坏的村庄,然后每一次查询,直接找到要查询 ...

  8. 跨站请求伪造(CSRF)攻击原理解析:比你所想的更危险

    跨站请求伪造(CSRF)攻击原理解析:比你所想的更危险 跨站请求伪造(Cross-Site Request Forgery)或许是最令人难以理解的一种攻击方式了,但也正因如此,它的危险性也被人们所低估 ...

  9. Spring4笔记9--Spring的事务管理(AOP应用的例子)

    Spring的事务管理: 事务原本是数据库中的概念,在 Dao 层.但一般情况下,需要将事务提升到业务层,即 Service 层.这样做是为了能够使用事务的特性来管理具体的业务.   在 Spring ...

  10. aarch64_l3

    librdmacm-utils-1.1.0-4.fc26.aarch64.rpm 2017-02-12 07:12 87K fedora Mirroring Project libreadline-j ...