跳到正文

题解

P14178 Jueves题解

HNO_3约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年10月7日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P14178

版本

  1. v1当前

P14178 Jueves题解

- 前置知识

  1. aand⁡b=a∨ba \operatorname{and} b= a \lor b;
  2. aor⁡b=a∧ba \operatorname{or} b=a \land b;
  3. axor⁡b=a⊕b=a∨b−a∧ba \operatorname{xor} b=a\oplus b=a \lor b - a \land b;

- 公式推导

aand⁡b+aor⁡b+axor⁡ba \operatorname{and} b+a \operatorname{or} b+a \operatorname{xor} b=

a∨b+a∧b+a∨b−a∧ba \lor b+a \land b+a \lor b - a \land b=

2×(a∨b)2\times (a \lor b);

- 结论

即a⊕b+a∨b+a∧b=2×(a∨b)=2×a∣ba\oplus b+a \lor b+a \land b=2 \times(a \lor b)=2 \times a \mid b ; (按位或)

  • 注意!!!题目中的0≤ai≤10180\le a_i\le 10^{18},且对于任意的au,ava_u,a_v,都有a⊕b+a∨b+a∧b=2×(a∨b)=2×a∣b≥0a\oplus b+a \lor b+a \land b=2 \times(a \lor b)=2 \times a \mid b \ge 0, 所以ss到tt最小贡献就是2×(a[u]+a[t])2\times(a[u]+a[t]) ( 若s=ts=t,则贡献为0 ).
  • 代码如下
C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
ll T,n,s,t,i,j;
ll a[N];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>T;
	while(T--)
	{
		cin>>n>>s>>t;
		for(i=1;i<=n;i++) cin>>a[i];
		if(s==t){
			cout<<0<<'\n';continue;
		}
		cout<<2*(a[s]|a[t])<<'\n';
	}
	return 0;
}

QwQ

原文