2019:
day1 :
T1:格雷码
#include<iostream>
int n;
unsigned long long k;
int main(){
std::cin>>n>>k;
k^=k>>1;
while(~--n)std::cout<<(k>>n&1);
}
T2:括号树
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const LL N=5e5+1;
LL n,h[N],w[N],fa[N],ans;
LL cn,hd[N],to[N],nt[N];
char s[N];
void dfs(LL x){
w[x]=w[fa[x]];
if(s[x]=='(')w[x]=x;
else if(w[x])h[x]=1+h[fa[w[x]]],w[x]=w[fa[w[x]]];
for(LL i=hd[x];i;i=nt[i])dfs(to[i]);
}
int main(){
scanf("%lld%s",&n,s+1);
for(LL i=2,x;i<=n;i++)
scanf("%lld",&x),fa[i]=x,
cn++,nt[cn]=hd[x],
hd[x]=cn,to[cn]=i;
dfs(1),ans=h[1];
for(LL i=2;i<=n;i++)
h[i]+=h[fa[i]],ans^=(i*h[i]);
printf("%lld\n",ans);
return 0;
}
T3:树上的数
#include <bits/stdc++.h>
using namespace std;
inline int gi(){
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
const int maxn = 2005;
inline void chkmin(int &a, int b) {if (a > b) a = b;}
int n, nump[maxn];
struct edge{
int to, next;
} e[maxn * 2];
int h[maxn], tot;
struct node{
int fst, lst, cnt, fa[maxn];
bool bg[maxn], ed[maxn];
void clear() {fst = lst = cnt = 0; for (int i = 1; i <= n; ++i) fa[i] = i, bg[i] = ed[i] = 1;}
int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}
} t[maxn];
inline void add(int u, int v){
e[++tot] = (edge) {v, h[u]}; h[u] = tot; ++t[u].cnt;
e[++tot] = (edge) {u, h[v]}; h[v] = tot; ++t[v].cnt;
}
int dfs1(int u, int fe){
int res = n + 1;
if (fe && (!t[u].lst || t[u].lst == fe)) {
if (t[u].ed[fe] && !(t[u].fst && t[u].cnt > 1 && t[u].find(fe) == t[u].find(t[u].fst)))
res = u;
}
for (int i = h[u], v, te; v = e[i].to, i; i = e[i].next) {
if (fe == (te = (i >> 1))) continue;
te = i >> 1;
if (!fe) {
if (!t[u].fst || t[u].fst == te) {
if (!t[u].bg[te]) continue;
if (t[u].lst && t[u].cnt > 1 && t[u].find(te) == t[u].find(t[u].lst)) continue;
chkmin(res, dfs1(v, te));
} else continue;
}else{
if (fe == t[u].lst || te == t[u].fst || t[u].find(fe) == t[u].find(te)) continue;
if (!t[u].ed[fe] || !t[u].bg[te]) continue;
if (t[u].fst && t[u].lst && t[u].cnt > 2 && t[u].find(fe) == t[u].find(t[u].fst) && t[u].find(te) == t[u].find(t[u].lst)) continue;
chkmin(res, dfs1(v, te));
}
}
return res;
}
int dfs2(int u, int fe, int p){
if (u == p) return t[u].lst = fe, 1;
for (int i = h[u], v, te; v = e[i].to, i; i = e[i].next)
if (fe != (te = (i >> 1)))
if (dfs2(v, te, p)) {
if (!fe) t[u].fst = te;
else {
t[u].ed[fe] = t[u].bg[te] = 0; --t[u].cnt;
t[u].fa[t[u].find(fe)] = t[u].find(te);
}
return 1;
}
return 0;
}
int main(){
int T = gi();
while (T--) {
tot = 1;
memset(h + 1, 0, sizeof(int) * n);
n = gi();
for (int i = 1; i <= n; ++i) t[i].clear();
for (int i = 1; i <= n; ++i) nump[i] = gi();
for (int i = 1; i < n; ++i) add(gi(), gi());
if (n == 1) {puts("1"); continue;}
for (int p, i = 1; i <= n; ++i) {
p = dfs1(nump[i], 0);
dfs2(nump[i], 0, p);
printf("%d ", p);
}
puts("");
}
return 0;
}
day2:
T1:Emiya 家今天的饭
#include<bits/stdc++.h>
#define mod 998244353
using namespace std;
typedef long long ll;
const int MAXN = 105, MAXM = 2005;
int n,m,a[MAXN][MAXM],sum[MAXN][MAXM];
ll f[MAXN][MAXN*2],g[MAXN][MAXN];
int main(){
cin >> n >> m;
for(int i = 1; i<=n; i++)
for(int j = 1; j<=m; j++){
scanf("%d",&a[i][j]);
sum[i][0] = (sum[i][0]+a[i][j])%mod;
}
for(int i = 1; i<=n; i++)
for(int j = 1; j<=m; j++)
sum[i][j] = (sum[i][0]-a[i][j]+mod)%mod;
ll ans = 0;
for(int col = 1; col<=m; col++){
memset(f,0,sizeof(f));
f[0][n] = 1;
for(int i = 1; i<=n; i++)
for(int j = n-i; j<=n+i; j++)
f[i][j] = (f[i-1][j]+f[i-1][j-1]*a[i][col]%mod+f[i-1][j+1]*sum[i][col]%mod)%mod;
for(int j = 1; j<=n; j++)
ans = (ans+f[n][n+j])%mod;
}
g[0][0] = 1;
for(int i = 1; i<=n; i++)
for(int j = 0; j<=n; j++)
g[i][j] = (g[i-1][j]+(j>0?g[i-1][j-1]*sum[i][0]%mod:0))%mod;
for(int j = 1; j<=n; j++)
ans = (ans-g[n][j]+mod)%mod;
cout << ans*(mod-1)%mod << endl;
return 0;
}
T2:划分
#include <cstdio>
#define ll long long
int n,type;
int dp[40000005];
ll pre[40000005];
int h = 1,r = 1;
struct node{
ll x,y;
}Q[40000005];
int m;
ll x,y,z;
int p[100005],l[100005],_r[100005];
void init(){
scanf("%lld%lld%lld%lld%lld%d",&x,&y,&z,&pre[1],&pre[2],&m);
for(int i = 3;i <= n;i++) pre[i] = (x * pre[i - 1] + y * pre[i - 2] + z) % 1073741824;
for(int i = 1;i <= m;i++){
scanf("%d%d%d",&p[i],&l[i],&_r[i]);
for(int j = p[i - 1] + 1;j <= p[i];j++) pre[j] = pre[j] % (_r[i] - l[i] + 1) + l[i];
}
}
int main(){
scanf("%d%d",&n,&type);
if(type == 1) init();
else for(int i = 1;i <= n;i++) scanf("%lld",&pre[i]);
for(int i = 1;i <= n;i++) pre[i] += pre[i - 1];
Q[r++] = {0,0};
int j = 0;
for(int i = 1;i <= n;i++){
while(h != r){
if(Q[h].x <= pre[i]){
j = Q[h++].y;
}else{
break;
}
}
dp[i] = j;
while(h != r){
if(Q[r - 1].x >= 2 * pre[i] - pre[j]){
r--;
}else{
break;
}
}
Q[r++] = {2 * pre[i] - pre[j],i};
}
__int128 ans = 0;
int p = n;
while(p){
ans += (__int128)(pre[p] - pre[dp[p]]) * (pre[p] - pre[dp[p]]);
p = dp[p];
}
int st[70],tp = 0;
while(ans){
st[++tp] = ans % 10;
ans /= 10;
}
while(tp) printf("%d",st[tp--]);
return 0;
}
T3:树的重心
#include<bits/stdc++.h>
#define o 300005
#define g0(a) memset(a,0,sizeof(a))
#define gc(a,b) memcpy(a,b,sizeof(a))
using namespace std;
inline int read()
{
register int data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=(data<<1)+(data<<3)+(ch^48),ch=getchar();
return data*w;
}
struct node
{
int to,next;
}w[o*2];
int n,T,son[o],s[o],pr[o],son2[o],p[o][18],son3[o],f[o],h[o],cnt,s2[o];
void add(int x,int y){++cnt;w[cnt].to=y;w[cnt].next=h[x];h[x]=cnt;}
void dfs(int x,int fa)
{
s[x]=1;pr[x]=fa;
for(int i=h[x];i;i=w[i].next)
{
int y=w[i].to;
if(y==fa)continue;
dfs(y,x);s[x]+=s[y];
if(s[y]>s[son[x]])son2[x]=son[x],son[x]=y;
else if(s[y]>s[son2[x]])son2[x]=y;
}
p[x][0]=son[x];
for(int i=1;i<=17;i++)p[x][i]=p[p[x][i-1]][i-1];
}
long long ans;
int judge(int x,int sum)
{
return x*(max(s2[son3[x]],sum-s2[x])<=sum/2);
}
void dfs2(int x,int fa)
{
for(int i=h[x];i;i=w[i].next)
{
int y=w[i].to;
if(y==fa)continue;
s2[x]=s[1]-s[y];f[y]=0;f[x]=0;
if(son[x]==y)son3[x]=son2[x];
else son3[x]=son[x];
if(s2[fa]>s2[son3[x]])son3[x]=fa;
p[x][0]=son3[x];
for(int j=1;j<=17;j++)p[x][j]=p[p[x][j-1]][j-1];
int b=x;
for(int j=17;j>=0;j--)if(s2[x]-s2[p[b][j]]<=s2[x]/2)b=p[b][j];
ans+=judge(son3[b],s2[x])+judge(b,s2[x])+judge(f[b],s2[x]);
b=y;
for(int j=17;j>=0;j--)if(s2[y]-s2[p[b][j]]<=s2[y]/2)b=p[b][j];
ans+=judge(son3[b],s2[y])+judge(b,s2[y])+judge(f[b],s2[y]);
f[x]=y;
dfs2(y,x);
}
son3[x]=p[x][0]=son[x];f[x]=pr[x];
for(int j=1;j<=17;j++)p[x][j]=p[p[x][j-1]][j-1];
s2[x]=s[x];
}
int main()
{
T=read();
while(T--)
{
g0(h);g0(son);g0(f);g0(pr);cnt=0;ans=0;
n=read();
for(int i=1;i<n;i++)
{
int x=read(),y=read();
add(x,y);add(y,x);
}
dfs(1,0);
gc(s2,s);gc(son3,son);gc(f,pr);
dfs2(1,0);
cout<<ans<<'\n';
}
return 0;
}