/**
题目:hdu6121 Build a tree
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121
题意:n个点标号为0~n-1;节点i的父节点为floor((i-1)/k); 0是根节点。
求这个树的所有节点为根的子树的节点数的异或和。
思路:模拟
可以发现k = min(k,n-1);即:k>=n-1时候结果一样。
然后画图可以发现是一个满k叉树(叶子不一定满)。
然后发现:如果这是一个叶子也满的k叉树,那么直接就可以计算出结果。
当不是叶子满的时候,可以发现它的某些地方是满的。那么想办法递归处理从上到下。
将那些满的取下来计算。剩下的继续递归。
当k=1的时候递归时间超限。
从1到n取异或和可以发现前i的前缀异或和有规律4为一周期。1,+1,0,原数; */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const LL INF = 1e10;
const int mod = 1e9 + ;
const int maxn = 3e5 + ;
map<LL,LL>mp;
void get(LL n,LL k,LL &h,LL &r)
{
LL p = ;
if(n==){
h = , r = ; return ;
}
n -= ;
for(int i = ; ; i++){
if(n==p*k){
h = i, r = ; return ;
}
if(n/k<p){///如果判断n<=k*p,那么可能要考虑取整。
h = i, r = n; return ;
}
/*if(log10(n)<log10(p)+log10(k)){
h = i, r = n;
return ;
}*/
p = p*k;
n -= p;
}
}
LL cal(LL h,LL k)
{
if(h==) return ;
LL p = ;
LL sum = ;
for(int i = ; i <= h; i++){
p *= k;
sum += p;
}
return sum;
}
void work(LL num,LL h,LL k)
{
if(num==) return ;
LL n = cal(h,k);
mp[n] += num;
n -= ;
while(n){
mp[n/k] += num*k;
n /= k;
n -= ;
}
}
LL Pow(LL a,LL b)
{
LL p = ;
while(b){
if(b&) p *= a;
a = a*a;
b >>= ;
}
return p;
}
void solve(LL n,LL k)
{
if(n==){
mp[]++;
return ;
}
LL h, r;
get(n,k,h,r);
if(r==){
work(,h,k); return ;
}
if(h==){
mp[n] += ;
mp[] += n-;
return ;
}
LL p = Pow(k,h-);
LL num;
if(r%p==) num = r/p;
else num = r/p+;
work(num-,h-,k);
work(k-num,h-,k);
mp[n]++;
solve(n-(num-)*cal(h-,k)-(k-num)*cal(h-,k)-,k); }
void test()///k=1时候的规律。
{
for(int i = ; i <= ; i++){
printf("%d: ",i);
int ans = ;
for(int j = ; j <= i; j++){
ans ^= j;
}
printf("%d\n",ans);
}
}
int main()
{
//freopen("YYnoGCD.in","r",stdin);
//freopen("YYnoGCD.out","w",stdout);
//freopen("in.txt","r",stdin);
int T;
//test();
LL n, k;
cin>>T;
while(T--)
{
scanf("%lld%lld",&n,&k);
LL ans = ;
if(k==){
if(n%==) ans = n;
if(n%==) ans = ;
if(n%==) ans = n+;
if(n%==) ans = ;
}else{
k = min(k,n-);
mp.clear();
solve(n,k);
map<LL,LL>::iterator it;
for(it = mp.begin(); it!=mp.end(); it++){
if((it->second)%){
ans ^= it->first;
}
}
}
cout<<ans<<endl;
}
return ;
}

hdu6121 Build a tree 模拟的更多相关文章

  1. hdu6121 Build a tree

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6121 题面: Build a tree Time Limit: 2000/1000 MS (J ...

  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. easyui Tree模拟级联勾选cascadeCheck,节点选择,父节点自动选中,节点取消,父节点自动取消选择,节点选择,所有子节点全部选择,节点取消,所有子节点全部取消勾选

    最近项目中用到easyui tree,发现tree控件的cascadeCheck有些坑,不像miniui 的tree控件,级联勾选符合业务需求,所以就自己重新改写了onCheck事件,符合业务需求.网 ...

  8. 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− ...

  9. 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 ...

随机推荐

  1. 吐槽win7

    在纠结了N次后,终于重装win7了.其间还是不死心,又找了新的xp版本来装了一下,确实不行,才心不甘情不愿地再次回到win7下. 说实话,Win7的各种新功能,我没有感觉到有多好,只有不适应,如: 资 ...

  2. linux 网卡eth0检测时没有IP地址,怎么回事??

    你没有配IP怎么会有,除非你自动获取了,自己动手配置命令: ifconfig eth0 IP地址 netmask 子网掩码自动获取ip地址: dhclient

  3. 使用javascript开发的视差滚动效果的云彩 极客标签 - 做最棒的极客知识分享平台

    www.gbtags.com 使用javascript开发的视差滚动效果的云彩 阅读全文:使用javascript开发的视差滚动效果的云彩 极客标签 - 做最棒的极客知识分享平台

  4. tomcat启动报错,找不到相应的 queue,从而引发内存泄漏

    tomcat启动报错,无法创建 bean listenerStatusChangeDealHandler, no queue 'STOCK.NOTIFY_CHANGE.INTER.CACHE.QUEU ...

  5. Project Euler:Problem 76 Counting summations

    It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...

  6. python发送邮件方法总结

    python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点.     一.相关模块介绍 发送邮件主要用到了smtplib和ema ...

  7. js 替换json对象中的键名

      js 替换json对象中的键名 CreateTime--2018年3月30日15:38:50 Author:Marydon 情景描述: 有个json数组,现在需要将json对象中的key替换掉,值 ...

  8. JDBC 事务(二)回滚到保存点

    public class SavePointTest { /**      * @param args      * @throws SQLException      */     public s ...

  9. 转:发一个自己用过的makefile .

    #gcc test.cpp -L. -Wl,-Bdynamic -ltestlib -Wl,-Bstatic -ltestlib  -Wl,-Bdynamic #make clean; make in ...

  10. public static void main(String args[])什么意思?

    public static void main(String[] args) 这绝对不是凭空想出来的,也不是没有道理的死规定,而是java程序执行的需要. jvm在试图运行一个类之前,先检查该类是否包 ...