AtCoder Beginner Contest 201 E - Xor Distances

https://atcoder.jp/contests/abc201/tasks/abc201_e

这应该是个人尽皆知经典傻逼题然而我想了一年

a[u]为根节点到u的异或,dist[i,j]=a[i]^a[j]

然后他要求和,我们发现可以把他们分成二次幂来统计数量求和,然后就没了,我太菜了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxl=3e5+10;
const int mod=1e9+7;

int n,m,k,cnt,tot,cas;ll ans;
ll a[maxl];
ll b[62][2];
struct ed
{
	int v;ll l;
};
vector<ed> e[maxl];
bool vis[maxl];
char s[maxl];

inline void dfs(int u,int fa)
{
	for(ed ee:e[u])
	{
		int v=ee.v;
		if(v==fa) continue;
		a[v]=a[u]^ee.l;
		dfs(v,u);
	}
}

inline void prework()
{
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++)
	{
		int u,v;ll l;scanf("%d%d%lld",&u,&v,&l);
		e[u].push_back(ed{v,l});
		e[v].push_back(ed{u,l});
	}
	dfs(1,0);
	for(int i=1;i<=n;i++)
	{
		for(int j=60;j>=0;j--)
			b[j][a[i]>>j&1]++;
	}
}

inline ll qp(ll a,ll b)
{
	ll ans=1,cnt=a;
	while(b)
	{
		if(b&1)
			ans=ans*cnt%mod;
		cnt=cnt*cnt%mod;
		b>>=1;
	}
	return ans;
	
}

inline void mainwork()
{
	ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=60;j>=0;j--)
			ans=(ans+(1ll<<j)%mod*b[j][(a[i]>>j&1)^1])%mod;
	}
	ans=ans*qp(2,mod-2)%mod;
}

inline void print()
{
	printf("%lld\n",ans);
}

int main()
{
	int t=1;
	//scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值