HDU 6121 Build a tree (技巧)
来源:互联网 发布:ubuntu usb启动盘制作 编辑:程序博客网 时间:2024/06/15 02:56
Description
HazelFan wants to build a rooted tree. The tree has
n nodes labeled0 ton−1 , and the father of the node labeledi 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
25 25 3
Sample Output
76
题意
有一棵
思路
考虑任意一个高度为
对于根节点的
一定有一部分构成了高度为
通过递推我们可以得出:
cnt[i] ,第i 层的节点个数sum[i] ,高为i 的满k 叉树节点个数xor[i] ,高为i 的满k 叉树所有子树异或和
我们知道,
因此我们设
在满
AC 代码
#include<bits/stdc++.h>typedef __int64 LL;using namespace std;const int maxn =105;LL cnt[maxn]; // 第 i 层的节点个数LL sum[maxn]; // 高为 i 的满 k 叉树节点个数LL xxor[maxn]; // 高为 i 的满 k 叉树所有子树异或和LL n,k;inline LL F(LL a,LL b){ return b&1?a:0;}LL dfs(LL dep,LL add){ if(dep==0)return 0; return (sum[dep] + add)^F(sum[dep],add/cnt[dep])^F(sum[dep-1],k-1-add/cnt[dep])^dfs(dep-1,add%cnt[dep]);}LL solve(){ if(k==1) // 对 1 特判 { int mo = n%4; if(mo==0) return n; else if(mo==1) return 1; else if(mo==2) return n+1; else return 0; } int dep=1; sum[dep]=cnt[dep]=xxor[dep]=1; while((n-sum[dep])/k>=cnt[dep]) // 枚举深度(所有满节点的层) { dep++; cnt[dep] = cnt[dep-1]*k; sum[dep] = sum[dep-1] + cnt[dep]; xxor[dep] = sum[dep]^F(xxor[dep-1],k); } return dfs(dep,n-sum[dep]);}int main(){ int T; cin>>T; while(T--) { LL ans; cin>>n>>k; ans=solve(); cout<<ans<<endl; } return 0;}
- HDU 6121 Build a tree (技巧)
- hdu 6121 Build a tree (模拟)
- hdu 6121 Build a tree (图论)
- HDU 6121 Build a tree(递归)
- HDU 6121 Build a tree
- hdu 6121 Build a tree
- hdu 6121 Build a tree
- HDU 6121Build a tree
- hdu--6121--Build a tree
- hdu--6121:Build a tree
- hdu 6121 Build a tree
- HDU-6121 Build a tree
- hdu 6121 Build a tree
- HDU 6121 Build a tree【】
- HDU 6121 Build a tree [想法题]
- HDU 6121 Build a tree [想法题]
- hdu 6121(Build a tree) 瞎搞+麻烦
- HDU 6121 Build a tree(找规律+implement)
- vuforia模型脱卡功能的实现
- 谷歌三大论文之一-Google File System读书笔记
- 极光推送
- Java 线程池(ThreadPoolExecutor)原理分析与使用
- 高精度算法
- HDU 6121 Build a tree (技巧)
- Project facet Java version 1.8 is not supported解决记录
- 搭建微信小程序服务
- 开源作业调度工具实现开源的Datax、Sqoop、Kettle等ETL工具的作业批量自动化调度
- DBUtils学习----RowProcessor接口与实现
- atomic_add实现分析(mips架构)
- 把数据通过C#写入数据库的模板
- 刷题——Cheapest Palindrome POJ
- ubuntu IPv4 pppoe服务器搭建