地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6121

题面:

Build a tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1240    Accepted Submission(s): 509

Problem Description
HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n−1, and the father of the node labeled i is the node labeled ⌊i−1k⌋. HazelFan wonders the size of every subtree, and you just need to tell him the XOR value of these answers.
 
Input
The first line contains a positive integer T(1≤T≤5), denoting the number of test cases.
For each test case:
A single line contains two positive integers n,k(1≤n,k≤1018).
 
Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.
 
Sample Input
2
5 2
5 3
 
Sample Output
7
6
 
Source
 
思路:
  画了几个图后,会发现除了n-1号节点所在的链上需要特殊计算,其他字数都是满k叉树
  所以递归下去计算即可,但对于1叉树要特判
 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; LL pp[],sum[];
LL go(LL x,LL y)
{
if(x==) return ;
LL cnt=,ret=;
if(y%==) return x;
while()
{
if(cnt==x) return ret;
cnt=cnt*y+;
ret^=cnt;
}
}
LL dfs(LL n,LL k,int d)
{
if(n==) return ;
LL pos=n-,ret;
for(int i=;i<=d-;i++) pos=(pos-)/k;
ret=dfs(n--(pos-)*sum[d-]-(k-pos)*sum[d-],k,d-);
if((pos-)&) ret^=go(sum[d-],k);
if((k-pos)&) ret^=go(sum[d-],k);
return ret^n;
}
int main(void)
{
LL n,k;
int t;cin>>t;
sum[]=pp[]=;
while(t--)
{
cin>>n>>k;
if(k==)
{
LL ans;
if(n%==) ans=n;
else if(n%==) ans=;
else if(n%==) ans=n+;
else if(n%==) ans=;
printf("%lld\n",ans);
continue;
}
int d=;
for(int i=;sum[i-]<n;i++,d++)
pp[i]=pp[i-]*k,sum[i]=sum[i-]+pp[i];
cout<<dfs(n,k,d)<<endl;
}
return ;
}
 

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

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

  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. Mock利器:PowerMock

    powerMock不仅支持接口mock,final类.静态类.静态方法.私有方法都支持mock,还是很强大的: 1.gradle引用: myonlycompile('org.powermock:pow ...

  2. docker容器跨服务器的迁移

    docker的备份方式有export和save两种. export是当前的状态,针对的是容器,docker save 是针对镜像images. export 找出要备份容器的ID [root@wls1 ...

  3. shell脚本学习总结08--比较与测试

    程序中流程控制是由比较语句和测试语句处理的 算数比较 [ $var -gt 20 ] [ $var -gt 20 -a $var -lt 50 ]         #使用逻辑与 [ $var -ne ...

  4. C语言二维数组

    上节讲解的数组可以看作是一行连续的数据,只有一个下标,称为一维数组.在实际问题中有很多数据是二维的或多维的,因此C语言允许构造多维数组.多维数组元素有多个下标,以确定它在数组中的位置.本节只介绍二维数 ...

  5. springmvc常用注解标签详解(转载)

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  6. MySql在Linux上实现每天自动备份

    Mysql自动备份 创建存放备份sql的文件夹 mkdir /jimisun/mysqlBackup 测试命令行备份数据库 /usr/bin/mysqldump --opt -uroot -pjimi ...

  7. CentOS中制作本地yum源

    1.光盘指向镜像 2.将镜像挂载到某个目录 mkdir /mnt/cdrom mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom 3.修改本机上的YUM源配置文件 ...

  8. 20165330 2017-2018-2 《Java程序设计》第4周学习总结

    课本知识总结 第五章 子类与继承 子类:在类的声明中,通过使用关键字extends来定义一个类的子类. class 子类名 extends 父类名 { ... } 继承:子类继承父类的成员变量作为自己 ...

  9. Android Handler 的使用

    Android UI 操作是线程不安全的.我们只能在UI线程或者说主线程中修改UI.试想多个Thread操作同一个UI,可能引起不一致.UI 线程的主要工作是:UI界面更新显示,各个控件的交互等等.一 ...

  10. 把www.domain.com均衡到本机不同的端口 反向代理 隐藏端口 Nginx做非80端口转发 搭建nginx反向代理用做内网域名转发 location 规则

    负载均衡-Nginx中文文档 http://www.nginx.cn/doc/example/loadbanlance.html 负载均衡 一个简单的负载均衡的示例,把www.domain.com均衡 ...