首页 > 其他分享 >F - Product Equality

F - Product Equality

时间:2024-02-04 21:33:25浏览次数:14  
标签:Product Equality int bmod h1 times Sample h2

F - Product Equality

Problem Statement

You are given $N$ integers $A_1, A_2, \dots, A_N$.
Find the number of triples of integers $(i, j, k)$ that satisfy the following conditions:

  • $1 \le i, j, k \le N$
  • $A_i \times A_j = A_k$

Constraints

  • $1 \le N \le 1000$
  • $\color{red}{1 \le A_i < 10^{1000}}$

Input

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

$N$
$A_1$
$A_2$
$\vdots$
$A_N$

Output

Print the answer as an integer.


Sample Input 1

5
2
3
6
12
24

Sample Output 1

6

The following six triples $(i, j, k)$ satisfy the conditions in the problem statement:

  • $(1, 2, 3)$
  • $(1, 3, 4)$
  • $(1, 4, 5)$
  • $(2, 1, 3)$
  • $(3, 1, 4)$
  • $(4, 1, 5)$

Sample Input 2

11
1
2
3
4
5
6
123456789123456789
123456789123456789
987654321987654321
987654321987654321
121932631356500531347203169112635269

Sample Output 2

40

Note that the values of each integer $A_i$ can be huge.


Sample Input 3

9
4
4
4
2
2
2
1
1
1

Sample Output 3

162

Note that there may be duplicates among the values of $A_i$.

 

解题思路

  如果 $a_i$ 比较小的话,由于 $n$ 最大只有 $1000$,因此直接暴力枚举 $a_i$ 和 $a_j$,然后在哈希表中查有多少个数等于 $a_i \times a_j$,答案累加这个数目。如果 $a_i$ 变成题目中的约束,哪怕用 FFT 也很明显会超时。

  然后很 trick 的地方是对运算取模。显然如果满足 $a_i \times a_j = a_k$,那么必然有 $(a_i \bmod m) \times (a_j \bmod m) = a_k \bmod m$。但反过来如果 $(a_i \bmod m) \times (a_j \bmod m) = a_k \bmod m$ 则不一定有 $a_i \times a_j = a_k$,如果模数 $m$ 比较小,数据量比较大,那么这个冲突会更明显。

  所以这里采用双哈希的做法,取两个 int 范围内尽可能大的质数 $m_1$ 和 $m_2$,然后对 $a_i$ 通过秦九韶算法分别求出模 $m_1$ 和 $m_2$ 的结果 $h_1$ 与 $h_2$,用二元组 $(h_1, h_2)$ 来表示 $a_i$ 的哈希值。

  ps:一开始我取 $m_1 = 10^9+7$ 和 $m_2 = 998244353$ 结果 WA 了 1 个数据 qwq,所以模数尽量不要取一些特殊值,防止被特殊数据卡。当然用多重哈希的话会更保险。

  AC 代码如下,时间复杂度为 $O(n \log{A} + n^2 \log{n})$:

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

typedef long long LL;

const int N = 1010, m1 = 999999937, m2 = 998244353;

char s[N];
int h1[N], h2[N];

LL get(int x, int y) {
    return 1ll * x * m2 + y;
}

int main() {
    int n;
    scanf("%d", &n);
    map<LL, int> mp;
    for (int i = 0; i < n; i++) {
        scanf("%s", s);
        for (int j = 0; s[j]; j++) {
            h1[i] = (h1[i] * 10ll + s[j] - '0') % m1;
            h2[i] = (h2[i] * 10ll + s[j] - '0') % m2;
        }
        mp[get(h1[i], h2[i])]++;
    }
    LL ret = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int t1 = 1ll * h1[i] * h1[j] % m1;
            int t2 = 1ll * h2[i] * h2[j] % m2;
            ret += mp[get(t1, t2)];
        }
    }
    printf("%lld", ret);
    
    return 0;
}

 

参考资料

  AtCoder Beginner Contest 339:https://www.cnblogs.com/2huk/p/18005328

  AtCoder Beginner Contest 339 A-G:https://zhuanlan.zhihu.com/p/681350652

标签:Product,Equality,int,bmod,h1,times,Sample,h2
From: https://www.cnblogs.com/onlyblues/p/18007036

相关文章

  • [office] excel sumproduct函数如何多条件求和
    1.打开含有模拟数据的EXCEL工作表;2.为工作方便,我们先将数据区定义名称,选中F2:F22,将名称定义为“销售额”;3.将E2:E22定义为“商品数据”;4.将D2:D22定义为“区域数据”;5.将C2:C22定义为“项目数据”;相关推荐:《excel基础教程》6.将B2:B22定义为“姓名数据”;7.将A2:A22定义为“月数据......
  • ABC339 F Product Equality 题解
    QuestionABC339FProductEquality给出一个序列\(A_1,A_2,\cdots,A_N\)计算数对\((i,j,k)\)满足\(A_i\timesA_j=A_k\)的个数\(A_i\le10^{1000}\)Solution思考\(A_i\)比较小的情况如果\(A_i\le1e9\)的,暴力枚举\(i,j\)然后用\(map\)查找\(A_i\timesA_j......
  • IEqualityComparer接口实现对象去重
    //Licensedtothe.NETFoundationunderoneormoreagreements.//The.NETFoundationlicensesthisfiletoyouundertheMITlicense.//SeetheLICENSEfileintheprojectrootformoreinformation.usingSystem;usingSystem.Collections;usingSyste......
  • 既可以通过从层次结构更高层组件(如 FilterableProductTable)开始“自上而下”构建,也可
    既可以通过从层次结构更高层组件(如FilterableProductTable)开始“自上而下”构建,也可以通过从更低层级组件(如ProductRow)“自下而上”进行构建。在简单的例子中,自上而下构建通常更简单;而在大型项目中,自下而上构建更简单。为什么这么说呢?合理吗?在构建React应用时,"自上而下"(Top-Do......
  • SciTech-Math-AdvancedAlgebra-Dot Product + Linear Equations And Inverse Matrices
    LinearEquationsAndInverseMatrices:https://math.mit.edu/~gs/dela/dela_4-1.pdfDotProduct:Theotherimportantoperationonvectorsisakindofmultiplication.Thisisnotordinarymultiplicationandwedon'twritevw.Theoutputfromvandwwi......
  • abc132F - Small Products
    abc132F-SmallProducts容易想到暴力dp,f[i][j]表示到第i个位置,且i位置上填的是j的方案数。虽然N非常大,但是如果我们考虑按\(\frac{n}{k}\)的值分块,那么就只有根号级别的数量\(f[i][j]\)表示在到第i个位置,且第i个位置选了第j个块中的数的方案数,那么所有能转移到第j个块的就是t......
  • 计算2个向量的夹角 dot product
    vectors-dot-product/*https://www.mathsisfun.com/algebra/vectors-dot-product.html*/functionVector3(x,y,z){ this.x=x; this.y=y; this.z=z;}Vector3.prototype.magnitude=function(){ returnMath.sqrt(this.x*this.x+this.y*this.y+this.z......
  • [ABC239Ex] Dice Product 2 题解
    原题链接:ABC239Ex。题意不多赘述。看到求期望值,我们想到可以用期望DP。设\(dp_{i}\)表示最终结果大于等于\(i\)时的操作次数的期望值。那么我们可以得到一个基本的状态转移方程:\(dp_{i}=\frac{1}{n}\times\sum_{j=1}^{n}dp_{\left\lceil\frac{i}{j}\right\rceil}+......
  • SAAS Product Edition | Profile
    *[AdobeCreativeCloud常见问题解答|中国](https://helpx.adobe.com/cn/creative-cloud/faq-china.html)Adobe为个人、公司和企业提供不同计划:对于个人而言,CreativeCloud摄影计划是目前在中国推出的唯一一项计划。对于中小型企业而言,我们提供CreativeCloud团队版。......
  • SAP CRM 和 ERP 系统之间的主数据同步 - PRODUCT_R3_ADAPTER
    SAPCRM系统中的Middleware是一个关键的集成组件,它允许CRM系统和其他SAP或非SAP系统交换和同步数据。Middleware提供了一种机制,允许在异构系统环境中实现数据和业务过程的一致性。在这个上下文中,PRODUCT_R3_ADAPTER是一种特定的Middleware组件,它主要负责处理CRM系......