Product it again

本文介绍了一种求解特定范围内所有整数的质数幂乘积的算法,并提供了具体的C++实现代码。该算法利用了质数分布的特性,通过预处理得到所有质数,然后计算每个质数对该乘积的贡献。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:求解 $$\prod_{1 \leq i \leq n} \prod_{1 \leq j \leq m} {(i,j)}$$

 

解法:

满脑子的反演

考虑对于第一个质数 $p$ 的贡献为 $p^{[\frac{n}{p}][\frac{m}{p}] + [\frac{n}{p^2}][\frac{m}{p^2}] ... }$

这样1~n的质数大概有$O(\frac{n}{logn})$,对于每一个质数$O(logn)$,总效率大概为 $O(n)$

 

#include <iostream>
#include <cstdio>
#include <cstring>

#define LL long long
#define N 10000010
#define P 1000000007LL

using namespace std;

int n, m, tot, prime[N];
bool v[N];

LL qpow(LL x, LL n)
{
    LL ans = 1;
    for(; n; n >>= 1, x = x * x % P)
        if(n & 1)
            ans = ans * x % P;
    return ans;
}

int main()
{
    int T;
    for(int i = 2; i < N; i++)
        if(!v[i])
        {
            prime[++tot] = i;
            for(int j = i+i; j < N; j += i)
                v[j] = 1;
        }
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &n, &m);
        if(n > m) swap(n, m);
        LL ans = 1;
        for(int i = 1; i <= tot; i++)
            if(prime[i] <= n)
            {
                LL tmp = prime[i];
                LL cnt = 0;
                while(tmp <= n)
                {
                    cnt += (n/tmp) * (m/tmp);
                    tmp *= prime[i];
                }
                ans = ans * qpow(prime[i], cnt) % P;
            }
        printf("%lld\n", ans);
    }
}
View Code

 

转载于:https://www.cnblogs.com/lawyer/p/6761475.html

if (IsAutoPassEnabled) { // This means trader has not enabled the AutoPass previously, and is trying to enable AutoPass now. if (SelectedInvestmentManagers.IsNullOrEmpty() || SelectedProductGroups.IsNullOrEmpty()) return; var coverageStatusList = SelectedInvestmentManagers.Join(SelectedProductGroups, _ => true, _ => true, (mgr, grp) => { var coverageStatus = _directiveSession.CreateCoverageStatusEntity(); coverageStatus.TraderId = EnvironmentProvider.GetLoggedInPilotUserId(); coverageStatus.InvestorId = mgr.ToString(); coverageStatus.InstrumentClass = Enum.TryParse<InstrumentClass>(grp.ToString(), out var productGroup) ? productGroup : InstrumentClass.UNKNOWN; return coverageStatus; }).ToList(); try { var result = _directiveSession.StartAutoPass(coverageStatusList); if (result?.Success != true) { // The coverage update is failed, as TAP does not publish the coverage status when action failed, so just UI has to rollback the change by itself. // User has to adjust the coverage and re-submit it again. IsAutoPassEnabled = !IsAutoPassEnabled; } ShowProxyResult(result, nameof(OnEnableOrDisableAutoPass)); } catch (Exception ex) { ShowProxyResult(null, nameof(OnEnableOrDisableAutoPass)); Log.Error($"{ComponentID}|{Identifier}|OnEnableOrDisableAutoPass|StartAutoPass|Exception: {ex.Message}\nStackTrace: {ex.StackTrace}"); } } else { if (_coverageStatusList == null || _coverageStatusList.IsNullOrEmpty()) return; try { var result = _directiveSession.StopAutoPass(_coverageStatusList); if (result?.Success != true) { // The coverage update is failed, as TAP does not publish the coverage status when action failed, so just UI has to rollback the change by itself. // User has to adjust the coverage and re-submit it again. IsAutoPassEnabled = !IsAutoPassEnabled; } ShowProxyResult(result, nameof(OnEnableOrDisableAutoPass)); } catch (Exception ex) { ShowProxyResult(null, nameof(OnEnableOrDisableAutoPass)); Log.Error($"{ComponentID}|{Identifier}|OnEnableOrDisableAutoPass|StopAutoPass|Exception: {ex.Message}\nStackTrace: {ex.StackTrace}"); } } } finally { IsProcessingCoverage = false; } } 可以给我详细分析一下这段代码吗
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值