# Codeforces Round #633 (Div. 1)

## A. Powered Addition (CF 1338 A)

### 解题思路

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
template <typename T>
int s = 0, c = getchar();
x = 0;
while (isspace(c)) c = getchar();
if (c == 45) s = 1, c = getchar();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
if (s) x = -x;
}

template <typename T>
void write(T x, char c = ' ') {
int b[40], l = 0;
if (x < 0) putchar(45), x = -x;
while (x > 0) b[l++] = x % 10, x /= 10;
if (!l) putchar(48);
while (l) putchar(b[--l] | 48);
putchar(c);
}

int main(void) {
for (int ii = 1; ii <= kase; ++ii) {
int n;
int ans=0;
int qwq=-1e9-7;;
for(int u,i=1;i<=n;++i){
if (qwq<=u) qwq=u;
else{
ans=max(ans,qwq-u);
}
}
int cnt=0;
while(ans){
ans>>=1;
++cnt;
}
write(cnt,'\n');
}
return 0;
}

## B. Edge Weight Assignment (CF 1338 B)

### 解题思路

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
template <typename T>
int s = 0, c = getchar();
x = 0;
while (isspace(c)) c = getchar();
if (c == 45) s = 1, c = getchar();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
if (s) x = -x;
}

template <typename T>
void write(T x, char c = ' ') {
int b[40], l = 0;
if (x < 0) putchar(45), x = -x;
while (x > 0) b[l++] = x % 10, x /= 10;
if (!l) putchar(48);
while (l) putchar(b[--l] | 48);
putchar(c);
}

void DFS(int u,int fa,int deep[],vector<int> edge[]){
deep[u]=deep[fa]+1;
for(auto v:edge[u]){
if (v==fa) continue;
DFS(v,u,deep,edge);
}
}

int dfs(int u,int fa,bool &qwq,int &ans,int deep[],vector<int> edge[],int st){
int cnt=0;
int aa=0;
deep[u]=deep[fa]+1;
for(auto v:edge[u]){
if (v==fa) continue;
++cnt;
aa+=dfs(v,u,qwq,ans,deep,edge,st);
}
if (!cnt){
if (!(deep[u]&1)) qwq=true;
return 1;
}
if (fa==st) ++aa;
ans-=max(0,aa-1);
return 0;
}

int main(void) {
int n;
vector<int> edge[n+1];
for(int u,v,i=1;i<n;++i){
edge[u].push_back(v);
edge[v].push_back(u);
}
int deep[n+1]={0};
DFS(1,1,deep,edge);
int st=0,dest=0;
for(int i=1;i<=n;++i)
if (dest<deep[i]){
dest=deep[i];
st=i;
}
deep[st]=0;
bool qwq=false;
int ans=n-1;
dfs(st,st,qwq,ans,deep,edge,st);
printf("%d %d\n",(qwq?3:1),ans);
return 0;
}

## C. Perfect Triples (CF 1338 C)

### 题目大意

• $a<b<c$
• $a,b,c \notin s$
• $a\bigoplus b\bigoplus c=0$
• (a,b,c)是可选中的字典序最小的一组
• 把a,b,c依次加入$s$数组的末尾
• 重复第一步

### 解题思路

• $0\ 0\ 0$
• $1\ 2\ 3$
• $2\ 3\ 1$
• $3\ 1\ 2$

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
template <typename T>
int s = 0, c = getchar();
x = 0;
while (isspace(c)) c = getchar();
if (c == 45) s = 1, c = getchar();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
if (s) x = -x;
}

template <typename T>
void write(T x, char c = ' ') {
int b[40], l = 0;
if (x < 0) putchar(45), x = -x;
while (x > 0) b[l++] = x % 10, x /= 10;
if (!l) putchar(48);
while (l) putchar(b[--l] | 48);
putchar(c);
}

void work(LL id,LL i,pair<LL,LL> &ans){
if (i<0) return;
if (i==0) {
LL rank=id;
if (rank==1){
ans.first^=(1ll<<(i));
ans.second^=(1ll<<(i+1));
}else if (rank==2){
ans.first^=(1ll<<(i+1));
ans.second^=((1ll<<(i))^(1ll<<(i+1)));
}else if (rank==3){
ans.first^=((1ll<<(i))^(1ll<<(i+1)));
ans.second^=(1ll<<(i));
}
return;
}
LL cnt=(1ll<<(i));
work(id%cnt,i-2,ans);
LL rank=id/cnt;
if (rank==1){
ans.first^=(1ll<<(i));
ans.second^=(1ll<<(i+1));
}else if (rank==2){
ans.first^=(1ll<<(i+1));
ans.second^=((1ll<<(i))^(1ll<<(i+1)));
}else if (rank==3){
ans.first^=((1ll<<(i))^(1ll<<(i+1)));
ans.second^=(1ll<<(i));
}
}

int main(void) {
int t;
while(t--){
LL n;
LL id=(n-1)/3;
LL pos=(n-1)%3;
pair<LL,LL> ans;
ans.first=0;
ans.second=0;
for(int i=0;true;i+=2){
if (id<(1ll<<i)){
work(id,i-2,ans);
ans.first^=(1ll<<(i));
ans.second^=(1ll<<(i+1));
break;
}else id-=(1ll<<i);
}
printf("%lld\n",pos==0?ans.first:(pos==1?ans.second:(ans.first^ans.second)));
}
return 0;
}

posted @ 2020-04-15 23:24  ~Lanly~  阅读(73)  评论(0编辑  收藏