我亲爱的学姐冒险跑去为我们送正解

但是,,,,

阿龙粗现了!

cao,,

考场期望得分:20   实际得分:20

Problem A. 最近公共祖先 (commonants.c/cpp/pas)

最近公共祖先(Lowest Common Ancestor,LCA)是指在一个树中同时拥有给定的两个点作为后

代的最深的节点。
为了学习最近公共祖先,你得到了一个层数为 n + 1 的满二叉树,其中根节点的深度为 0,其他
节点的深度为父节点的深度 +1 。你需要求出二叉树上所有点对 (i,j),(i,j 可以相等,也可以 i > j)
的最近公共祖先的深度之和对 10 9 + 7 取模后的结果。
Input
一行一个整数 n 。
Output
一行一个整数表示所有点对 (i,j),(i,j 可以相等,也可以 i > j)的最近公共祖先的深度之和对
10 9 + 7 取模后的结果。

Notes
样例 1 解释:

树一共有 7 个节点(一个根节点和两个子节点) ,其中 (4,4),(5,5),(6,6),(7,7) 共 4 对的最近公共
祖先深度为 2,(4,2),(2,4),(5,2),(2,5),(5,4),(4,5),(2,2),(6,3),(3,6),(3,7),(7,3),(6,7),(7,6),(3,3) 共 14 对最
近公共祖先深度是 1 ,其他的点对最近公共祖先深度为 0 ,所以答案为 22 。

思路呢,,大概就是这样的:

算法1:

N<=10

直接暴力求树上两点的LCA

期望得分:20

(大概就是我的那20分吧)

代码~:

 #include<cmath>
#include<cstdio>
#include<iostream>
using namespace std;
int n;
const int mod=1e9+;
int deep[];
long long ans;
void dfs(int i,long long t) {
if(i>n) return;
deep[t]=i;
dfs(i+,t*),dfs(i+,t*+);
}
long long lca(long long i,long long j) {
while() {
if(i==j)break;
if(i<j)swap(i,j);
i/=;
}
return deep[i];
}
long long tot;
int main() {
freopen("commonants.in","r",stdin);
freopen("commonants.out","w",stdout);
cin>>n;
tot=pow(,n+)-;
dfs(,);
for(int i=; i<=tot; i++)
for(int j=; j<=tot; j++) {
int k=lca(i,j);
ans+=k;
ans%=mod;
}
cout<<ans;
fclose stdin;
fclose stdout;
return ;
}

算法2:

代码:没得~

算法三:

借土蛋 的代码一用

 #include <bits/stdc++.h>

 using namespace std;
typedef long long ll;
const ll mod = 1e9 + ;
ll qpow(ll a, ll b){
ll ret = ;
for(; b; b >>= , a = a * a % mod){
if(b & ) ret = ret * a % mod;
}
return ret;
}
ll n;
ll f[];
int main(){
freopen("commonants.in", "r", stdin);
freopen("commonants.out", "w", stdout); cin >> n;
printf("%lld\n", (qpow(, * n + ) - ( * n % mod + ) * qpow(, n) % mod + mod - + mod) % mod);
return ;
}

Problem A. 最近公共祖先 ———2019.10.12的更多相关文章

  1. Problem C. 欧皇 ————2019.10.12

    题目: 再次感激土蛋 #include <bits/stdc++.h> using namespace std; typedef long long ll; ; ll C[][]; voi ...

  2. Problem B. 即时战略 ———2019.10.12

    题目:   代码~:感谢土蛋 #include <iostream> #include <cstring> #include <cmath> #include &l ...

  3. jQuery进阶第三天(2019 10.12)

    一.原生JS快捷的尺寸(属性)(注意这些属性的结果 不带PX单位) clientWidth/clientHeight  =====> 获得元素content+padding的宽/高: offse ...

  4. 题解:T103342 Problem A. 最近公共祖先

    题目链接 题目大意 求每个点对的lca深度的和 以每一层分析,得出通式 由于1e9的数据范围要化简表达式得到O(能过) 瞎搞后就是2^(2n+2)-(4n+2)*2^n-2 code: #includ ...

  5. 【CSP-S膜你考】最近公共祖先 (数学)

    Problem A. 最近公共祖先 (commonants.c/cpp/pas) 注意 Input file: commonants.in Output file: commonants.out Ti ...

  6. 北邮OJ-257- 最近公共祖先-软件14 java

    思路分析:思路应该比较简单也很容易想的来,就是比较两个节点的最近的祖先节点,要对每个节点依次记录下他的所有祖先节点,包括其自己,因为自己也算自己的祖先节点,这一点题目中没有明确指出 所以比较坑. 我们 ...

  7. 清北学堂2019.8.10 & 清北学堂2019.8.11 & 清北学堂2019.8.12

    Day 5 杨思祺(YOUSIKI) 今天的难度逐渐上升,我也没做什么笔记 开始口胡正解 今天的主要内容是最小生成树,树上倍增和树链剖分 最小生成树 Prim 将所有点分为两个集合,已经和点 1 连通 ...

  8. 【JZOJ4888】【NOIP2016提高A组集训第14场11.12】最近公共祖先

    题目描述 YJC最近在学习树的有关知识.今天,他遇到了这么一个概念:最近公共祖先.对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. ...

  9. 「10.19」最长不下降子序列(DP)·完全背包问题(spfa优化DP)·最近公共祖先(线段树+DFS序)

    我又被虐了... A. 最长不下降子序列 考场打的错解,成功调了两个半小时还是没A, 事实上和正解的思路很近了,只是没有想到直接将前$D$个及后$D$个直接提出来 确实当时思路有些紊乱,打的时候只是将 ...

随机推荐

  1. 怎么写自己的CMakeLists.txt--二

    之前写过一篇及其简单的关于CMakeLists.txt的写法,现在重点剖析find_package的用法. 如果程序中使用了外部库,事先并不知道它的头文件和链接库的位置,就要给出头文件和链接库的查找方 ...

  2. HDU 5047 Sawtooth 找规律+拆分乘

      Sawtooth Think about a plane: ● One straight line can divide a plane into two regions. ● Two lines ...

  3. Mysql系列(九)—— 性能分析explain执行计划

    explain是mysql中sql优化的一个重要手段.顾名思义,explain就是解释sql,用于表示sql是怎样执行的信息,即sql执行计划! 语法 explain statement statem ...

  4. .net架构的浅谈

    ,net的架构有以下几种 1.两层架构:UI + 数据层 2.三层架构:UI + 业务层 + 数据层 3.三层 + 接口层 (把相关的业务层抽象成接口,下层来实现接口,中层是依赖) 4.三层 + 接口 ...

  5. [Leetcode] 5279. Subtract the Product and Sum of Digits of an Integer

    class Solution { public int subtractProductAndSum(int n) { int productResult = 1; int sumResult = 0; ...

  6. 关于在linux上部署scrapy的爬虫

    1.在服务器中安装chrome sudo apt-get install libxss1 libappindicator1 libindicator7 wget https://dl.google.c ...

  7. java包装类和值类型的关系

    java包装类总是让人疑惑 它与值类型到底是怎么样一种关系? 本文将以int和Integer为例来探讨它们的关系 java值类型有int short char boolean byte long fl ...

  8. Qt keyevent学习笔记

    在按下一个键不放后,会发生: 1.触发keypressevent(),此时isautorepeat()返回false: 2.set isautorepeat(),使其返回值为true; 3.触发key ...

  9. CreateProcess执行一个控制台程序,隐藏窗口

    STARTUPINFO   StartupInfo;//创建进程所需的信息结构变量 PROCESS_INFORMATION   ProcessInfo; GetStartupInfo(&Sta ...

  10. spring AOP的两种配置

    xml配置 定义要被代理的方法的接口 public interface TestAop { public void print(String s); } 实现上述接口 public class Tes ...