E. Kamil and Making a Stream

参考:Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和

思路:求的就是1~n之间所有最短路的gcd之和。

用一个set来储存每一个结点可能的gcd,另外再用一个三维的map来记录每一个结点的每一个gcd出现的次数。

代码:

// Created by CAD on 2019/9/28.
#include <bits/stdc++.h>
#define ll long long
using namespace std; const int maxn=1e5+5;
vector<int> g[maxn];
map<ll,ll> cnt[maxn];
set<ll> s[maxn];
ll x[maxn];
ll ans=0;
const int mod=1e9+7;
void dfs(int u,int f)
{
for(auto i:g[u])
{
if(i==f) continue;
for(auto v:s[u])
{
ll temp=__gcd(1ll*v,x[i]);
ans=(ans+1ll*temp%mod*cnt[u][v]%mod)%mod;
cnt[i][temp]+=cnt[u][v];
s[i].insert(temp);
}
s[i].insert(x[i]);
ans=(ans+x[i]%mod)%mod;
cnt[i][x[i]]++;
dfs(i,u);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>x[i];
for(int i=1,u,v;i<=n-1;++i)
cin>>u>>v,g[u].push_back(v),g[v].push_back(u);
ans=(ans+x[1])%mod;
s[1].insert(x[1]);
cnt[1][x[1]]=1;
dfs(1,-1);
cout<<ans<<endl;
return 0;
}

Kamil and Making a Stream的更多相关文章

  1. E. Kamil and Making a Stream 区间gcd

    E. Kamil and Making a Stream 这个题目要用到一个结论,就是区间一个区间长度为n的不同的gcd不会超过logn 个, 其实就是知道这个题目可以暴力就好了. 然后就是对于每一个 ...

  2. Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)

    链接: https://codeforces.com/contest/1230/problem/E 题意: Kamil likes streaming the competitive programm ...

  3. CF1230 E. Kamil and Making a Stream gcd+暴力

    比赛的时候TLE,第二天发现合并方向合并错了~ 改了一下顺序就切了~ 又掉分了,好难过QAQ...... Code: #include <bits/stdc++.h> #define N ...

  4. Codeforces 1229B. Kamil and Making a Stream

    传送门 注意到只要考虑祖先和后代之间的贡献 发现对于一个节点,他和所有祖先最多产生 $log$ 个不同的 $gcd$ 所以每个节点开一个 $vector$ 维护祖先到自己所有不同的 $gcd$ 和这个 ...

  5. 【CF1210C】Kamil and Making a Stream(vector,数论,树)

    题意:给定一棵n个点带点权的树,i号点的点定义f(i,j)为i到j路径上所有点的gcd,其中i是j的一个祖先,求所有f(i,j)之和mod1e9+7 2<=n<=1e5,0<=a[i ...

  6. CF1230E Kamil and Making a Stream

    题目大意是求 \(\sum_{v,fa,lca(v,fa)=fa}gcd(v \to fa)\) 容易发现 \(\gcd\) 只会变小,所以根据这玩意是从上到下的,每次暴力一下就可以了,\(\gcd\ ...

  7. Codeforces Round #588 (Div. 2)

    传送门 A. Dawid and Bags of Candies 乱搞. Code #include <bits/stdc++.h> #define MP make_pair #defin ...

  8. Codeforces Round #588 (Div. 1) 简要题解

    1. 1229A Marcin and Training Camp 大意: 给定$n$个对$(a_i,b_i)$, 要求选出一个集合, 使得不存在一个元素好于集合中其他所有元素. 若$a_i$的二进制 ...

  9. SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)

    前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...

随机推荐

  1. .Net Core 3.0 内置依赖注入:举例

    原文:.Net Core 3.0 内置依赖注入:举例 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn ...

  2. Sharepoint 开启App 配置App

    如果没有Enable app,打开app store的时候出出现错误: Sorry, apps are turned off. If you know who runs the server, tel ...

  3. 06 Go语言基本命令

    在命令行执行go命令查看相关的Go语言命令: 以windows为例,在DOS窗口输入go Go is a tool for managing Go source code. Usage: go com ...

  4. vue中使用proxy配置不同端口和ip接口

    问题描述: 使用vue-cli创建的项目,开发地址是localhost:8080,由于后台开发不同的模块,导致每个模块请求的ip和端口号不一致 例如:http://192.168.10.22:8081 ...

  5. 销售订单(SO)-API-给已有的销售订单增加一行

    在已存在的OM订单中增加一物料: PROCEDURE insert_new_so_api(p_return_code OUT VARCHAR2, p_return_msg OUT VARCHAR2) ...

  6. count(*),count(1),count(字段)

    如果null参与聚集运算,则除count(*)之外其它聚集函数都忽略null. 如:    ID     DD     1      e     2    null    select  count( ...

  7. Python中GUI库PyQt5的安装和配置

    在使用Tkinter开发GUI程序时,发现相关文档比较少,开发起来太累.经过综合比较,决定使用PyQt这个库.下面是简单的安装步骤. 1.安装 PyQt5 : pip install PyQt5 -i ...

  8. C#字符串和16进制字符串之间的转换

    将字符串编码成 16进制 字符串表示: using System;using System.Collections.Generic;using System.Linq;using System.Tex ...

  9. Phoenix设置联合主键

    1例如 create table test12(email varchar not null,id integer not null,name varchar not null,age integer ...

  10. zabbix4.2Proxy安装文档

    zabbix4.2Proxy安装文档 目录 zabbix4.2Proxy安装文档    1 一.安装    2 1.前期安装准备    2 2.安装zabbix RPM源    3 2.1下载zabb ...