首页 > 其他分享 >F - Oddly Similar

F - Oddly Similar

时间:2024-04-08 15:48:07浏览次数:10  
标签:27 int ++ Oddly leq bitset Similar ldots

F - Oddly Similar

Problem Statement

There are $N$ sequences of length $M$, denoted as $A_1, A_2, \ldots, A_N$. The $i$-th sequence is represented by $M$ integers $A_{i,1}, A_{i,2}, \ldots, A_{i,M}$.

Two sequences $X$ and $Y$ of length $M$ are said to be similar if and only if the number of indices $i (1 \leq i \leq M)$ such that $X_i = Y_i$ is odd.

Find the number of pairs of integers $(i,j)$ satisfying $1 \leq i < j \leq N$ such that $A_i$ and $A_j$ are similar.

Constraints

  • $1 \leq N \leq 2000$
  • $1 \leq M \leq 2000$
  • $1 \leq A_{i,j} \leq 999$
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$N$ $M$
$A_{1,1}$ $A_{1,2}$ $\ldots$ $A_{1,M}$
$A_{2,1}$ $A_{2,2}$ $\ldots$ $A_{2,M}$
$\vdots$
$A_{N,1}$ $A_{N,2}$ $\ldots$ $A_{N,M}$

Output

Print the answer as an integer.


Sample Input 1

3 3
1 2 3
1 3 4
2 3 4

Sample Output 1

1

The pair $(i,j) = (1,2)$ satisfies the condition because there is only one index $k$ such that $A_{1,k} = A_{2,k}$, which is $k=1$.

The pairs $(i,j) = (1,3), (2,3)$ do not satisfy the condition, making $(1,2)$ the only pair that does.


Sample Input 2

6 5
8 27 27 10 24
27 8 2 4 5
15 27 26 17 24
27 27 27 27 27
27 7 22 11 27
19 27 27 27 27

Sample Output 2

5

 

解题思路

  纯暴力题,开优化甚至能在时限内跑 $O(n^2m)$ 的暴力。正解是用 std::bitset 优化到 $O\left(nm\frac{n}{w}\right)$。

  最暴力的做法是枚举当前行 $i$ 和之前行 $k < i$,然后比较两行的元素。对于某一列 $j$,可以发现只有 $a_{k,j} = a_{i,j}$ 的行 $k$ 才会与第 $i$ 行有相同的元素的贡献。所以对于第 $i$ 行,当枚举到第 $j$ 列时,我们希望能快速知道前面有哪些行 $k$ 的 $a_{k,j} = a_{i,j}$。

  只需开一个 std::bitset<n> 数组 $st[j][x]$,表示前 $i$ 行中,第 $j$ 列有哪些行的值为 $x$,这些行用状态压缩来表示。因此在枚举到第 $i$ 行时,用一个 std::bitset<n> 变量 $t$ 来维护第 $i$ 行与之前每一行相同元素数量的奇偶性,$t$ 中的第 $k$ 位表示第 $i$ 行与第 $k$ 行中相同元素数量的奇偶性。枚举每一列 $j$,并令 $t \oplus st[j][a_{i,j}]$。最后 $t$ 中 $1$ 的数量就是第 $i$ 行与之前相同元素数量为奇数的行的数量。

  AC 代码如下,时间复杂度为 $O\left(\frac{n^2m}{w}\right)$:

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

typedef long long LL;

const int N = 2005, M = 1005;

int a[N][N];
bitset<N> st[N][M];

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%d", &a[i][j]);
        }
    }
    int ret = 0;
    for (int i = 0; i < n; i++) {
        bitset<N> t;
        for (int j = 0; j < m; j++) {
            t ^= st[j][a[i][j]];
        }
        ret += t.count();
        for (int j = 0; j < m; j++) {
            st[j][a[i][j]][i] = 1;
        }
    }
    printf("%d", ret);
    
    return 0;
}

 

参考资料

  AtCoder Beginner Contest 348:https://www.cnblogs.com/Lanly/p/18118195

标签:27,int,++,Oddly,leq,bitset,Similar,ldots
From: https://www.cnblogs.com/onlyblues/p/18121306

相关文章

  • Paper Content Similarity Detection
    PaperContentSimilarityDetectiongitcode项目地址:https://gitcode.com/2301_78305256/PaperContentSimilarityDetection/tree/masterPSP表格PSP2.1PersonalSoftwareProcessStages预估耗时(分钟)实际耗时(分钟)Planning计划2020·Estimate·估计这个任......
  • GRLSTM:基于图的残差LSTM轨迹相似性计算《GRLSTM: Trajectory Similarity Computation
    2023年10月18日,14:14。来不及了,这一篇还是看的翻译。论文:GRLSTM:TrajectorySimilarityComputationwithGraph-BasedResidualLSTM(需要工具才能访问)Github: AAAI2023的论文。 摘要轨迹相似性的计算是许多空间数据分析应用中的一项关键任务。然而,现有的方法主要是......
  • 1360C - Similar Pairs
    C.SimilarPairshttps://codeforces.com/problemset/problem/1360/C"""思路:1.因为n为偶数,所以偶数如果有偶数个的话,那么奇数也有偶数个,正好可以两两配对2.如果偶数为奇数个,那么奇数也是有奇数个,内部消化后多一个奇数和偶数3.剩下的奇数和偶数按相差是1计算,如......
  • How to use Javascript JSON.stringify similar method in Python All In One
    HowtouseJavascriptJSON.stringifysimilarmethodinPythonAllInOne如何在Python中使用类似JavaScriptJSON.stringify的方法应用场景比较两个数组(列表)对象是否相等/comparestwoarray(list)objectsforequality//jsarr1=[1,2,3]arr2=[1,2,3]......
  • LEA: Improving Sentence Similarity Robustness to Typos Using Lexical Attention B
    LEA:ImprovingSentenceSimilarityRobustnesstoTyposUsingLexicalAttentionBias论文阅读KDD2023原文地址Introduction文本噪声,如笔误(Typos),拼写错误(Misspelling)和缩写(abbreviations),会影响基于Transformer的模型.主要表现在两个方面:Transformer的架......
  • CosineSimilarity
    余弦相似度implementation'org.apache.commons:commons-text:1.10.0'MeasurestheCosinesimilarityoftwovectorsofaninnerproductspaceandcomparestheanglebetweenthem.ForfurtherexplanationabouttheCosineSimilarity,refertohttp://en.......
  • CF1817C Similar Polynomials 题解
    可能更好的阅读体验题目传送门Div.1C拉格朗日差值,赛时开香槟。题目大意给定\(d\)次两个多项式\(A(x),B(x)\)。求\(s\),使得\(B(x)\equivA(x+s)\pmod{10^9+7}\),保证\(s\)存在。给出多项式的形式为给出\(x=0,1,\cdots,d\)模\(10^9+7\)的值。\(d\le2.5\times......
  • 迁移学习(SPI)《Semi-Supervised Domain Adaptation by Similarity based Pseudo-label
    论文信息论文标题:Semi-SupervisedDomainAdaptationbySimilaritybasedPseudo-labelInjection论文作者:AbhayRawat, IshaDua, SauravGupta, RahulTallamraju 论文来源:PublishedinECCVWorkshops5September2022论文地址:download 论文代码:download视屏讲解:click......
  • Improving Zero-Shot Coordination Performance Based on Policy Similarity 2023-
    基于策略相似度的零样本协调表现改进总结:这篇论文本质上是研究智能体的泛化性能,文中涉及的问题是在一个常规多智能体系统中的智能体如果要与新加入的或者说没有交互过的......
  • 兼容oracle的edit_distance_similarity 比较两个字符串相似度
    瀚高数据库目录环境症状问题原因解决方案报错编码环境系统平台:Linuxx86RedHatEnterpriseLinux6版本:4.5.7症状在进行应用适配过程中会遇到用户使用oracle的SYS.UTL_MAT......