/**
题目: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. openstack horizon CSS 离线 改动

    Openstack horizon 的CSS主要保存在几个文件夹中,各自是horizon/static/dashboard/scss;horizon/openstack_dashboard/stati ...

  2. Java网络爬虫 - 一个简单的爬虫例子

    WikiScraper.java package master.haku.scrape; import org.jsoup.Jsoup; import org.jsoup.nodes.Document ...

  3. android源代码在线阅读

    http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/

  4. SQL server 2008里面通过sys.dm_exec_procedure_stats得到存储过程的执行信息--转

    --转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/05/13/sql-server-2008-sys-dm-exec-procedure-stats. ...

  5. LDAP编辑器 LDAPAdmin

    LDAPAdmin 是一个在 Windows 用来编辑 LDAP 账户信息的管理工具,采用 Delphi 开发.

  6. Android WiFi热点7.1以上版本适配

    代码地址如下:http://www.demodashi.com/demo/13907.html 一.准备工作 开发环境:  jdk1.8  AS(3.0.1) 运行环境:  华为V10(Android ...

  7. 加速nginx: 开启gzip

    nginx 是一个高性能的 Web 服务器,之前也写过一些关于 nginx 的文章.为了提高博客的响应速度,可以从设置 nginx 的 gzip 和缓存这2方面入手.为字体开启 gzip 和缓存能大大 ...

  8. c#:无法将 NULL 转换成“System.DateTime”,因为它是一种值类型(转)

    摘自:http://www.blogjava.net/parable-myth/archive/2010/09/30/333454.html 在C# 2.0里面的数据类型中,分为值类型和引用类型,引用 ...

  9. Ant打包Android代码生成apk文件

    可參考下面,实现一套代码不同渠道的打包 http://blog.csdn.net/liuhe688/article/details/6679879 http://cnn237111.blog.51ct ...

  10. poi读取excel元素

    Java读取excel元素 忽略元数据末尾回到原数据开始处 pom文件设置 <dependency><groupId>org.apache.poi</groupId> ...