CF1866B
2023-10-18 19:42:47 # OI # Problem

题面

Link

On the trip to campus during the mid semester exam period, Chaneka thinks of two positive integers XX and YY. Since the two integers can be very big, both are represented using their prime factorisations, such that:

  • X=A1B1×A2B2××ANBNX=A_1^{B_1}\times A_2^{B_2}\times\ldots\times A_N^{B_N} (each AiA_i is prime, each BiB_i is positive, and A1<A2<<ANA_1<A_2<\ldots<A_N)
  • Y=C1D1×C2D2××CMDMY=C_1^{D_1}\times C_2^{D_2}\times\ldots\times C_M^{D_M} (each CjC_j is prime, each DjD_j is positive, and C1<C2<<CMC_1<C_2<\ldots<C_M)

Chaneka ponders about these two integers for too long throughout the trip, so Chaneka’s friend commands her “Gece, deh!” (move fast) in order to not be late for the exam.

Because of that command, Chaneka comes up with a problem, how many pairs of positive integers pp and qq such that lcm(p,q)=X\operatorname{lcm}(p, q) = X and gcd(p,q)=Y\gcd(p, q) = Y. Since the answer can be very big, output the answer modulo 998244353998\,244\,353.

Notes:

  • lcm(p,q)\operatorname{lcm}(p, q) is the smallest positive integer that is simultaneously divisible by pp and qq.
  • gcd(p,q)\gcd(p, q) is the biggest positive integer that simultaneously divides pp and qq.

思路

显然,ppqqgcd\gcdlcm\operatorname{lcm} 满足如下性质:

fi(p)f_i(p) 表示 ppii 这个质因子的指数,SSppqq 的质因子集合的并集。

gcd(p,q)\gcd(p,q)ppqq 质因子的最小交集,即 iS,fi(gcd(p,q))=min(fi(p),fi(q))\forall i\in S,f_i(\gcd(p,q))=\min(f_i(p),f_i(q))

同理,iS,fi(lcm(p,q))=max(fi(p),fi(q))\forall i\in S,f_i(\operatorname{lcm}(p,q))=\max(f_i(p),f_i(q))

现在已知:lcm(p,q)=X,gcd(p,q)=Y\operatorname{lcm}(p,q)=X,\gcd(p,q)=Y,那么就有以下三种情况:

  • iS,fi(X)<fi(X)\exists i\in S,f_i(X)<f_i(X) 直接输出 0,因为不存在一组 p,qp,q 满足条件。
  • fi(X)=fi(Y)f_i(X)=f_i(Y),这个时候只有一种情况,即 fi(p)=fi(q)=fi(X)=fi(Y)f_i(p)=f_i(q)=f_i(X)=f_i(Y)
  • fi(X)>fi(Y)f_i(X)>f_i(Y),这个时候有两种情况:
    • fi(p)=fi(X),fi(q)=fi(Y)f_i(p)=f_i(X),f_i(q)=f_i(Y)
    • fi(p)=fi(Y),fi(q)=fi(X)f_i(p)=f_i(Y),f_i(q)=f_i(X)

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
typedef long long LL;
const int MOD = 998244353;

map<int, int> s1, s2; // X Y 中对应的指数
set<int> s;
int n, m, ans = 1;
int a[N], b[N], c[N], d[N];

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]), s.insert(a[i]);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &b[i]);
        s1[a[i]] = b[i];
    }
    scanf("%d", &m);
    for(int i = 1; i <= m; i++)
        scanf("%d", &c[i]), s.insert(c[i]);
    for(int i = 1; i <= m; i++)
    {
        scanf("%d", &d[i]);
        s2[c[i]] = d[i];
    }
    for(auto &i : s)
    {
        if(s1[i] < s2[i])
        {
            cout << 0 << endl;
            return 0;
        }
        else if(s1[i] == s2[i]) continue;
        else ans = ans * 2 % MOD;
    }
    cout << ans;
    return 0;
}