【暴力】hdu6121 Build a tree

给你n,K,让你构造出一颗n个结点的完全K叉树,求所有结点子树大小的异或和。

先把n号结点到根的路径提取出来单独计算。然后这条路径把每一层分成了左右两部分,每一层的左侧和其上一层的右侧的结点的子树大小相同。

就可以容易计算每种大小的子树个数了。

当K等于1时,要单独讨论,答案为1 xor 2 xor ... xor n。这个打个表非常明显。

#include<cstdio>
using namespace std;
typedef long long ll;
ll n,K,pw[105];
int T;
int main(){
//	freopen("1002.in","r",stdin);
//	freopen("1002.out","w",stdout);
	scanf("%d",&T);
	for(;T;--T){
		scanf("%lld%lld",&n,&K);
		if(K==1){
			if(n%4ll==0){
				printf("%lld\n",n);
			}
			else if(n%4ll==1ll){
				printf("%lld\n",1ll);
			}
			else if(n%4ll==2ll){
				printf("%lld\n",n+1ll);
			}
			else{
				printf("%lld\n",0ll);
			}
			continue;
		}
		pw[0]=1;
		int j;
		for(j=1;pw[j-1]<=n/K;++j){
			pw[j]=pw[j-1]*K;
		}
		--j;
		int e=j;
		for(int k=1;k<=j;++k){
			if(pw[k-1]>n-pw[k]){
				e=k-1;
				break;
			}
			pw[k]+=pw[k-1];
		}
		if(n==pw[e]){
			--e;
		}
		ll i=n-1ll;
		ll now=n;
		ll nowsiz=1,sumnowsiz=1;
		ll ans=1;
		ll sheng=((n-pw[e])%K!=0 ? (n-pw[e])%K : K);
		int E=e;
		while(i!=0){
			ll fa=(i-1)/K;
			if((i-1ll-fa)%2ll==1ll){
				ans^=sumnowsiz;
			}
			ans^=(sumnowsiz+sheng);
			now=pw[e];
			--e;
			i=fa;
			nowsiz*=K;
			sumnowsiz+=nowsiz;
			sheng=((n-pw[E])%(nowsiz*K)!=0 ? (n-pw[E])%(nowsiz*K) : (nowsiz*K));
		}
		printf("%lld\n",ans);
	}
	return 0;
}
时间: 2024-10-13 13:52:10

【暴力】hdu6121 Build a tree的相关文章

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叉树,那么直接就可以计算出结果. 当不

HDU 6121 Build a tree(找规律+模拟)

Build a tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 946    Accepted Submission(s): 369 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n−1

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 node labeled i is the node labeled  . HazelFan wonders the size of every subtree, and you just need to tell him the XOR value of these answers. Input

HDU 6121 Build a tree(完全K叉树)

http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满K叉树的一些知识: 对于每棵子树,只有三种情况: ①是满K叉树  ②不是满K叉树  ③叶子节点 并且非满K叉树最多只有一个,所以只需要将它进行特殊处理,具体看代码吧,说不清楚.代码参考了http://blog.csdn.net/my_sunshine26/article/details/77200282

FZU2077——暴力技巧——The tallest tree

lzs种了n棵树,每棵树每天长高一定的高度.某一天,lzs想知道树长得怎么样了,你能求出那一天最高的树有多高吗? Input 有多组测试数据,每组数据第一行输入两个整数n,m(1<=n,m<=100000),接下来n行,每行两个整数a,b(0<=a,b<=100000),表示第i棵树在第0天的高度以及每天生长的高度.接下来m行,每行一个整数x(0<=x<=100000),表示询问第x天最高的树有多高. Output 对于每个询问输出一行,为当天最高的树的高度. Samp

Build Relationship Tree

import java.util.*; class Node{ String val; Set<Node> children = new HashSet<Node>(); // at most 15 children; public Node(String val){ this.val = val; this.children = new HashSet<>(); } } public class RelationTree { private void travel(N

POJ-2201-Cartesian Tree(笛卡尔树)

Description Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binary search tree is a rooted ordered binary tree, such that for its every node x the following condition is satisfied: each node in its left

【HDU 5370】 Tree Maker(卡特兰数+dp)

Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting game which is named Tree Maker. In this game, all trees are binary trees. Initially, there is a tree with only one vertex and a cursor on it. Tree Lover

Verify Preorder Serialization of a Binary Tree

One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / 3 2 / \ / 4 1 # 6 / \ / \ / # # # # # # For examp