位运算
背景
出题人的官方题解。
本题解贪心的证明部分由 @chenbs 完成(他自认为完备)。题目背景里的错误是他在造这题数据的时候犯的,望周知。
下面给出本题的整体思路以及一些想法。
时间限制已经在 std 六倍以上。赛时好像写最终正解的人不多。std 仅使用了题目提供的读入模板。
题意
长度为
部分分
第
第
第
第
赛时要是往 dp 想是很难的,标解和 dp 没有关系。
思路
首先看到三个位运算可以考虑拆位。虽然乘积没法拆位但是可以先转化一下问题。这里判无解边界有点多,不要骂我呜呜呜。
算出所有数字每位置总共最多能放多少
这样问题转化为了所有数字每一位有放
然后有一个好猜不好证的贪心构造:从高位往低位考虑,每次选择最小的数字填
下面是这个贪心的正确性证明,来源于 chenbs:
:::info[by chenbs]{open}
不妨设
对于一个数
我们称一个序列
引理
证明:考虑一个初始全
引理
证明:反证法,假设存在一个最优序列为
答案必然更优,与
结论:好的序列一定是乘积最大的序列。
| 设一个乘积最大的序列为 |
|---|
但是这样做,复杂度至少是
我们回想一下拆位的过程,因为每一位的最多
这里比较考察精细实现,不同做法会获得不同的分数,所以数据给了一定的梯度。
比如用堆维护复杂度也是
然后我们可以发现,可以动态维护最大值和严格次大值做到
具体地,因为当前考虑位的后面位所有数是一样的,所以最大值和严格次大值的区别一定在更高位。设最大值为
另外,可以
实现
:::info[possible 满分做法(waz),堆]
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100,mod = 998244853;
LL s[N];
LL n,a,b,c;//and or xor
typedef unsigned long long ull;
char buf[1<<20],*p1,*p2;
char gc() {
if(p1==p2) p2=(p1=buf)+fread(buf,1,1<<20,stdin);
return *p1++;
}
ull rd() {
ull s=0;
char c=gc();
while(c<48) c=gc();
while(c>47) s=s*10+(c^48), c=gc();
return s;
}
LL qmi(LL x,LL k)
{
LL res = 1;
x %= mod;
while(k)
{
if(k & 1) res = res * x % mod;
k >>= 1;
x = x * x % mod;
}
return res;
}
int main()
{
int t = rd();
while(t --)
{
n = rd(),a = rd(),b = rd(),c = rd();
assert(n > 0),assert(a >= 0),assert(b >= 0),assert(c >= 0);
assert(n < (1ll << 60)),assert(a < (1ll << 60)),assert(b < (1ll << 60)),assert(c < (1ll << 60));
if(n == 1)
{
if(a == b && b == c) cout<<a % mod<<'\n';
else cout<<"-1\n";
continue;
}
int flag = 1;
LL all = 0,cnt = 0;
for(int i = 0; i <= 60; i ++)
{
LL sand = (a >> i) & 1,sor = (b >> i) & 1,sxor = (c >> i) & 1;
if(sand > sor)
{
cout<<"-1\n";
flag = 0;
break;
}
if(sand)
{
if((n & 1) != sxor)
{
cout<<"-1\n";
flag = 0;
break;
}
else s[i] = n,cnt ++,all |= (1ll << i);
continue;
}
if(!sor)
{
if(sxor)
{
cout<<"-1\n";
flag = 0;
break;
}
else s[i] = 0;
continue;
}
s[i] = n - 2 + ((n & 1) != (sxor & 1));
if(s[i]) cnt ++,all |= (1ll << i);
else
{
cout<<"-1\n";
flag = 0;
break;
}
}
if(!flag) continue;
priority_queue<pair<LL,LL>> q;
q.push({all,n});
LL res = 1;
for(int i = 60; i >= 0; i --)
{
if(s[i] == 0 || s[i] == n) continue;
if(s[i] == n - 1)
{
auto t = q.top();
q.pop();
if(t.second > 1) q.push({t.first,t.second - 1});
LL cur = t.first - (1ll << i);
q.push({cur,1});
}
if(s[i] == n - 2)
{
auto t = q.top();
q.pop();
LL cur = t.first - (1ll << i);
if(t.second > 1)
{
if(t.second > 2) q.push({t.first,t.second - 2});
q.push({cur,2});
continue;
}
auto tt = q.top();
q.pop();
if(t.second > 1) q.push({t.first,t.second - 1});
q.push({cur,1});
if(tt.second > 1) q.push({tt.first,tt.second - 1});
cur = tt.first - (1ll << i);
q.push({cur,1});
}
}
while(q.size())
{
auto t = q.top();
q.pop();
res = res * qmi(t.first,t.second) % mod;
}
cout<<res<<'\n';
}
return 0;
}
::: :::info[possible 满分做法(waz)]
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100,mod = 998244853;
LL s[N];
LL n,a,b,c;//and or xor
char buf[1 << 20],*p1,*p2;
char gc()
{
if(p1 == p2) p2 = (p1 = buf) + fread(buf,1,1 << 20,stdin);
return *p1 ++;
}
LL rd()
{
LL s = 0;
char c = gc();
while(c < 48) c = gc();
while(c > 47) s =s * 10 + (c ^ 48),c = gc();
return s;
}
LL qmi(LL x,LL k)
{
LL res = 1;
x %= mod;
while(k)
{
if(k & 1) res = res * x % mod;
k >>= 1;
x = x * x % mod;
}
return res;
}
int main()
{
int t;
cin>>t;
while(t --)
{
n = rd(),a = rd(),b = rd(),c = rd();
if(n == 1)
{
if(a == b && b == c) cout<<a % mod<<'\n';
else cout<<"-1\n";
continue;
}
int flag = 1;
LL all = 0,cnt = 0;
for(int i = 0; i <= 60; i ++)
{
LL sand = (a >> i) & 1,sor = (b >> i) & 1,sxor = (c >> i) & 1;
if(sand > sor)
{
cout<<"-1\n";
flag = 0;
break;
}
if(sand)
{
if((n & 1) != sxor)
{
cout<<"-1\n";
flag = 0;
break;
}
else s[i] = n,cnt ++,all |= (1ll << i);
continue;
}
if(!sor)
{
if(sxor)
{
cout<<"-1\n";
flag = 0;
break;
}
else s[i] = 0;
continue;
}
s[i] = n - 2 + ((n & 1) != (sxor & 1));
if(s[i]) cnt ++,all |= (1ll << i);
else
{
cout<<"-1\n";
flag = 0;
break;
}
}
if(!flag) continue;
LL maxv = all,maxs = n,max2v = 0,max2s = 0;
LL res = 1;
for(int i = 60; i >= 0; i --)
{
if(s[i] == 0 || s[i] == n) continue;
if(s[i] == n - 1)
{
LL cur = maxv - (1ll << i);
if(maxs > 1)
{
maxs --;
res = res * qmi(max2v,max2s) % mod;
max2v = cur,max2s = 1;
}
else maxv = cur;
}
if(s[i] == n - 2)
{
LL cur = maxv - (1ll << i);
if(maxs > 2)
{
maxs -= 2;
res = res * qmi(max2v,max2s) % mod;
max2v = cur,max2s = 2;
}
else if(maxs == 2) maxv = cur;
else
{
if(max2s > 1)
{
maxv = cur;
res = res * ((max2v - (1ll << i)) % mod) % mod;
max2s --;
}
else maxv -= (1ll << i),max2v -= (1ll << i);
}
}
}
res = res * qmi(maxv,maxs) % mod * qmi(max2v,max2s) % mod;
cout<<res<<'\n';
}
return 0;
}
::: :::info[standard 满分做法(cbs),小常数]
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
char buf[1<<20],*p1,*p2;
char gc() {
if(p1==p2) p2=(p1=buf)+fread(buf,1,1<<20,stdin);
return *p1++;
}
ull rd() {
ull s=0;
char c=gc();
while(c<48) c=gc();
while(c>47) s=s*10+(c^48), c=gc();
return s;
}
ull n,a,b,c,op[130]; // and or xor
const int M=998244853;
ull qpow(ull x, ull y) {
x%=M;
ull res=1;
while(y) {
if(y&1) res=res*x%M;
x=x*x%M;
y>>=1;
}
return res;
}
void ATRI() {
if(n==1) {
if(a==b && b==c) cout<<a%M<<'\n';
else cout<<"-1\n";
return;
}
if((~b)&(a|c)) {
cout<<"-1\n";
return;
}
ull tmp=(n&1 ? -1llu : 0);
if(a&(c^tmp)) {
cout<<"-1\n";
return;
}
ull op1=b&(~a)&(c^tmp), op2=b&(~a)&(~(c^tmp));
if(op2 && n==2) {
cout<<"-1\n";
return;
}
ull ans=1,mx=b,se=1,cntmx=n,cntse=0;
for(int i=63; i>=0; i--) {
if(op1&(1ll<<i)) {
if(cntmx==1) mx^=(1llu<<i);
else cntmx--, ans=ans*qpow(se,cntse)%M, se=mx^(1llu<<i), cntse=1;
} else if(op2&(1ll<<i)) {
if(cntmx==1) {
if(cntse==1) mx^=(1llu<<i), se^=(1llu<<i);
else mx^=(1llu<<i), ans=ans*((se^(1llu<<i))%M)%M, cntse--;
}
else if(cntmx==2) mx^=(1llu<<i);
else cntmx-=2, ans=ans*qpow(se,cntse)%M, se=mx^(1llu<<i), cntse=2;
}
}
ans=ans*qpow(mx,cntmx)%M*qpow(se,cntse)%M;
cout<<ans<<'\n';
return;
}
int main() {
int T=rd();
while(T--) {
n=rd(), a=rd(), b=rd(), c=rd();
ATRI();
}
return 0;
}
:::