51nod1238 最小公倍数之和V3

本文介绍了一种高效求解特定数学问题的方法,利用杜教筛算法优化了大数运算过程,并通过实例详细解释了算法的具体实现步骤。

题目:求ni=1nj=1lcm(i,j)(n<=1010)
先算一个东西。

C(n)=j=1ni[in]()

证:
C(n)=j=1ni[in]

反演一波
C(n)=g|nμ(g)i=1ngig

C(n)=g|nμ(g)gng(ng+1)2

把分数外边的g和分子上的g消掉,把n提出去。
C(n)=ng|nμ(g)(ng+1)2

括号里的两项拆开
C(n)=ng|nμ(g)ng2+ng|nμ(g)2

左边是经典的式子
C(n)=nφ(n)2+ng|nμ(g)2

右边只有n=1时有值
C(n)=nφ(n)+[n=1]2

就是说小于等于n的数中于n互质的和是A(n)。


A(i)=j=1ilcm(i,j)

A(i)=d|ij=1idij[idj]

A(i)=d|iij=1idj[idj]

带入C(i/d)
A(i)=d|iiidφ(id)+[id=1]2

把常数提出来,顺便交换d和i/d
A(i)=i2d|i(dφ(d)+[d=1])

ans=i=1nj=1nlcm(i,j)

ans=2i=1nj=1ilcm(i,j)i=1ni

把A(i)带入
ans=2i=1n(i2d|i(dφ(d)+[d=1]))i=1ni

2的常数就没了,然后看[d=1]的项,每一个i都只有一个,系数就是i,所以总共是ni=1i和后边抵消
ans=i=1nid|idφ(d)

ab=i,把d|i改写一下
ans=i=1niab=iaφ(a)

ans=i=1nab=ia2bφ(a)

枚举a,看有多少个b
ans=a=1na2φ(a)b=1nab

这样左边就可以配一个f(x)=x2杜教筛,右边进行分块。
#include <stdio.h>
#include <algorithm>

using namespace std;

//long long
const long long maxn = 5100000;
const long long mod = 1000000007;
const long long mod1 = 1000007;
const long long rev2 = 500000004;
const long long rev6 = 166666668;
struct link {
    long long x , y;
    link *next;
} pool[maxn] , *g[mod1 + 13];
long long top;
long long n , N;
long long p[maxn] , prime[maxn] , tot;
long long f[maxn];
long long ans;
void predo () {
    long long i , j;
    N = 4700000;
    f[1] = 1;
    for ( i = 2 ; i <= N ; i++ ) {
        if ( p[i] == 0 ) {
            prime[++tot] = i;
            f[i] = i - 1;
        }
        for ( j = 1 ; j <= tot ; j++ ) {
            if ( i * prime[j] > N ) break;
            p[i*prime[j]] = 1;
            f[i*prime[j]] = f[i] * (prime[j] - 1);
            if ( i % prime[j] == 0 ) {
                f[i*prime[j]] = f[i] * prime[j];
                break;
            }
        }
    }
    for ( i = 1 ; i <= N ; i++ ) {
        f[i] = (((f[i]*i)%mod)*i)%mod;
    }
    for ( i = 1 ; i <= N ; i++ ) f[i] = (f[i] + f[i-1]) % mod;
}
long long linsum ( long long x ) {
    long long ret;
    ret = ((x%mod)*((x+1)%mod))%mod;
    ret = (ret*rev2) % mod;
    return ret;
}
long long sqrsum ( long long x ) {
    long long ret;
    ret = ( (x%mod) * ((x+1)%mod) ) % mod;
    ret = (ret * ((2*x+1)%mod) ) % mod;
    ret = (ret * rev6) % mod;
    return ret;
}
long long get ( long long x ) {
    if ( x <= N ) return f[x];
    for ( link *j = g[x%mod1] ; j ; j = j -> next ) if ( j -> x == x ) return j -> y;
    long long l , r , ret = ((((x%mod)*((x+1)%mod))%mod)*rev2)%mod;
    ret = (ret*ret) % mod;
    for ( l = 2 ; l <= x ; l = r + 1 ) {
        r = x/(x/l);
        ret -= ( ((sqrsum(r)-sqrsum(l-1)+mod)%mod) * get ( x / l ) ) % mod;
        ret += mod;
        ret %= mod;
    }
    link *tmp = &pool[++top];
    tmp -> x = x; tmp -> y = ret; tmp -> next = g[x%mod1]; g[x%mod1] = tmp;
    return ret;
}
void work () {
    long long l , r;
    scanf ( "%lld" , &n );
    for ( l = 1 ; l <= n ; l = r + 1 ) {
        r = n/(n/l);
        ans += ( ((get(r)-get(l-1)+mod)%mod) * linsum (n/l) ) % mod;
        ans %= mod;
    }
    printf ( "%lld\n" , ans );
}
int main () {
    predo ();
    work ();
    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、付费专栏及课程。

余额充值