给你n,K,让你构造出一颗n个结点的完全K叉树,求所有结点子树大小的异或和。

先把n号结点到根的路径提取出来单独计算。然后这条路径把每一层分成了左右两部分,每一层的左侧和其上一层的右侧的结点的子树大小相同。

就可以容易计算每种大小的子树个数了。

当K等于1时,要单独讨论,答案为1 xor 2 xor ... xor n。这个打个表非常明显。

#include<cstdio>
using namespace std;
typedef long long ll;
ll n,K,pw[105];
int T;
int main(){
// freopen("1002.in","r",stdin);
// freopen("1002.out","w",stdout);
scanf("%d",&T);
for(;T;--T){
scanf("%lld%lld",&n,&K);
if(K==1){
if(n%4ll==0){
printf("%lld\n",n);
}
else if(n%4ll==1ll){
printf("%lld\n",1ll);
}
else if(n%4ll==2ll){
printf("%lld\n",n+1ll);
}
else{
printf("%lld\n",0ll);
}
continue;
}
pw[0]=1;
int j;
for(j=1;pw[j-1]<=n/K;++j){
pw[j]=pw[j-1]*K;
}
--j;
int e=j;
for(int k=1;k<=j;++k){
if(pw[k-1]>n-pw[k]){
e=k-1;
break;
}
pw[k]+=pw[k-1];
}
if(n==pw[e]){
--e;
}
ll i=n-1ll;
ll now=n;
ll nowsiz=1,sumnowsiz=1;
ll ans=1;
ll sheng=((n-pw[e])%K!=0 ? (n-pw[e])%K : K);
int E=e;
while(i!=0){
ll fa=(i-1)/K;
if((i-1ll-fa)%2ll==1ll){
ans^=sumnowsiz;
}
ans^=(sumnowsiz+sheng);
now=pw[e];
--e;
i=fa;
nowsiz*=K;
sumnowsiz+=nowsiz;
sheng=((n-pw[E])%(nowsiz*K)!=0 ? (n-pw[E])%(nowsiz*K) : (nowsiz*K));
}
printf("%lld\n",ans);
}
return 0;
}

【暴力】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. hdu6121 build a tree(树)

    题解: 可以考虑每一层结点的子树大小 必定满足下面的情况,即 a,a,a,a,a,a,b,c,c,c,c........ 然后每一层依次往上更新,结果是不变的 一共有logn层,所以依次扫上去,统计结 ...

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

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

  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. 【hdu6121】 Build a tree 简单数学题

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6121 我好像推得挺久的诶..... 题目大意:给你一棵有$n$个点的树,根节点为$0$,对于其余节点 ...

  7. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

  8. HDU 6121 Build a tree —— 2017 Multi-University Training 7

    HazelFan wants to build a rooted tree. The tree has nn nodes labeled 0 to n−1, and the father of the ...

  9. HDU 6121 Build a tree(完全K叉树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满K叉 ...

随机推荐

  1. 大聊Python----生产消费者模型

    在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题.该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度. 为什么要使用生产者和消费者模式? 在线程世界里,生产者就是生产数 ...

  2. spring-retry 重试机制

    业务场景 应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务逻辑包装给处理方法返回处理结果:第二步拿 ...

  3. python中的Queue模块

    queue介绍 queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue.python3直接queue即可 在python中,多个线程之间的数据 ...

  4. SD卡spi读写流程

    SD卡spi读写流程 1.SD卡的命令格式: SD卡的指令由6字节(Byte)组成,如下: Byte1:0 1 x x x x x x(命令号,由指令标志定义,如CMD39为100111即16进制0x ...

  5. 64_k2

    kf5-libkdepim-akonadi-17.04.1-1.fc26.x86_64.rpm 25-May-2017 12:22 177198 kf5-libkdepim-devel-17.04.1 ...

  6. 比特币编译(Ubuntu 16.04)

    安装比特币需要的所有库 sudo apt-get install build-essential libtool autotools-dev automake pkg-config libssl-de ...

  7. tornado write render redirect IP

    write 用法( self.flush() ) render (跳转指定网页)用法 redirect(跳转指定路由)用法 self.request.remote_ip 显示用户 IP 地址 less ...

  8. 虚拟机 VMware Workstation12 安装OS X 系统

      Windows下虚拟机安装Mac OS X —– VMware Workstation12安装Mac OS X 10.11 本文即将介绍WIN虚拟MAC的教程.完整详细教程(包含安装中的一些问题) ...

  9. 简写代码:当变量为false时['',false,null,undefined,0,NaN]时,返回默认值

    当变量为'',false,null,undefined,0,NaN时,返回默认值 var a='' a || 'hello world'   "hello world" var a ...

  10. python资源合集

    Python 官网: https://www.python.org/ Python2.7 doc: https://docs.python.org/2/ Python Package User Gui ...