2017 Multi-University Training Contest - Team 7 待补

Build a tree HDU 6121

 

题意:

一棵 n 个点的完全 k 叉树,结点标号从 0 到 n - 1,求以每一棵子树的大小的异或和。

 

思路:

 

考虑每一层最多只存在一个特殊点,以该特殊点为根的子树不是满k叉树,那么这个特殊点左面的所有点构成的

树一定都是满k叉树,右面一定是比左面的层数-1的满k叉树.

那么我们可以预处理出所有的层数为h的满k叉树的结点的个数.然后从下往往上依次找每一层特殊的分界点,

依次计算结点个数异或,对于以特殊分界点为根构成的子树,因为除叶子节点那一层剩下的一定也是一个满的,所以子树结点个数为 num[h-1]+叶子节点个数

 

 

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 100 + 10;

ll n,k;
ll num[maxn];

ll qmod(ll a,ll b)
{
    ll res = 1;
    while(b)
    {
        if(b&1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

void init(ll deep)
{
    for(ll i = 1;i <= deep;i++)//k 叉树前i层的结点个数 
        num[i] = ( qmod(k,i) - 1 ) / (k - 1);
    return ;
}

int main()
{

    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll ans;
        scanf("%lld%lld",&n,&k);
        if(k == 1)//k等于1特判 
        {
            if(n % 4 == 0)
               ans = n;
            else if(n % 4 == 1)
                ans = 1;
            else if(n % 4 == 2)
                ans = n + 1;
            else
                ans = 0;
            printf("%lld\n",ans);
            continue;
        }
        ll depth = 1;
        ll res = n - 1;
        while(res > 0)//确定树的层数 
        {
            res = (res - 1) / k;
            depth ++;
        }
        init(depth);
        depth --;
        int siz = 2; //从下往上计算,不考虑叶子节点 
        ans = n;
        ans ^= (n - num[depth]) & 1;//叶子节点 
        ll pos = (n - 1 - 1) / k;//找到第一个特殊分界点 
        while(depth > 1)
        {
            ll l = num[depth - 1];//确定该层最左边的结点编号, 
            ll r = num[depth] - 1;//最右边的结点编号 
            ll tmpl = num[siz]; //左面的树的深度比右面大1 
            ll tmpr = num[siz - 1];
            if((pos - l) & 1)
                ans ^= tmpl;
            if((r - pos) & 1)
                ans ^= tmpr;
            ll fa = pos;//考虑以特殊分界点为根的非满k叉树的结点个数. 
            while(fa <= (n - 1 - 1) / k)
            {
                fa = fa * k + 1;
            }
            ll cnt = num[siz - 1] + n - fa;//除叶子节点那一层剩下的也是满k叉树 
            ans ^= cnt;
            siz++;
            depth--;
            pos = (pos - 1)/k;//往上继续找分界点 
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

 

 

 

EEuler theorem HDU 6124

 

题目大意:给你 a ,问 a 对所有数取模后的所有结果有多少个。

打表得到规律

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e5+10;

set<int>st;

int main()
{
//	int n = 10;
//	for(int i = 1;i <= 20;i++)
//	{
//		st.clear();
//		for(int j = 1;j <= i ;j++)
//		st.insert(i%j);
//		printf("%d %d\n",i,st.size()+1);
//		puts("");
//	}
	int _;
	int a;
	cin>>_;
	while(_--)
	{
		cin>>a;
		printf("%d\n",(a+1)/2+1);
	}
	return 0;
}

 

 

 

HDU - 6125

见 点击打开链接

 

Kolakoski HDU - 6130

 

 

定义的序列,然后让你输出第n项,找规律

大体可以得到递推,如果这一项为2,那么他可以推得后面两项和他前面的一项相反,为1的话就决定后面一项和前面相反.

它的定义很简单,若把数列中相同的数定为一组,令a(1)=1,a(2)=2,则a(n)等于第n组数的长度。 
可以根据这个定义来推算第三项以后的数:例如由于a(2)=2,因此第2组数的长度是2,因此a(3)=2,; 
由于a(3)=2,所以第三组数的长度是2,因此a(4)=a(5)=1;由于a(4)=1,a(5)=1,所以第四组数和第五组数的长度都为1,因此a(6)=2,a(7)=1,以此类推。

 

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e7+10;

int a[maxn]; 

void init()
{
	a[1] = 0;
	a[2] = a[3] = 1;
	for(int j = 4,i = 3;j < maxn ;i++)
	{
		if(a[i] == 1)
		{
			a[j] = a[j + 1] = !a[j - 1];
			j += 2;
		}
		else
		{
			a[j] = !a[j - 1];
			j++;
		}
	}
}

int main()
{
	init();
	int _;
	cin>>_;
	while(_--)
	{
		int n;
		cin>>n;
		printf("%d\n",1 + a[n]);
	}
	return 0;
}

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marcus-Bao

万水千山总是情,只给五角行不行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值