Hdu6121 Build a tree(2017多校第7场)
来源:互联网 发布:象棋网络直播 编辑:程序博客网 时间:2024/06/01 09:56
Build a tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 556 Accepted Submission(s): 178
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
Source
2017 Multi-University Training Contest - Team 7
————————————————————————————————————
有一棵n个点的有根树,标号为0到n−1,i号点的父亲是⌊ki−1⌋号点,求所有子树大小的异或和。
这是一棵完全k叉树,考虑根的所有孩子,最多只有一个不是满k叉树,对这个孩子进行递归处理即可,剩下的可以直接算出来。k>1时只有log层,直接做就到底就好了,k=1时要特判
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <map>#include <set>#include <algorithm>#include <complex>#include <vector>#include <bitset>#include <stack>#include <queue>#include <unordered_map>#include <functional>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;LL n,k;LL ans;LL llpow(LL x, LL y){ LL ans = 1; while(y >= 1) { if(y & 1) { ans *= x; } x *= x; y >>= 1; } return ans;}void fff(LL x){ if(x < 1) return; ans ^= x; if(k % 2 == 0) { LL nn = x, p = 1, cs = 1; nn--; while(nn - p * k >= 0) p *= k, nn -= p, cs++; if(nn == 0) { return; } LL t = llpow(k, cs - 1); LL tt = nn / t; if(nn % t == 0) { if(tt % 2 != 0) { fff((x - nn - 1) / k + t); fff((x - nn - 1) / k); } } else { if(tt % 2 != 0) { fff((x - nn - 1) / k + t); fff((x - nn - 1) / k + nn % t); } else { fff((x - nn - 1) / k + nn % t); fff((x - nn - 1) / k); } } } else { LL nn = x, p = 1, cs = 1; nn--; while(nn - p * k >= 0) p *= k, nn -= p, cs++; if(nn == 0) { fff((x - 1) / k); return; } LL t = llpow(k, cs - 1); LL tt = nn / t; if(nn % t == 0) { if(tt % 2 != 0) { fff((x - nn - 1) / k + t); } else { fff((x - nn - 1) / k); } } else { if(tt % 2 != 0) { fff((x - nn - 1) / k); fff((x - nn - 1) / k + t); fff((x - nn - 1) / k + nn % t); } else { fff((x - nn - 1) / k + nn % t); } } }}int main(){ int T; scanf("%d", &T); while(T--) { scanf("%lld %lld",&n,&k); if(k==1) { if(n%4==0) printf("%lld\n",n); else if(n%4==1) printf("1\n"); else if(n%4==2) printf("%lld\n",n+1LL); else printf("0\n"); continue; } ans = 0; fff(n); printf("%lld\n", ans); } return 0;}
阅读全文
0 0
- Hdu6121 Build a tree(2017多校第7场)
- HDU6121-Build a tree
- HDU6121 Build a tree(树+异或)
- hdu6121 Build a tree(好题)
- 2017多校联合第7场/hdu 6121 Build a tree(K叉树的异或和)+满K叉树的性质总结
- hdu6121(想法题目)
- hdu6121
- HDU 6121 Build a tree (递归+特判, 2017 Multi-Univ Training Contest 7)
- 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(树 递归 17多校第七场)
- Build A Binary Search Tree
- Build A Binary Search Tree
- HDU 6121 Build a tree
- hdu 6121 Build a tree
- hdu 6121 Build a tree
- JavaScript难点系列(一):内存空间
- 字符串匹配算法
- Java:Object类详解
- 【Python】安装python包时遇到"error: Microsoft Visual C++ 9.0 is required"问题的解决方法
- Android开发——JVM、Dalvik以及ART的区别
- Hdu6121 Build a tree(2017多校第7场)
- 网易面经-基础知识
- 评估产品机会+产品评审团------《启示录》
- Hive安装并使用MySQL存储元数据
- 运营小游戏分类或角度
- SQL注入学习笔记之SQL盲注
- cookie 存菜单的展开状态
- .netcore开发
- log4j.properties打印mybatis 日志