给你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. PAT L1-009 N个数求和(运用GCD进行通分)

    题目链接:https://www.patest.cn/contests/gplt/L1-009 题目: 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数“分子/分母”的形式给出的,你 ...

  2. spring cloud config 详解

    Spring Cloud 为开发人员提供了一系列的工具来快速构建分布式系统的通用模型 .例如:配置管理.服务发现.断路由.智能路由.微代理.控制总线.一次性Token.全局锁.决策竞选.分布式sess ...

  3. Webmin LFD to LFI

    Webmin < 1.290 / Usermin < 1.220 - Arbitrary File Disclosure (Perl) https://www.exploit-db.com ...

  4. Android控件——AutoCompleteTextView与MultiAutoCompleteTextView(实现自动匹配输入的内容)

    ------------------------------------AutoCompleteTextView----------------------

  5. perl6 拖库脚本

    #注入点: #https://fei.sg/shop/products.php?action=content&id=-23 #check mysql column_name of the sq ...

  6. python 学习笔记 sqlalchemy

    数据库表是一个二维表,包含多行多列.把一个表的内容用Python的数据结构表示出来的话,可以用一个list表示多行,list的每一个元素是tuple,表示一行记录,比如,包含id和name的user表 ...

  7. centos 挂在ntfs

    Installing build-essentials in CentOS (make, gcc, gdb):http://www.techblogistech.com/2012/03/install ...

  8. sunos kernel src

    https://github.com/eocallaghan/AuroraUX-SunOS https://github.com/zoyanhui/coroutine-libtask https:// ...

  9. Oracle例外定义

    例外名 ORA-XXXXX SQLCODE ACCESS_INTO_NULL ORA-06530 -6530 CASE_NOT_FOUND ORA-06592 -6592 COLLECTION_IS_ ...

  10. sicily 1059. Exocenter of a Trian

    Description Given a triangle ABC, the Extriangles of ABC are constructed as follows: On each side of ...