Codeforces 1229B. Kamil and Making a Stream
注意到只要考虑祖先和后代之间的贡献
发现对于一个节点,他和所有祖先最多产生 $log$ 个不同的 $gcd$
所以每个节点开一个 $vector$ 维护祖先到自己所有不同的 $gcd$ 和这个 $gcd$ 的出现次数即可
之所以可以用 $vector$ 而不用 $set$ 是因为每个节点越祖先的节点下来的 $gcd$ 显然是越小的,存在单调性
直接根据单调性从父亲的 $vector$ 小到大和自己的值 $gcd$ 并加入到自己的 $vector$ 里面,父亲的都加完后最后加入本身的值到 $vector$ 最后面即可
因为每个节点最多有 $log$ 个不同的 $gcd$ ,所以复杂度是对的
总复杂度 $O(n \log (10^{12}))$
当然用 $set$ 多一个 $\log$ 应该也能过
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- #include<cmath>
- #include<vector>
- using namespace std;
- typedef long long ll;
- inline ll read()
- {
- ll x=,f=; char ch=getchar();
- while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
- while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
- return x*f;
- }
- const int N=2e5+,mo=1e9+;
- int n;
- ll val[N];
- int fir[N],from[N<<],to[N<<],cntt;
- inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
- struct dat {
- ll d,cnt;
- dat (ll _d=,ll _cnt=) { d=_d,cnt=_cnt; }
- };
- vector <dat> V[N];
- inline ll gcd(ll a,ll b) { return b ? gcd(b,a%b) : a; }
- void dfs(int x,int fa)
- {
- if(fa)
- for(auto A: V[fa])
- {
- ll d=gcd(A.d,val[x]);
- if(V[x].empty() || V[x].back().d!=d)
- V[x].push_back(dat(d,A.cnt));
- else V[x].back().cnt+=A.cnt;
- }
- if(V[x].empty() || V[x].back().d!=val[x])
- V[x].push_back(dat(val[x],));
- else V[x].back().cnt++;
- for(int i=fir[x];i;i=from[i])
- if(to[i]!=fa) dfs(to[i],x);
- }
- int main()
- {
- n=read(); for(int i=;i<=n;i++) val[i]=read();
- int a,b;
- for(int i=;i<n;i++)
- {
- a=read(),b=read();
- add(a,b); add(b,a);
- }
- dfs(,); ll Ans=;
- for(int i=;i<=n;i++)
- for(auto A: V[i]) Ans=(Ans+A.d*A.cnt)%mo;
- printf("%lld\n",Ans);
- return ;
- }
Codeforces 1229B. Kamil and Making a Stream的更多相关文章
- Kamil and Making a Stream
E. Kamil and Making a Stream 参考:Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上 ...
- E. Kamil and Making a Stream 区间gcd
E. Kamil and Making a Stream 这个题目要用到一个结论,就是区间一个区间长度为n的不同的gcd不会超过logn 个, 其实就是知道这个题目可以暴力就好了. 然后就是对于每一个 ...
- 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 ...
- CF1230 E. Kamil and Making a Stream gcd+暴力
比赛的时候TLE,第二天发现合并方向合并错了~ 改了一下顺序就切了~ 又掉分了,好难过QAQ...... Code: #include <bits/stdc++.h> #define N ...
- 【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 ...
- CF1230E Kamil and Making a Stream
题目大意是求 \(\sum_{v,fa,lca(v,fa)=fa}gcd(v \to fa)\) 容易发现 \(\gcd\) 只会变小,所以根据这玩意是从上到下的,每次暴力一下就可以了,\(\gcd\ ...
- Codeforces Round #588 (Div. 1) 简要题解
1. 1229A Marcin and Training Camp 大意: 给定$n$个对$(a_i,b_i)$, 要求选出一个集合, 使得不存在一个元素好于集合中其他所有元素. 若$a_i$的二进制 ...
- Codeforces Round #588 (Div. 2)
传送门 A. Dawid and Bags of Candies 乱搞. Code #include <bits/stdc++.h> #define MP make_pair #defin ...
- Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和
Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description ...
随机推荐
- HDU 5810 Balls and Boxes ——(数学,概率,方差)
官方题解看不太懂,参考了一些人的博客以后自己证明如下: 其中D(X)和E(X)的公式如下(参考自百度百科): 其中 p = 1 / m .(这是每一个单独事件发生的概率期望,在这里单独事件指的是一个球 ...
- Linux sssd 进程 ldap 客户端配置
Linux sssd 进程 ldap 客户端配置 标签(空格分隔): ldap authconfig authconfig命令解析:authconfig 面对多计算机的身份管理以及账户信息同步, 其解 ...
- js获取当前页面url信息
<html> <head> <meta charset="utf-8" /> <title></title> <s ...
- find命令不递归查询子目录
[root@dbrg-2 test]# find . ! -name "." -type d -prune -o -type f -name "*.jpg" ...
- 系统芯片 SoC
SoC的定义多种多样,由于其内涵丰富.应用范围广,很难给出准确定义.一般说来, SoC称为系统级芯片,也有称片上系统,意指它是一个产品,是一个有专用目标的集成电路,其中包含完整系统并有嵌入软件的全部内 ...
- Linux远程连接工具 Shell Xshell6 XFtp6 绿色破解免安装版
百度云下载链接: https://pan.baidu.com/s/1HMkuxv1yaAM1yhtz09zpfQ 关注以下公众号,回复xshell,获取提取码 关注公众号githubcn,免费获取更多 ...
- koa 基础(七)错误处理中间件
1.错误处理中间件 app.js /** * 错误处理中间件 */ // 引入模块 const Koa = require('koa'); const router = require('koa-ro ...
- java实现几种常用排序:选择排序
一.选择排序介绍 选择排序,顾名思义就是用逐个选择的方式来进行排序,逐个选择出数组中的最大(或最小)的元素,直到选择至最后一个元素.此时数组完成了排序. 二.选择排序原理分析 三.选择排序代码实现 / ...
- Nginx配置文件详细说明 (转)
Nginx配置文件详细说明 原文链接:http://www.cnblogs.com/Joans/p/4386556.html 在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收 ...
- 调用远程linux服务器shell脚本
package com.haiyisoft.hyoaPc.ui; import java.io.BufferedReader;import java.io.IOException;import jav ...