51NOD 1040 1040 最大公约数之和 数论 欧拉函数

本文详细解析了求解1至n各数与n的最大公约数之和的问题,通过枚举和利用欧拉函数进行优化,提供了一个高效的算法实现。
部署运行你感兴趣的模型镜像
1040 最大公约数之和
题目来源: rihkddd
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏  关注
给出一个n,求1-n这n个数,同n的最大公约数的和。比如:n = 6
1,2,3,4,5,66的最大公约数分别为1,2,3,2,1,6,加在一起 = 15
Input
1个数N(N <= 10^9)
Output
公约数之和
Input示例
6
Output示例
15

设整数d,满足n%d=0;
gcd(n,k)=d —> nx + ky = d —-> n/d * x + k/d *y = 1
显然 对任意k<=n,满足gcd(n/d,k/d)=1的k的个数 就是gcd(n,k)=d的k个数
而这个k的个数就等于phi(n/d) (n的欧拉函数)
所以枚举d,将d*phi(n/d)累加就是最终答案


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

void caulate_factor(int n,vector<int>&factor){//分解n的质因子
    factor.clear();
    for(int i=2,end=sqrt(n+1);i<=end;++i){
        if(n%i==0){
            factor.push_back(i);
            while(n%i==0){
                n/=i;
            }
        }
        if(i>=n)
            break;
    }
    if(n!=1)
        factor.push_back(n);
}

ll phi(ll n,vector<int>&factor){
    ll ans=n;
    for(int i=0;i<factor.size();++i){
        ans=ans*(factor[i]-1)/factor[i];
    }
    return ans;
}

ll slove(int n){
    ll ans=0;
    vector<int>factor_fac;
    for(int i=1,end=sqrt(n);i<=end;++i){//枚举d 
        if(n%i==0){
            int factor=i;
            caulate_factor(n/factor,factor_fac);
            ans+=factor*phi(n/factor,factor_fac);
            if(n/i>end){
                factor=n/i;
                caulate_factor(n/factor,factor_fac);
                ans+=factor*phi(n/factor,factor_fac);
            }
        }
    }
    return ans;
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int n;
    while(~scanf("%d",&n)){
        printf("%lld\n",slove(n));
    }
    return 0;
}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.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('inf') # 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、付费专栏及课程。

余额充值