其他分享
首页 > 其他分享> > Wannafly挑战赛23

Wannafly挑战赛23

作者:互联网

B. 游戏

$n$堆石子, 第$i$堆初始$a_i$, 每次只能选一堆, 假设一堆个数$x$, 只能取$x$的约数, 求先手第一步必胜取法.

 

SG入门题, 预处理出所有$SG$值. 先手要必胜必须满足留给后手的异或值为0.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e5+10;
int n, a[N], sg[N], vis[N];
vector<int> fac[N];

void init() {
	REP(i,1,N-1) for(int j=i;j<N;j+=i) fac[j].pb(i);
	REP(i,1,N-1) {
		for (int j:fac[i]) vis[sg[i-j]]=i;
		REP(j,0,N-1) if (vis[j]!=i) {
			sg[i] = j; break;
		}
	}
}

int main() {
	init();
	scanf("%d", &n);
	int s = 0;
	REP(i,1,n) { 
		scanf("%d", a+i);
		s ^= sg[a[i]];
	}
	if (!s) return puts("0"),0;
	int ans = 0;
	REP(i,1,n) {
		for (int x:fac[a[i]]) {
			if (!(s^sg[a[i]]^sg[a[i]-x])) ++ans;
		}
	}
	printf("%d\n", ans);
}

 

D.漂亮的公园

给定树, 点$i$颜色为$c[i]$, 每次询问所有颜色为$x$的点到颜色为$y$的点的最大距离.

 

结论: 对于树上点集$S$, $S$内距离最远的两点为$x,y$, 则其他点$u$到点集$S$的最远距离必然是$u$到$x$或$u$到$y$.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#define REP(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int N = 1e5+10;
int n, q, c[N], sz[N], dep[N];
int fa[N], son[N], top[N];
int f[N][2];
vector<int> g[N];
int b[N];

void dfs(int x, int d, int f) {
    sz[x]=1,fa[x]=f,dep[x]=d;
    for (int y:g[x]) if (y!=f) {
        dfs(y,d+1,x),sz[x]+=sz[y];
        if (sz[y]>sz[son[x]]) son[x]=y;
    }
}
void dfs(int x, int tf) {
    top[x]=tf;
    if (son[x]) dfs(son[x],tf);
    for (int y:g[x]) if (!top[y]) dfs(y,y);
}
int lca(int x, int y) {
    while (top[x]!=top[y]) {
        if (dep[top[x]]<dep[top[y]]) swap(x,y);
        x=fa[top[x]];
    }
    return dep[x]<dep[y]?x:y;
}
int dis(int x, int y) {
	if (!x||!y) return 0;
	return dep[x]+dep[y]-2*dep[lca(x,y)];
}

void upd(int x) {
	int &A = f[c[x]][0], &B = f[c[x]][1];
	if (!A) A = x;
	else if (!B) B = x;
	else {
		int d1 = dis(A,B), d2 = dis(A,x), d3 = dis(B,x);
		if (d2>d1&&d2>d3) { 
			if (d2>d3) B = x;
			else A = x;
		}
		else if (d3>d1) A = x;
	}
}

int main() {
	scanf("%d%d", &n, &q);
	REP(i,1,n) scanf("%d",c+i),b[i]=c[i];
	sort(b+1,b+1+n),*b=unique(b+1,b+1+n)-b-1;
	REP(i,1,n) c[i]=lower_bound(b+1,b+1+*b,c[i])-b;
	REP(i,2,n) {
		int u, v;
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1,0,0),dfs(1,1);
	REP(i,1,n) upd(i);
	while (q--) {
		int x, y;
		scanf("%d%d", &x, &y);
		int xx=lower_bound(b+1,b+1+*b,x)-b;
		int yy=lower_bound(b+1,b+1+*b,y)-b;
		if (b[xx]!=x||b[yy]!=y) {
			puts("0"); continue;
		}
		x = xx, y = yy;
		int ans = 0;
		REP(i,0,1) REP(j,0,1) ans = max(ans, dis(f[x][i],f[y][j]));
		printf("%d\n", ans);
	}
}

 

标签:23,int,REP,dfs,Wannafly,挑战赛,include,ll,define
来源: https://www.cnblogs.com/uid001/p/10972827.html