传送门

注意到只要考虑祖先和后代之间的贡献

发现对于一个节点,他和所有祖先最多产生 $log$ 个不同的 $gcd$

所以每个节点开一个 $vector$ 维护祖先到自己所有不同的 $gcd$ 和这个 $gcd$ 的出现次数即可

之所以可以用 $vector$ 而不用 $set$ 是因为每个节点越祖先的节点下来的 $gcd$ 显然是越小的,存在单调性

直接根据单调性从父亲的 $vector$ 小到大和自己的值 $gcd$ 并加入到自己的 $vector$ 里面,父亲的都加完后最后加入本身的值到 $vector$ 最后面即可

因为每个节点最多有 $log$ 个不同的 $gcd$ ,所以复杂度是对的

总复杂度 $O(n \log (10^{12}))$

当然用 $set$ 多一个 $\log$ 应该也能过

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<vector>
  7. using namespace std;
  8. typedef long long ll;
  9. inline ll read()
  10. {
  11. ll x=,f=; char ch=getchar();
  12. while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
  13. while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
  14. return x*f;
  15. }
  16. const int N=2e5+,mo=1e9+;
  17. int n;
  18. ll val[N];
  19. int fir[N],from[N<<],to[N<<],cntt;
  20. inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
  21. struct dat {
  22. ll d,cnt;
  23. dat (ll _d=,ll _cnt=) { d=_d,cnt=_cnt; }
  24. };
  25. vector <dat> V[N];
  26. inline ll gcd(ll a,ll b) { return b ? gcd(b,a%b) : a; }
  27. void dfs(int x,int fa)
  28. {
  29. if(fa)
  30. for(auto A: V[fa])
  31. {
  32. ll d=gcd(A.d,val[x]);
  33. if(V[x].empty() || V[x].back().d!=d)
  34. V[x].push_back(dat(d,A.cnt));
  35. else V[x].back().cnt+=A.cnt;
  36. }
  37. if(V[x].empty() || V[x].back().d!=val[x])
  38. V[x].push_back(dat(val[x],));
  39. else V[x].back().cnt++;
  40. for(int i=fir[x];i;i=from[i])
  41. if(to[i]!=fa) dfs(to[i],x);
  42. }
  43. int main()
  44. {
  45. n=read(); for(int i=;i<=n;i++) val[i]=read();
  46. int a,b;
  47. for(int i=;i<n;i++)
  48. {
  49. a=read(),b=read();
  50. add(a,b); add(b,a);
  51. }
  52. dfs(,); ll Ans=;
  53. for(int i=;i<=n;i++)
  54. for(auto A: V[i]) Ans=(Ans+A.d*A.cnt)%mo;
  55. printf("%lld\n",Ans);
  56. return ;
  57. }

Codeforces 1229B. Kamil and Making a Stream的更多相关文章

  1. Kamil and Making a Stream

    E. Kamil and Making a Stream 参考:Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上 ...

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

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

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

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

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

  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. 1) 简要题解

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

  8. Codeforces Round #588 (Div. 2)

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

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

随机推荐

  1. HDU 5810 Balls and Boxes ——(数学,概率,方差)

    官方题解看不太懂,参考了一些人的博客以后自己证明如下: 其中D(X)和E(X)的公式如下(参考自百度百科): 其中 p = 1 / m .(这是每一个单独事件发生的概率期望,在这里单独事件指的是一个球 ...

  2. Linux sssd 进程 ldap 客户端配置

    Linux sssd 进程 ldap 客户端配置 标签(空格分隔): ldap authconfig authconfig命令解析:authconfig 面对多计算机的身份管理以及账户信息同步, 其解 ...

  3. js获取当前页面url信息

    <html> <head> <meta charset="utf-8" /> <title></title> <s ...

  4. find命令不递归查询子目录

    [root@dbrg-2 test]# find .  ! -name "." -type d -prune -o -type f -name "*.jpg" ...

  5. 系统芯片 SoC

    SoC的定义多种多样,由于其内涵丰富.应用范围广,很难给出准确定义.一般说来, SoC称为系统级芯片,也有称片上系统,意指它是一个产品,是一个有专用目标的集成电路,其中包含完整系统并有嵌入软件的全部内 ...

  6. Linux远程连接工具 Shell Xshell6 XFtp6 绿色破解免安装版

    百度云下载链接: https://pan.baidu.com/s/1HMkuxv1yaAM1yhtz09zpfQ 关注以下公众号,回复xshell,获取提取码 关注公众号githubcn,免费获取更多 ...

  7. koa 基础(七)错误处理中间件

    1.错误处理中间件 app.js /** * 错误处理中间件 */ // 引入模块 const Koa = require('koa'); const router = require('koa-ro ...

  8. java实现几种常用排序:选择排序

    一.选择排序介绍 选择排序,顾名思义就是用逐个选择的方式来进行排序,逐个选择出数组中的最大(或最小)的元素,直到选择至最后一个元素.此时数组完成了排序. 二.选择排序原理分析 三.选择排序代码实现 / ...

  9. Nginx配置文件详细说明 (转)

    Nginx配置文件详细说明 原文链接:http://www.cnblogs.com/Joans/p/4386556.html 在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收 ...

  10. 调用远程linux服务器shell脚本

    package com.haiyisoft.hyoaPc.ui; import java.io.BufferedReader;import java.io.IOException;import jav ...