题解:

可以考虑每一层结点的子树大小

必定满足下面的情况,即

a,a,a,a,a,a,b,c,c,c,c........

然后每一层依次往上更新,结果是不变的

一共有logn层,所以依次扫上去,统计结果即可

注意到k=1的时候,是一条链,需要特殊处理orz

这个打表可以发现明显的规律,也是二进制的一种容斥吧

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
typedef long long ll;
LL n, k;
LL mypow(LL a, LL b){
if( pow((double)a, b) > 1.5e18) return 1e18;
LL ans = ; for(; b; b >>= ) { if(b&) ans *= a; a *= a; } return ans;
} int main()
{
int T;
cin>>T;
while(T--){
cin>>n>>k;
if(n == ){
cout<<<<endl;
continue;
}
if(k == ){
LL t = n%;
if(t == ) cout<<<<endl;
else if(t == ) cout<<n+<<endl;
else if(t == ) cout<<<<endl;
else cout<<n<<endl;
continue;
}
LL d = , sum = ;
while(sum + mypow(k, d) <= n) sum += mypow(k, d), d++;
if(sum == n){
d--;
LL ans = , sz = ;
while(d != -){
LL N = mypow(k, d);
if(N&) ans ^= sz;
sz = +sz*k;
d--;
}
cout<<ans<<endl;
continue;
}
LL N = n - sum, ans = ;
LL X = N, L = , R = , Xv = , D = d;
while(d){
LL dL = mypow(k, d);
ans ^= Xv;
if((X-)&) ans ^= L;
if((dL-X)&) ans ^= R;
LL Nl = X-;
LL Nx = Nl/k*k + , Nfx = X-Nx;
LL Ny = Nx+k-, Nfy = Ny-X;
Xv = Nfx*L + Nfy*R + Xv + ;
if((double)L*k < 1.5e18) L = +L*k;
R = +R*k;
X = (Nx-)/k + ;
d--;
}
ans ^= Xv;
cout<<ans<<endl;
}
}

hdu6121 build a tree(树)的更多相关文章

  1. hdu6121 Build a tree 模拟

    /** 题目:hdu6121 Build a tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:n个点标号为0~n-1:节点i的父节点 ...

  2. hdu6121 Build a tree

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6121 题面: Build a tree Time Limit: 2000/1000 MS (J ...

  3. 【思维】2017多校训练七 HDU6121 Build a tree

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 [题意] 询问n个结点的完全k叉树,所有子树结点个数的异或和是多少 [思路] 一棵完全K叉树,对于树的每一 ...

  4. 【暴力】hdu6121 Build a tree

    给你n,K,让你构造出一颗n个结点的完全K叉树,求所有结点子树大小的异或和. 先把n号结点到根的路径提取出来单独计算.然后这条路径把每一层分成了左右两部分,每一层的左侧和其上一层的右侧的结点的子树大小 ...

  5. HDU 6121 Build a tree(找规律+模拟)

    Build a tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  6. HDU 5614 Baby Ming and Matrix tree 树链剖分

    题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...

  7. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  8. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  9. Tree( 树) 组件[4]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一.方法列表 //部分方法onClick : funct ...

随机推荐

  1. 使用zxing二维码识别

    1.多二维码识别 (同一张图片中多二维码识别) 直接上代码舒服: pom文件: <!-- QR Code --> <dependency> <groupId>com ...

  2. C++远征之封装篇(下)-学习笔记

    C++远征之封装篇(下) c++封装概述 下半篇依然围绕类 & 对象进行展开 将原本学过的简单元素融合成复杂的新知识点. 对象 + 数据成员 = 对象成员(对象作为数据成员) 对象 + 数组 ...

  3. Windows Server 2012下手动配置IIS的文件夹访问权限

    当新建一个website的时候,一般情况下IIS对相应的物理文件夹的访问权限是不够的. 针对匿名认证(anonymous authentication)需要: 打开文件夹properties-> ...

  4. oracle查询优化,存储过程select表循环插入另一个表,以及索引重建

    查询语句pl/sql中用F5优化语句 ORACLE的explain plan工具的作用只有一个,获取语句的执行计划1.语句本身并不执行,ORACLE根据优化器产生理论上的执行计划2.语句的分析结果存放 ...

  5. 最新cloudera大数据培训班 ccah ccdh 数据分析师 数据科学家

      上海2月21-24日Cloudera Developer training for Spark and Hadoop(CCA-175)北京2月23-26日Cloudera Developer tr ...

  6. 形象的理解Strong和Weak

    Strong Weak

  7. 【廖雪峰老师python教程】——filter/sorted

    filter Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数依次作用于每个元素,然 ...

  8. coreos install megacli

    基于官方的coreos ramdisk安装dell raid管理工具,其版本为debian8 jessie root@c64c7df05677:/# more /etc/apt/sources.lis ...

  9. pandas DataFrame的创建方法

    pandas DataFrame的增删查改总结系列文章: pandas DaFrame的创建方法 pandas DataFrame的查询方法 pandas DataFrame行或列的删除方法 pand ...

  10. 一些排序算法的Python实现

    ''' Created on 2016/12/16 Created by freeol.cn 一些排序算法的Python实现 @author: 拽拽绅士 ''' '''值交换''' def swap( ...