首页 > 其他分享 >33. CF-Divisor Paths

33. CF-Divisor Paths

时间:2023-03-06 22:15:03浏览次数:49  
标签:Paths 33 ll CF ans vx vy fact mod

链接

求从 \(x\) 到 \(y\) 的最短路径的数量。

显然应该从 \(x\) 走到 \(\gcd(x, y)\) 再走到 \(y\),容易证明这样走是最优的。那么现在只需要把两段的最短路径数量分别求出来就行了。

最短路径数量随便排列组合算一下就好了。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e5 + 5;
const ll mod = 998244353;
ll qpow(ll b, ll k) {
    ll ret = 1;
    while (k) {
        if (k & 1) ret = ret * b % mod;
        b = b * b % mod;
        k /= 2;
    }
    return ret;
}
vector<ll> prime;
ll fact[maxn], ifact[maxn];

void init_fact(int n) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
        fact[i] = fact[i - 1] * i % mod;
    ifact[n] = qpow(fact[n], mod - 2);
    for (int i = n - 1; i >= 0; --i)
        ifact[i] = ifact[i + 1] * (i + 1) % mod;
}

void solve() {
    ll x, y;
    cin >> x >> y;
    ll ans = 1;
    ll g = __gcd(x, y);
    x /= g, y /= g;
    vector<int> vx, vy;
    for (auto p : prime) {
        int cntx = 0, cnty = 0;
        while (x % p == 0) {
            x /= p;
            cntx++;
        }
        while (y % p == 0) {
            y /= p;
            cnty++;
        }
        vx.push_back(cntx);
        vy.push_back(cnty);
    }
    ans = ans * fact[accumulate(vx.begin(), vx.end(), 0)] % mod;
    ans = ans * fact[accumulate(vy.begin(), vy.end(), 0)] % mod;
    for (auto i : vx) ans = ans * ifact[i] % mod;
    for (auto i : vy) ans = ans * ifact[i] % mod;
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    ll x = n;
    for (ll i = 2; i * i <= x; ++i) {
        if (x % i != 0) continue;
        prime.push_back(i);
        while (x % i == 0) x /= i;
    }
    if (x != 1) prime.push_back(x);
    init_fact(2333);
    int q;
    cin >> q;
    while (q--) {
        solve();
    }
}

标签:Paths,33,ll,CF,ans,vx,vy,fact,mod
From: https://www.cnblogs.com/theophania/p/p33.html

相关文章

  • CF 做题记录
    CF1784C弱化版就是将序列进行排序,设\(a\)的排名为\(k\),如果\(a<k\),就将\(a\)删除(后面的数排名也相应减一),否则将\(a-k\)加入到答案中。现在我们考虑每次加一个数,......
  • [Leetcode Weekly Contest]334
    链接:LeetCode[Leetcode]6307.递枕头n个人站成一排,按从1到n编号。最初,排在队首的第一个人拿着一个枕头。每秒钟,拿着枕头的人会将枕头传递给队伍中的下一个人。一旦......
  • ASP.NET Core 使用app.UseStaticFiles配置静态文件中间件,达到类似IIS中虚拟目录的效果
    1、项目中静态文件存放在wwwroot文件夹之下,如下:要访问nihao.jpg这个文件夹,url路径可以这样写:<imgsrc="~/images/inhao.jpg"alt="pic"/>wwwrootcssimagesnihao.jpgjs那......
  • 开源项目的演进会遇到哪些“坑”?KubeVela 从发起到晋级 CNCF 孵化的全程回顾
    作者:孙健波、曾庆国点击查看:「开源人说」第五期《KubeVela:一场向应用交付标准的冲锋》2023 年 2 月,**KubeVela[1]** 经过全体ToC投票成功进入CNCFIncubation,......
  • 字符串匹配【第二次CCF计算机软件能力认证】
    字符串匹配给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行。你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符;当选......
  • CF1787C - Remove the Bracket
    https://codeforces.com/problemset/problem/1787/CThisisthereasonwhytheproblemwasnamedasRemovetheBracket.\begin{aligned}\text{Product}&=a_1\cdo......
  • CF1741E - Sending a Sequence Over the Network
    https://codeforces.com/contest/1741/problem/ELet'sintroducethedynamics.\({\displaystyledp[i]=true}\)ifontheprefixiitheanswerisYes.Theninthis......
  • 33 openEuler使用LVM管理硬盘-管理逻辑卷
    33openEuler使用LVM管理硬盘-管理逻辑卷33.1创建逻辑卷可在root权限下通过lvcreate命令创建逻辑卷。#lvcreate[option]vgname其中:option:命令参数选项。常用的参......
  • LeetCode 周赛 335,纯纯手速场!
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。昨晚是LeetCode第335场周赛,你参加了吗?这场周赛整体难度不高,有两道模板题......
  • CF1738F
    傻题考虑一个点集\(S\),初始\(S=\{1,2,...n\}\)。考虑一个图\(G\)。每次取出\(S\)中度数最大的点\(x\),询问它的所有相连的点并且把这些点从\(S\)中删除,并且把它和这些点在......