地址: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. Hibernate_day03--课程安排_表之间关系_一对多操作

    Hibernate_day03 上节内容 今天内容 表与表之间关系回顾(重点) Hibernate的一对多操作(重点) 一对多映射配置(重点) 一对多级联操作 一对多级联保存 一对多级联删除 一对多修 ...

  2. JSP状态管理 及 Cookie实例

    HTTP协议的无状态性 无状态是指,当浏览器发送请求给服务器的时候,服务器响应客户端的请求. 但是当同一个浏览器再次发送请求给了服务器的时候,服务器并不知道它就是刚才那个浏览器. 简单地说,就是服务器 ...

  3. 插件—jquery.validate.js

    前言 在学习jquery.validate.js中的一个小案例,只是这个插件的简单使用. 案例代码如下: <head>    <title></title>    ...

  4. PHP 开发环境的搭建和使用02--整合让apache处理php

    PHP5.3.5直接下载解压即可.但是怎样才能让apache处理php呢? 1/  在apache 的conf目录下 的 httpd.conf(用于指定apache的设置)加入如下代码:   Load ...

  5. Ubuntu 16.04 安装 Gnome 桌面环境

    个人博客链接:Ubuntu 16.04 安装 Gnome 桌面环境

  6. 160504、springmvc返回json数据编码格式设定

    <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 --> <bean class="org.springframework.web ...

  7. the age of the TCP connection TCP Slow Start

    w防止网络过载和拥塞 HTTP The Definitive Guide The performance of TCP data transfer also depends on the age of ...

  8. angular(一)

    angularjs第一章•angluarjs介绍·什么是angularjs•AngularJS 是一个为动态WEB应用设计的结构框架,提供给大家一种新的开发应用方式,这种方式可以让你扩展HTML的语法 ...

  9. Zipline Data Bundles

    Data Bundles A data bundle is a collection of pricing data, adjustment data, and an asset database. ...

  10. python基础-第六篇-6.2模块

    python之强大,就是因为它其提供的模块全面,模块的知识点不仅多,而且零散---一个字!错综复杂 没办法,二八原则抓重点咯!只要抓住那些以后常用开发的方法就可以了,哪些是常用的?往下看--找答案~ ...