2017多校联合第7场/hdu 6121 Build a tree(K叉树的异或和)+满K叉树的性质总结
来源:互联网 发布:linux 定时重启 编辑:程序博客网 时间:2024/06/18 01:08
Build a tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 937 Accepted Submission(s): 366
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 integersn,k(1≤n,k≤1018) .
For each test case:
A single line contains two positive integers
Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.
A single line contains a nonnegative integer, denoting the answer.
Sample Input
25 25 3
Sample Output
76
参考题解 http://blog.csdn.NET/hackertom/article/details/77199170
当k=1时,需要特别处理。打表观察一下规律:
int get_ans(int n){ int ans=n; for(int i=1;i<n;i++) ans^=i; return ans;}int main(){ for(int i=1;i<=48;i++) { cout<<get_ans(i)<<' '; if(i%4==0)cout<<endl; } return 0;}运行结果为
1 3 0 4 1 7 0 8 1 11 0 12 1 15 0 16 1 19 0 20 1 23 0 24 1 27 0 28 1 31 0 32 1 35 0 36 1 39 0 40 1 43 0 44 1 47 0 48
可以观察到规律:
if(k==1) { if(n%4==1)cout<<"1"<<endl; else if(n%4==2)cout<<n+1<<endl; else if(n%4==3)cout<<"0"<<endl; else cout<<n<<endl; }
最多只有一个节点不是满K叉树,单独处理这个节点的子树,其余子树的各子树的异或和等于其子树的大小,递归处理这个子树;
#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <string.h>#include <map>#include <set>#include <queue>#include <deque>#include <list>#include <bitset>#include <stack>#include <stdlib.h>#define lowbit(x) (x&-x)#define e exp(1.0)const int mod=1e9+7;const int inf=0x3f3f3f;//ios::sync_with_stdio(false);// auto start = clock();// cout << (clock() - start) / (double)CLOCKS_PER_SEC;typedef long long ll;typedef long long LL;using namespace std;//int get_ans(int n)//{ // int ans=n; //for(int i=1;i<n;i++) // ans^=i; //return ans;//}int main(){ ios::sync_with_stdio(false); //for(int i=1;i<=48;i++)//打表求1-n的异或 //{ // cout<<get_ans(i)<<' '; // if(i%4==0)cout<<endl; //} int T; cin>>T; while(T--) { ll n,k; cin>>n>>k; if(k==1) { if(n%4==1)cout<<"1"<<endl; else if(n%4==2)cout<<n+1<<endl; else if(n%4==3)cout<<"0"<<endl; else cout<<n<<endl; continue; } ll ans=n; while(1) { if(n-1<=k)//depth=1 { if(n%2==0)// xor一个数两次等于不做任何处理 ans^=1; break; } ll x=1,now=1,full=1; while(x>=0 && now>=0 && now+k*x<n) { x*=k; now+=x; full^=now; } ll l=(n-now-1)/x;//不满足k叉树的左边树的数量 ll r=k-l-1; if(k%2==1) { if(l%2==1)ans^=full; if(r%2==1)ans^=(full^now); } else { if(l%2==1)ans^=now; if(r%2==1)ans^=(now-x); } n-=(l*now+r*(now-x)+1); ans^=n; } cout<<ans<<endl; } return 0;}
阅读全文
0 0
- 2017多校联合第7场/hdu 6121 Build a tree(K叉树的异或和)+满K叉树的性质总结
- hdu 6121 Build a tree(K叉树的异或和)
- hdu 6121 Build a tree(k叉树的异或和)
- HDU 6121 K叉树的异或值和
- HDU 6121 Build a tree(树 递归 17多校第七场)
- HDU 3949 XOR (第k小的异或值)
- hdu 3949(线性基模版) 异或和中第k小的数
- HDU 3949 XOR 线形基,异或消元,求一个数组的第k小异或和
- 2017多校联合第一场 1003题 hdu 6035 Colorful Tree 部分对整体的贡献 + 补集思想 + 树分块
- [dfs]多校联合第三场 K Work
- HDU 1325 Is It A Tree 并查集+树的性质
- 2017 icpc广西邀请赛 K.Query on A Tree (hdu 6191)可持久化字典树
- HDU 4632 2013多校联合第4场 A - Palindrome subsequence
- hdu3949 XOR(求所有的异或和的第k小,高斯消元求线性基)
- K叉树的运算
- HDU6121 Build a tree(树+异或)
- 2017杭电多校联赛第三场-Kanade's sum (hdu6058) 求第k大的数的和
- hdu 5423 Rikka with Tree 树的性质
- 关于git的使用
- HTML样式
- 【Spring】DispatcherServlet源码分析
- 定时器
- 针对带有参数的onclick事件,点击无效的问题解决方案
- 2017多校联合第7场/hdu 6121 Build a tree(K叉树的异或和)+满K叉树的性质总结
- 易中天品三国
- 安卓开发前期准备
- java的值传递和引用传递的比较,内存位置。
- mysql无法写入中文
- oracle 分组后外排序
- 全文替换字符
- mysql存储引擎
- 文件读取到链表尾节点不被置空