首页 > 其他分享 >质因数+树形DP例题

质因数+树形DP例题

时间:2022-08-25 18:46:04浏览次数:40  
标签:cnt 质因数 return res LL num div 例题 DP

题目:

https://www.codechef.com/submit/MAKEIT1?tab=statement

题解:

https://www.codechef.com/submit/ROCKET_PACK?tab=solution

代码:

#include<bits/stdc++.h>
#include<unordered_set>
#define fore(x,y,z) for(LL x=(y);x<=(z);x++)
#define forn(x,y,z) for(LL x=(y);x<(z);x++)
#define rofe(x,y,z) for(LL x=(y);x>=(z);x--)
#define rofn(x,y,z) for(LL x=(y);x>(z);x--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

unordered_map<LL, unordered_map<LL, LL>> p_to_p;

LL MOD = 998244353;
LL dp[1000010];

unordered_set<LL> vis;
LL DP(LL num)
{
    
    if (dp[num]!=0)
        return dp[num];
    if (p_to_p.count(num) == 0)
    {
        dp[num] = 1;
        return 1;
    }
    LL res = 1;
    vis.insert(num);
    for (auto [nxt, cnt] : p_to_p[num])
    {
        if (vis.count(nxt))
            return -1;
        LL tmp = DP(nxt);
        if (tmp > 0)
        {
            res = (res + tmp * cnt % MOD) % MOD;
        }
        else
            return -1;
    }
    vis.erase(num);
    dp[num] = res;
    return res;

}
void YD()
{
    LL n, m; cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        LL a, b;
        cin >> a >> b;
        

        LL o = b;
        for (LL j = 2; j * j <= o; j++)
        {
            if (b % j == 0)
            {
                LL cnt = 0;

                while (b % j == 0)
                    b /= j, cnt++;
                p_to_p[a][j] += cnt;
                p_to_p[a][j] %= MOD;
            }
        }
        if (b != 1)
        {
            p_to_p[a][b] += 1;
            p_to_p[a][b] %= MOD;
        }


        

    }


    vector<LL> div;
    vector<LL> cnt;
    LL o = n;
    for (LL i = 2; i * i <= n; i++)
    {
        if (o % i == 0)
        {
            int c = 0;
            div.push_back(i);
            while (o % i == 0)
                o /= i, c++;
            c %= MOD;
            cnt.push_back(c);
        }
    }
    if (o != 1)
    {
        div.push_back(o);
        cnt.push_back(1);
    }

    unordered_map<LL, LL> div_cnt;
    for (int i = 0; i < div.size(); i++)
    {
        div_cnt[div[i]] = cnt[i];
    }

    LL res = 0;

    for (auto [aa, bb] : div_cnt)
    {
        vis.clear();
        LL num = DP(aa);
        if (num < 0)
        {
            cout << -1 << endl;
            return;
        }
        res += bb * num%MOD; 
        res %= MOD;
    }
        





    cout << res << endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T = 1;
    //cin >> T;
    while (T--)
    {
        YD();
    }
    return 0;
}
View Code

 

标签:cnt,质因数,return,res,LL,num,div,例题,DP
From: https://www.cnblogs.com/ydUESTC/p/16625345.html

相关文章

  • CountDownLatch+ThreadPool 优化统计报表
    一、功能要求业务方要求每天发一个统计日报到用户邮箱、业务为统计每日的多项市场指标数据,因为数据表中数据量庞大,每项指标的SQL是单独的逻辑,所以要在一个接口内执行多个S......
  • Sub 1G 433M 无线收发芯片DP4301兼容替代CC1101 智能抄表/无线游戏/无线门磁等应用
     近年来以470MHz中短距离、低功耗的无线通信技术)为代表的射频通信技术得到了抄表行业的广泛重视。470MHz无线通信技术以其低成本、低功耗、数据传输可靠性高、网络容......
  • 矩阵快速幂优化dp
    writtenon2022-08-24总结一下该算法适用题目类型以及一般方法。在碰到需要优化的dp时,这是一种思考方向。在往这方面思考时,要注重观察转移形式是否是基本一致的。以P3......
  • IfcGridPlacementDirectionSelect
    IfcGridPlacementDirectionSelect类型定义IfcGridPlacementDirectionSelect允许选择将网格放置定义为显式方向,或通过引用第二个网格交点来提供方向。 IFC4中的新选择......
  • [HNOI2004] L 语言 题解(AC 自动机上 dp)
    前言:原版数据超弱,爆搜就能过(即洛谷里面80分的数据),在此不多说,这里讲的是正解。(如果不是正解我还敢写题解吗)唔······话说洛谷里的题解用的都有状压,蒟蒻表示这题不......
  • 【开源】串口/蓝牙/TCP/UDP调试工具SerialTest
    可在Windows/Linux/Android上运行,功能丰富的调试工具。支持数据收发/实时绘图/快捷方式/文件收发功能。支持串口/蓝牙SPP客户端/蓝牙SPP服务器/蓝牙BLE客户端/TCP客户端/......
  • 浅谈数位DP
    动态规划,是OI中极其重要的一环。由于它的重要性,解决问题的广泛性,它衍生出了多种多样的DP。其中,有一种特别搞人的叫做数位DP思想数位DP是通过每一位数字去递推,来统计从......
  • 【luogu AT2366】Prefix Median(DP)
    PrefixMedian题目链接:luoguAT2366题目大意给你一个长度为2n-1的序列,你可以任意排序它们,问你有多少个不同的b数组。b数组的第i位为a数组1~2i-1区间的数的......
  • 【转载】Python(cx_oracle)的DPI-1047错误
    转自:https://blog.csdn.net/weixin_45158749/article/details/124800132 Python(cx_oracle)的DPI-1047错误步步FAN已于2022-05-1615:19:11修改981收藏文章标签:......
  • WPF中使用 WndProc 来处理Windows Messages
    WPF对应的C#程序有时需要与Window32程序进行通信,会使用到窗口过程函数来接受Windows消息。引入System.Windows.Interop命名空间,将使用到其中的HwndSource使用实例如......