地址: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. WinFrom 第三方控件 TeleRik控件

    1.首先从工具-拓展与应用中下载安装  TeleRik WinFroms VsExtensions   TeleRik dll文件     2.工具箱控件  将Telerik控件更新过来 3.新建一个 ...

  2. 给Jquery easyui 的datagrid 每行添加操作链接

    背景 我们都知道Jquery的Easy-UI的datagrid能够加入而且自己定义Toolbar,这样我们选择一行然后选择toolbar的对应button就能够对这行的数据进行操作.但实际项目里我们可 ...

  3. 系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面

    改进_使用ModelDroven方案 @Controller @Scope("prototype") public class RoleAction extends ActionS ...

  4. 指针与C++基本原理

    面向对象编程与传统的过程性编程的区别在于,OOP强调的是在运行阶段(而不是编译阶段)进行决策.运行阶段指的是程序正在运行时,编译阶段指的是编译器将程序组合起来时.运行阶段决策就好比度假时,选择参观那些 ...

  5. JQZoom

    UI采用jQuery插件 习惯网购的朋友都深有体会.大部分皇冠级淘宝卖家都是图片控.京东商城的放大图效果也是吸引消费者的法宝之一.京东商城产品展示页支持多图切换并放大代码,放大功能的核心代码为jQzo ...

  6. Java中break、continue、return语句的使用区别

    break.continue.return之间的区别与联系 在软件开发过程中,逻辑清晰是非常之重要的. 代码的规范也是非常重要的.往往细节决定成败.在编写代码的时候,一定要理解语言的作用以及使用的方法 ...

  7. PAT 甲级 1026 Table Tennis(模拟)

    1026. Table Tennis (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A table ...

  8. mysql 下数据库升级脚本的编写

    1 升级时必须得存储过程 /**/ drop procedure if exists pro_upgrade; DELIMITER // CREATE DEFINER=`root`@`%` PROCE ...

  9. Limits on Table Column Count and Row Size Databases and Tables Table Size 最大行数

    MySQL :: MySQL 8.0 Reference Manual :: C.10.4 Limits on Table Column Count and Row Size https://dev. ...

  10. FW:主流RPC框架

    主流RPC框架  2015年10月27日  zman  RPC 介绍目前在互联网公司比较流行的开源的RPC框架. RPC框架比较   语言 协议 ​服务治理 ​社区 机构 Hessian 多语言 he ...