您的位置:首页 > 大数据 > 人工智能

2017 Multi-University Training Contest - Team 7 待补

2017-08-19 00:48 288 查看
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
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐