题解:P11647 【MX-X8-T6】「TAOI-3」俄罗斯蓝猫
not_clever_syl · · 题解
首先,假设所有
用的是 lmh_qwq 的思路,即询问一个以
最后使用
这样看似是
但是我们比较蠢,不能像 lmh_qwq 一样写出来好剪枝或常数很大(可以看我代码里的奶龙实现),
考虑怎么优化,你注意到计算环中每条边的计算过程都要用到
并且你发现每次翻转前后
于是我们考虑对每个
考虑算不中的概率(单组数据失败率):有恰好
你发现在
当然
只在
代码:
#include<bits/stdc++.h>
#include<vector>
std::vector<long long> game(int n);
std::vector<long long> ask(std::vector<int> x,std::vector<int> y);
using namespace std;
int d[505];
std::vector<int>e[505];
pair<int,int>ee[505];
std::unordered_map<long long,int>mp;
std::unordered_set<long long>st;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
std::vector<long long> game(int n){
std::vector<long long>ans,res1,res2;
ans.resize(n);
std::vector<int>xx,yy;
{
xx.reserve(n-1),yy.reserve(n-1);
for(int i=0;i<n-1;++i){
xx.emplace_back(0);
yy.emplace_back(i+1);
}
xx.back()=1,yy.back()=2;
res1=ask(xx,yy);
}
{
xx.clear(),yy.clear();
for(int i=1;i<n;++i){
xx.emplace_back(i);
yy.emplace_back((i+1==n)?1:i+1);
}
xx[0]=0,yy[0]=n-1;
res2=ask(xx,yy);
}
__int128 sum1=0,sum2=0;
st.clear();mp.clear();
for(int i=0;i<n-1;++i)sum1+=res1[i],sum2+=res2[i];
for(int k=0;k<n-1;++k){
for(int l=0;l<k;++l){
long long x;
x=res1[k]+res1[l];
if(n>10){
int t=10;
while(t--){
int i=rnd()%(n-1);
auto y=x-res2[i];
if(y<=2e18&&y>0&&!(y&1))st.insert(y/2);
}
}
else{
for(int i=0;i<n-1;++i){
auto y=x-res2[i];
if(y<=2e18&&y>0&&!(y&1))st.insert(y/2);
}
}
}
}
for(int i=0;i<n-1;++i){
for(int j=0;j<n-1;++j){
__int128 s1=sum1-res1[i]+res2[j],s2=sum2-res2[j]+res1[i];
if(!(s2&1)&&((s1-(s2>>1))%(n-1)==0)){
ans[0]=(s1-(s2>>1))/(n-1);
if(!st.count(ans[0]))continue;//优化
/*
ans[0]=3*s1-(sum1+sum2) / (2n-2)
*/
std::swap(res1[i],res2[j]);
mp.clear();
for(int k=0;k<n-1;++k){
mp[res2[k]]=k;
ee[k]=make_pair(0,0);
}
bool fl=true;
for(int k=0;k<n-1;++k){
for(int l=0;l<k;++l){
auto x=res1[k]+res1[l]-2*ans[0];
if(!mp.count(x))continue;
auto t=mp[x];
if(ee[t].first||ee[t].second){
fl=false;
break;
}
ee[t]=make_pair(l,k);
}
if(!fl)break;
}
if(fl){
for(int i=0;i<n-1;++i){
if(!ee[i].first&&!ee[i].second){
fl=false;
break;
}
}
}
if(fl){
for(int i=0;i<=n;++i)d[i]=0;
for(int i=0;i<n-1;++i){
auto tp=ee[i];
++d[tp.first],++d[tp.second];
if(d[tp.first]>2||d[tp.second]>2){
fl=false;
break;
}
}
if(fl)for(int i=0;i<n-1;++i)if(d[i]!=2){
fl=false;
break;
}
if(fl){
for(int i=0;i<=n;++i)e[i].clear();
for(int i=0;i<n-1;++i){
auto tp=ee[i];
e[tp.first].emplace_back(tp.second);
e[tp.second].emplace_back(tp.first);
}
auto tp=ee[j];
int u,pre=-1,nxt;
for(auto it:e[i]){
if(it==tp.first){
u=tp.first;
pre=tp.second;
}
if(it==tp.second){
u=tp.second;
pre=tp.first;
}
}
pre^=e[u][0]^e[u][1];
for(int i=1;i<n;++i){
ans[i]=res1[u]-ans[0];
nxt=e[u][0]^e[u][1]^pre;
pre=u;u=nxt;
}
return ans;
}
}
std::swap(res1[i],res2[j]);
}
}
}
return ans;
}