[51NOD1237]最大公约数之和 V3

本文介绍了一种高效算法,用于解决特定的数论问题——在给定的大整数范围内求解两数最大公约数(GCD)之和,并通过数学转换简化问题,最终采用杜教筛算法进行前缀和的快速计算。

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

题目大意

给定n,试求

i=1nj=1ngcd(i,j)

结果对109+7取模。

2n1010


题目分析

我们将题目改为求

i=1nj=1igcd(i,j)

然后将答案乘二再减去1n的和即可。
那么上面这条式子是什么呢?
i=1nj=1igcd(i,j)=d=1ndi=1ndφ(i)=d=1nφ(d)(1+nd)nd2

nd分块,对前面的欧拉函数使用杜教筛求前缀和。
时间复杂度?这个算起来有点麻烦,我来口胡一下。
假设我们预处理的部分大小为a
i=1nj=1iaOij+i=1nj=1niaO(nij)

显然右边部分的复杂度要大于左边,使用积分来计算:
i=1nj=1niaO(nij)O(n0(nax0nxy dy) dx)=O(n0nx1a)=O(1anlogn)

然后a可以在空间限制内尽量取大一点,我取了1.5×107

代码实现

我的代码莫名其妙常数巨大,卡着空间取的a让我卡着时限过了这题。。。

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

typedef long long LL;

const int P=1000000007;
const int itwo=500000004;
const int L=15000000;
const int N=100050;

int pri[L+5],phi[L+5],f[L+5],sum[L+5];
int S[N],vis[N];
bool mark[L+5];
int ans,l,s,cnt;
LL n;

void pre()
{
    s=trunc(sqrt(n)),l=L;
    mark[1]=1,sum[1]=phi[1]=1,f[1]=1;
    for (int i=2;i<=l;++i)
    {
        if (!mark[i]) pri[++pri[0]]=i,phi[i]=i-1,f[i]=i;
        for (int j=1,k;j<=pri[0];++j)
        {
            if (1ll*pri[j]*i>l) break;
            mark[k=pri[j]*i]=1,f[k]=pri[j];
            phi[k]=phi[i]*(pri[j]-(f[i]!=f[k]));
            if (!(i%pri[j])) break;
        }
        sum[i]=(sum[i-1]+phi[i])%P;
    }
}

int id(LL x){return n/x;}

int sieve(LL n)
{
    if (n<=l) return sum[n];
    int idn=id(n);
    if (vis[idn]==cnt) return S[idn];
    int res=1ll*((n+1)%P)*(n%P)%P*itwo%P;
    for (LL st=2,en,x;st<n;st=en)
    {
        x=n/st,en=n/x+1;
        res=(res-1ll*sieve(x)*(en-st)%P+P)%P;
    }
    return vis[idn]=cnt,S[idn]=res;
}

int main()
{
    freopen("gcdsumIII.in","r",stdin),freopen("gcdsumIII.out","w",stdout);
    scanf("%lld",&n),pre();
    ans=0;
    for (LL st=1,en,x;st<n;st=en)
    {
        x=n/st,en=n/x+1;
        ans=(ans+1ll*((x+1)%P)%P*(x%P)%P*itwo%P*(((++cnt,sieve(en-1))-(++cnt,sieve(st-1))+P)%P)%P)%P;
    }
    ans=(ans*2%P-1ll*((n+1)%P)*(n%P)%P*itwo%P+P)%P;
    printf("%d\n",ans);
    fclose(stdin),fclose(stdout);
    return 0;
}
题目 51nod 3478 涉及一个矩阵问题,要求通过最少的操作次数,使得矩阵中至少有 `RowCount` 行 `ColumnCount` 列是回文的。解决这个问题的关键在于如何高效地枚举所有可能的行列组合,并计算每种组合所需的操作次数。 ### 解法思路 1. **预处理每一行每一列变为回文所需的最少操作次数**: - 对于每一行,计算将其变为回文所需的最少操作次数。这可以通过比较每对对称位置的值是否相同来完成。 - 对于每一列,计算将其变为回文所需的最少操作次数,方法同上。 2. **枚举所有可能的行列组合**: - 由于 `N` `M` 的最大值为 8,因此可以枚举所有可能的行组合列组合。 - 对于每一种组合,计算其所需的最少操作次数,并取最小值。 3. **计算操作次数**: - 对于每一种组合,需要计算哪些行列需要修改,并且注意行列的交叉点可能会重复计算,因此需要去重。 ### 代码实现 以下是一个可能的实现方式,使用了枚举位运算来处理组合问题: ```python def min_operations_to_palindrome(matrix, row_count, col_count): import itertools N = len(matrix) M = len(matrix[0]) # Precompute the cost to make each row a palindrome row_cost = [] for i in range(N): cost = 0 for j in range(M // 2): if matrix[i][j] != matrix[i][M - 1 - j]: cost += 1 row_cost.append(cost) # Precompute the cost to make each column a palindrome col_cost = [] for j in range(M): cost = 0 for i in range(N // 2): if matrix[i][j] != matrix[N - 1 - i][j]: cost += 1 col_cost.append(cost) min_total_cost = float(&#39;inf&#39;) # Enumerate all combinations of rows and columns rows = list(range(N)) cols = list(range(M)) from itertools import combinations for row_comb in combinations(rows, row_count): for col_comb in combinations(cols, col_count): # Calculate the cost for this combination cost = 0 # Add row costs for r in row_comb: cost += row_cost[r] # Add column costs for c in col_comb: cost += col_cost[c] # Subtract the overlapping cells for r in row_comb: for c in col_comb: # Check if this cell is part of the palindrome calculation if r < N // 2 and c < M // 2: if matrix[r][c] != matrix[r][M - 1 - c] and matrix[N - 1 - r][c] != matrix[N - 1 - r][M - 1 - c]: cost -= 1 min_total_cost = min(min_total_cost, cost) return min_total_cost # Example usage matrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ] row_count = 2 col_count = 2 result = min_operations_to_palindrome(matrix, row_count, col_count) print(result) ``` ### 代码说明 - **预处理成本**:首先计算每一行每一列变为回文所需的最少操作次数。 - **枚举组合**:使用 `itertools.combinations` 枚举所有可能的行列组合。 - **计算成本**:对于每一种组合,计算其成本,并考虑行列交叉点的重复计算问题。 ### 复杂度分析 - **时间复杂度**:由于 `N` `M` 的最大值为 8,因此枚举所有组合的时间复杂度为 $ O(N^{RowCount} \times M^{ColCount}) $,这在实际中是可接受的。 - **空间复杂度**:主要是存储预处理的成本,空间复杂度为 $ O(N + M) $。 ### 相关问题 1. 如何优化矩阵中行列的枚举组合以减少计算时间? 2. 在计算列的交叉点时,如何更高效地处理重复计算的问题? 3. 如果矩阵的大小增加到更大的范围,如何调整算法以保持效率? 4. 如何处理矩阵中行列的回文条件不同时的情况? 5. 如何扩展算法以支持更多的操作类型,例如翻转某个区域的值?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值