hdu6121 Build a tree 模拟
/**
题目: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 模拟的更多相关文章
- hdu6121 Build a tree
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6121 题面: Build a tree Time Limit: 2000/1000 MS (J ...
- 【暴力】hdu6121 Build a tree
给你n,K,让你构造出一颗n个结点的完全K叉树,求所有结点子树大小的异或和. 先把n号结点到根的路径提取出来单独计算.然后这条路径把每一层分成了左右两部分,每一层的左侧和其上一层的右侧的结点的子树大小 ...
- hdu6121 build a tree(树)
题解: 可以考虑每一层结点的子树大小 必定满足下面的情况,即 a,a,a,a,a,a,b,c,c,c,c........ 然后每一层依次往上更新,结果是不变的 一共有logn层,所以依次扫上去,统计结 ...
- 【思维】2017多校训练七 HDU6121 Build a tree
http://acm.hdu.edu.cn/showproblem.php?pid=6121 [题意] 询问n个结点的完全k叉树,所有子树结点个数的异或和是多少 [思路] 一棵完全K叉树,对于树的每一 ...
- HDU 6121 Build a tree(找规律+模拟)
Build a tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- 【hdu6121】 Build a tree 简单数学题
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6121 我好像推得挺久的诶..... 题目大意:给你一棵有$n$个点的树,根节点为$0$,对于其余节点 ...
- easyui Tree模拟级联勾选cascadeCheck,节点选择,父节点自动选中,节点取消,父节点自动取消选择,节点选择,所有子节点全部选择,节点取消,所有子节点全部取消勾选
最近项目中用到easyui tree,发现tree控件的cascadeCheck有些坑,不像miniui 的tree控件,级联勾选符合业务需求,所以就自己重新改写了onCheck事件,符合业务需求.网 ...
- 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− ...
- 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 ...
随机推荐
- 关于在Visual Studio 2008/2010 和Opencv平台下出现LINK : fatal error
http://blog.sina.com.cn/s/blog_9015f3230101bbef.html 关于在Visual Studio 2008/2010 和Opencv平台下出现LINK : f ...
- (C++)浅谈多态基类析构函数声明为虚函数
主要内容: 1.C++类继承中的构造函数和析构函数 2.C++多态性中的静态绑定和动态绑定 3.C++多态性中析构函数声明为虚函数 1.C++类继承中的构造函数和析构函数 在C++的类继承中, 建立对 ...
- Python网络爬虫 - 一个简单的爬虫例子
下面我们创建一个真正的爬虫例子 爬取我的博客园个人主页首页的推荐文章列表和地址 scrape_home_articles.py from urllib.request import urlopen f ...
- synthesis-of-weak-property-only-allowed-in-arc-or-gc-mode ARC属性
synthesis-of-weak-property-only-allowed-in-arc-or-gc-mode ARC属性 错误提示: 1:确认你的项目是 ARC环境: 2:如果 ARC下出现上面 ...
- Reimplementing event handler
Events in PyQt4 are processed often by reimplementing event handlers. #!/usr/bin/python # -*- coding ...
- linux(Centos)下编译安装gcc4.8.2
欢迎转载.转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46811159 近期要用到c++ 11,所以没办法仅仅得把那台ce ...
- CentOS 6.3下Samba服务器的安装与配置(转)
一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microsoft的 ...
- TocControl控件图层无法显示问题
在窗口里的层层嵌套SplitContainer后,出现最内层SplitContainer内部TocControl控件图层无法显示问题:加载完mxd后代后加上axTOCControl1.SetBuddy ...
- Web服务(Web Service)相关概念
1.概述 Web服务技术(Web Service )是一种面向服务的架构技术,通过标准的Web协议提供服务,保证不同平台的应用服务能够互相操作. 因为Web服务公布的数据基于XML格式和 SOAP协议 ...
- python 读取文件时报错: UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 127: illegal multibyte sequence
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 127: illegal multibyte sequence p ...