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\) ...
随机推荐
- 【官方文档】Nginx负载均衡学习笔记(三) TCP和UDP负载平衡官方参考文档
本章介绍如何使用NGINX Plus和NGINX开放源代理和负载平衡TCP和UDP流量. 目录 介绍 先决条件 配置反向代理 配置TCP或UDP负载平衡被动健康监控 选择负载平衡方法 配置会话持久性 ...
- spring-boot RestTemplate 连接池
以前我们项目都是基于Apache HttpClient 连接池进行web 接口调用,后来用spring-boot, 发现 RestTemplate 挺好用. 简单介绍下: 什么是RestTemplat ...
- Python3中urllib使用与源代码
Py2.x: Urllib库 Urllin2库 Py3.x: Urllib库 变化: 在Pytho2.x中使用import urllib2---对应的,在Python3.x中会使用import url ...
- 【转】线程间操作无效: 从不是创建控件“textBox2” 的线程访问它。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 双击CAD对象,显示自定义对话框实现方法
class TlsApplication : IExtensionApplication { void IExtensionApplication.Initialize() { TTest.Start ...
- Velocity VelocityEngine 支持多种loader 乱码问题
最近升级团队的代码生成工具,此工具是velocity实现的. 之前习惯使用UTF-8编码,现在团队使用GBK. 所以遇到一种场景,模板文件使用UTF-8(习惯了所有任性),输出文件使用GBK(项目需要 ...
- 【FCS NOI2018】福建省冬摸鱼笔记 day2
第二天. 同学还是不带本子记笔记.dalao. 第二天:图论,讲师:@ExfJoe 全程划水,前面都讲水算法[虽然我可能已经忘记了]什么最短路,Tarjan,最小生成树,2SAT,差分约束啥的,我现在 ...
- Centos7安装FTP突然无法登录
vi /etc/pam.d/vsftpd //注释掉auth required pam_shells.so session optional pam_keyinit.so force revokeau ...
- flask基础之蓝图的使用(七)
前言 关于蓝图是什么?或为什么使用蓝图的详细介绍,官方文档讲的很详细,不再赘述.简单来说,在大型的应用中,我们不想视图函数显得杂乱无章,难以维护,将众多的视图函数按照Api的设计规则进行切割是一个好方 ...
- Flask:使用Eclipse+PyDev插件编辑基于package的项目
Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2,Eclipse Oxygen.1a Release (4.7.1a),PyDev 6.3.2 本文记录了 使用Ecli ...