给你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. B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. js_!和!!的使用

    js中有些特殊的数据(“” 0 null undefined NaN),请求后台返回的数据中往往都有一些这样的数据,需要对这些数据进行过滤. 过滤代码 var a = 0;//0 "&quo ...

  3. eclipse+EGIT+GitHub

    下载EGIT:http://wiki.eclipse.org/EGit/FAQ#Where_can_I_find_older_releases_of_EGit.3F 1.下载eclipse版本对应的E ...

  4. 独立服务器远程重装Linux系统

    独立服务器远程重装Linux系统 http://rashost.com/blog/remote-reinstall-linux-dedicated-server 本文介绍怎样在没有console连接, ...

  5. 【bzoj3545】peaks

    离线一下,动态开点+线段树合并,然后权值线段树上询问kth即可. #include<bits/stdc++.h> ; *; using namespace std; ; inline in ...

  6. mysql分组取前N记录

    http://blog.csdn.net/acmain_chm/article/details/4126306 http://bbs.csdn.net/topics/390958705 1 我只用到了 ...

  7. 动画基础--基于Core Animation(2)

    参考:https://zsisme.gitbooks.io/ios-/content/ 前面的文章动画基础--基于Core Animation(1)提到了图层的基本概念以及可动画参数几何学等知识. 本 ...

  8. [Think] position与anchorPoint关系

    1.简介: 最近在学习动画,所以接触到了CAlayer类. 其中的position与anchorPoint关系一感觉一头雾水,网上有篇文章 彻底理解position和anchorPoint关系  里面 ...

  9. 流程控制--for序列

    In []: list1 = [,,,] In []: for i in list1: ...: print i ...: In []: for i in list1: print i, ...: / ...

  10. 五种WordPress防止垃圾评论方法-过滤垃圾评论提高WP运行效率

    WordPress貌似和垃圾评论是一对“孪生兄弟”,无论在国内还是国外的空间主机上搭建的Wordpress博客,无论Wordpress有多少流量多么低的权重,垃圾评论都会自动找上门来,假如有好几天没有 ...