【暴力】hdu6121 Build a tree
给你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的更多相关文章
- hdu6121 Build a tree 模拟
/** 题目:hdu6121 Build a tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:n个点标号为0~n-1:节点i的父节点 ...
- hdu6121 Build a tree
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6121 题面: Build a tree Time Limit: 2000/1000 MS (J ...
- hdu6121 build a tree(树)
题解: 可以考虑每一层结点的子树大小 必定满足下面的情况,即 a,a,a,a,a,a,b,c,c,c,c........ 然后每一层依次往上更新,结果是不变的 一共有logn层,所以依次扫上去,统计结 ...
- 【思维】2017多校训练七 HDU6121 Build a tree
http://acm.hdu.edu.cn/showproblem.php?pid=6121 [题意] 询问n个结点的完全k叉树,所有子树结点个数的异或和是多少 [思路] 一棵完全K叉树,对于树的每一 ...
- HDU 6121 Build a tree(找规律+模拟)
Build a tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- 【hdu6121】 Build a tree 简单数学题
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6121 我好像推得挺久的诶..... 题目大意:给你一棵有$n$个点的树,根节点为$0$,对于其余节点 ...
- 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− ...
- 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 ...
- HDU 6121 Build a tree(完全K叉树)
http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满K叉 ...
随机推荐
- margin 居中
左右auto加个宽度.margin-left: auto; margin-right: auto; width:640px;
- pandas中DataFrame使用
切片选择 #显示第一行数据print(df.head(1)) #显示倒数三行数据 print(df.tail(3)) loc df.loc[row_index,col_index] 注意loc是根 ...
- CentOS7修改默认运行级别
新装了一个虚拟机,图形界面启动太慢,想调整一下按照以前的经验改运行级别,输入: vi /etc/inittab 然后发现跟之前不一样了,在inittab设置不再生效: # inittab is no ...
- sqlmap参数说明
--delay 设置每隔几秒测试一次注入 --safe-url 设置sqlmap要访问的正常url --safe-freq 设置每测试多少条注入语句后才去访问safe-url --code 设置能正常 ...
- devm_xxx机制
前言 devm是内核提供的基础机制,用于方便驱动开发者所分配资源的自动回收.参考内核文档devres.txt.总的来说,就是驱动开发者只需要调用这类接口分配期望的资源,不用关心释放问题.这些资源的释放 ...
- Yii 1.1.17 二、Gii创建后台与后台登录验证
一.用Gii创建后台模块 1.启用gii,在config/main.php 'gii' => array( 'class' => 'system.gii.GiiModule', 'pass ...
- 【hihocoder】sam-3
把Parent Tree拓扑排序下,然后从下往上合并. 具体的看官方题解啦~ #include<bits/stdc++.h> #define N 1000010 using namespa ...
- 【NOIP2016】组合数问题
写着玩玩…… 反正超级sb题. #include<bits/stdc++.h> typedef long long ll; using namespace std; ll c[][],h[ ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
- 高性能网络编程(1)—accept建立连接(待研究)
阿里云博客上一篇感觉还不错的文章,待研究,原文链接如下: http://blog.aliyun.com/673?spm=5176.7114037.1996646101.3.oBgpZQ&pos ...