首页 > 其他分享 >Atcoder ABC091D Two Sequences

Atcoder ABC091D Two Sequences

时间:2024-07-03 15:44:43浏览次数:1  
标签:logV Atcoder xor int operatorname maxn Sequences ABC091D 进位

首先根据 \(\operatorname{xor}\),就想到拆成二进制的 \(a_{i, w}, b_{i, w}\) 来处理。

类似竖式加法,考虑把得到的结果 \(c_{w}\) 分为 \(a_{i, w} + b_{j, w} + x\),其中 \(x\) 就是上一位的进位。
进一步的,发现对于总的 \(c_{w}\),\(a_{i, w}, b_{j, w}\) 肯定都在这个位置加了 \(n\) 次,相当于是固定的。
于是就只需要考虑上一位往这一位进位多少次即可。

然后考虑如果 \(w\) 位向 \(w + 1\) 位进位有什么条件。
手玩一下能整理出来以下条件:

  1. 存在 \(p(p\le w)\),\(a_{i, p} = b_{j, p} = 1\),相当于首先要有进位。
  2. \(\forall x\in (p, w]\),\(a_{i, x} + b_{j, x} = 1\),相当于这个进位在向前传递。

对于条件 2,因为 \(a_{i, x} + b_{j, x} = 1\),就相当于是 \(a_{i, x} \operatorname{xor} b_{j, x} = 1\),那么就说明 \((p, w]\) 这些位 \(a_i, b_j\) 异或起来得到的就是 \(\operatorname{lim} = \sum\limits_{i = p + 1}^w 2^i\)。
那么就可以反过来用 \(\operatorname{lim}\operatorname{xor} a_i\) 去计数 \(b_j\) 了。

时间复杂度 \(\mathcal{O}(n\log^2 V)\),\(V\) 为值域。

计数的时候需要用个 Hash,常数有点大,需要卡卡常。

#include<bits/extc++.h>
const int maxn = 2e5 + 10, logV = 28;
int a[maxn][logV], b[maxn][logV];
int av[maxn], bv[maxn];
int c[logV + 1];
int main() {
   int n; scanf("%d", &n);
   for (int i = 1, x; i <= n; i++) {
      scanf("%d", &x);
      for (int w = 0; w < logV; w++) a[i][w] = x & (1 << w);
   }
   for (int i = 1, x; i <= n; i++) {
      scanf("%d", &x);
      for (int w = 0; w < logV; w++) b[i][w] = x & (1 << w);
   }
   for (int i = 1; i <= n; i++)
      for (int w = 0; w < logV; w++) c[w] ^= (a[i][w] && (n & 1)) << w;
   for (int i = 1; i <= n; i++)
      for (int w = 0; w < logV; w++) c[w] ^= (b[i][w] && (n & 1)) << w;
   std::unordered_map<int, int> mp;
   for (int j = 0; j < logV; j++) {
      memset(av, 0, sizeof(av)), memset(bv, 0, sizeof(bv));
      std::vector<int> A, B;
      for (int i = 1; i <= n; i++)
         if (a[i][j]) A.push_back(i);
      for (int i = 1; i <= n; i++)
         if (b[i][j]) B.push_back(i);
      c[j + 1] ^= ((A.size() & 1) && (B.size() & 1)) << j + 1;
      int lim = 0;
      for (int k = j + 1; k < logV; k++) {
         lim |= 1 << k;
         mp.clear();
         for (int i : A) mp[lim ^ (av[i] |= a[i][k])] ^= 1;
         int tot = 0;
         for (int i : B) tot ^= mp[bv[i] |= b[i][k]];
         c[k + 1] ^= tot << k + 1;
      }
   }
   int ans = 0;
   for (int i = 0; i <= logV; i++) ans += c[i];
   printf("%d\n", ans);
   return 0;
}

标签:logV,Atcoder,xor,int,operatorname,maxn,Sequences,ABC091D,进位
From: https://www.cnblogs.com/rizynvu/p/18281712

相关文章

  • Atcoder ARC090F Number of Digits
    记\(n\)为题面的\(S\)。能发现对于\(f(l)=8\),共有\(9\times10^7\)个数。此时就已经有\(8\times9\times10^7>10^8=n_{\max}\)了,就说明不存在\(f\ge8\)的情况,还满足这部分对应的数能全被选满。所以可以知道对于\(f(l)\ge8\)的情况,只存在\(f(r)-f(l)=......
  • AtCoder Beginner Contest 359 (A ~F)
    A-CountTakahashiQuestion:给你n个单词,要么是Takahashi,要么是Aoki;输出有几个Takahashi即可。Code:#include<bits/stdc++.h>usingnamespacestd;#defineendl'\n'#defineintlonglongtypedeflonglongll;typedefunsignedlonglongull;typedefpair<......
  • Atcoder ABC 360 全题解
    致歉对不起,我不应该在全题解的编写上咕咕咕两个月,导致流量大量流失。我知错了,下次还犯。AB无C考虑一个箱子里的所有球,我们需要把这些球放进互不相同的一些箱子里。然而我们可以留一个球在箱子里,显然留重量最大的最好,所以答案就是$\sum_{i=1}^{N}W_i$减去每个箱子里的最......
  • AtCoder Beginner Contest 360
    A-AHealthyBreakfast(abc360A)题目大意给定一个字符串包含RMS,问R是否在S的左边。解题思路比较R和S的下标,谁小即谁在左边。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=longlong;intmain(void){ios::sync_with_stdio(false);......
  • AtCoder Beginner Contest 359
    https://atcoder.jp/contests/abc359/tasksA-CountTakahashivoidsolve(){ intn; cin>>n; intans=0; while(n--){ strings; cin>>s; if(s=="Takahashi"){ ans++; } } cout<<ans<<endl;}B-......
  • AtCoder Beginner Contest 359
    AtCoderBeginnerContest359(3/6)A-CountTakahashiProblemStatementYouaregivenNNNstrings.Thei......
  • AtCoder Beginner Contest 359
    A-CountTakahashi(abc359A)题目大意给定\(n\)个字符串,问有多少个字符串是Takahashi解题思路注意判断比较即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=longlong;intmain(void){ios::sync_with_stdio(false);cin.tie(0);......
  • UNIQUE VISION Programming Contest 2024 Summer (AtCoder Beginner Contest 359)
    A-CountTakahashi(abc359A)解题思路遍历判断即可神奇的代码#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<map>#include<set>#include<cstring>usingnamespacestd;#defineendl'\n......
  • UNIQUE VISION Programming Contest 2024 Summer (AtCoder Beginner Contest 359) 题
    点我看题A-CountTakahashi没啥好说的点击查看代码#include<bits/stdc++.h>#definerep(i,n)for(inti=0;i<n;++i)#definerepn(i,n)for(inti=1;i<=n;++i)#defineLLlonglong#definefifirst#definesesecond#definepbpush_back#definemprmake_pair......
  • AtCoder Beginner Contest 359 解题报告
    AtCoderBeginnerContest359吐槽:A-F还算正常,G题你tm给我放了个出过的板子(ABC340G)是几个意思啊???ASimulate.#include<bits/stdc++.h>usingnamespacestd;#definelllonglong#defineendl'\n'#definePBemplace_back#definePPBpop_back#defineMPmake_pai......