紫书 例题 10-7 UVa10820 欧拉函数

本文介绍了一种使用欧拉函数和筛法高效求解1到n之间互质数对数量的方法,通过预处理减少时间复杂度至O(nloglogn),并提供了完整的C++实现代码。

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

题意:求1~n之间共有多少对互质的数
分析:如果用普通方法一个一个判断,时间复杂度是O(n^2),会超时。但是可以利用欧拉函数和筛法在O(nloglogn)时间内把50000内与每个数互质的正整数的个数求出来。当求有多少对时,只需令sum[n]*2 —1即可。
解题方法:

//
//Created by BLUEBUFF 2016/1/11
//Copyright (c) 2016 BLUEBUFF.All Rights Reserved
//

#pragma comment(linker,"/STACK:102400000,102400000")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
//#include <bits/stdc++.h>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstdlib>
#include <cstring>
#include <complex>
#include <sstream> //isstringstream
#include <iostream>
#include <algorithm>
using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, LL> pp;
#define REP1(i, a, b) for(int i = a; i < b; i++)
#define REP2(i, a, b) for(int i = a; i <= b; i++)
#define REP3(i, a, b) for(int i = a; i >= b; i--)
#define CLR(a, b)     memset(a, b, sizeof(a))
#define MP(x, y)      make_pair(x,y)
template <class T1, class T2>inline void getmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void getmin(T1 &a, T2 b) { if (b<a)a = b; }
const int maxn = 50010;
const int maxm = 1e5+5;
const int maxs = 10;
const int maxp = 1e3 + 10;
const int INF  = 1e9;
const int UNF  = -1e9;
const int mod  = 1e9 + 7;
const int rev = (mod + 1) >> 1; // FWT
const double PI = acos(-1);
//head

void phi_table(int n, int *phi){
    for(int i = 2; i <= n; i++) phi[i] = 0;
    phi[1] = 1;
    for(int i = 2; i <= n; i++){
        if(!phi[i]){
            for(int j = i; j <= n; j += i){
                if(!phi[j]) phi[j] = j;
                phi[j] = phi[j] / i * (i - 1);
            }
        }
    }
}

int phi[maxn], sum[maxn];
int main()
{
    phi_table(maxn - 1, phi);
    sum[0] = 0;
    for(int i = 1; i < maxn; i++) sum[i] = sum[i - 1] + phi[i];
    int n;
    while(scanf("%d", &n) != EOF && n)
    {
        printf("%d\n", 2 * sum[n] - 1);
    }
    return 0;
}

欧拉函数(Euler's Totient Function),也称为积性函数,是指小于等于正整数n的数中与n互质的数的个数。我们通常用φ(n)表示欧拉函数。 具体来说,如果n是一个正整数,那么φ(n)表示小于等于n的正整数中与n互质的数的个数。例如,φ(1)=1,因为1是唯一的小于等于1的正整数且1与1互质;φ(2)=1,因为小于等于2的正整数中只有1与2互质;φ(3)=2,因为小于等于3的正整数中与3互质的数是1和2。 欧拉函数的计算方法有很多,下面介绍两种常见的方法: 1. 分解质因数法 将n分解质因数,假设n的质因数分别为p1, p2, …, pk,则φ(n) = n × (1 - 1/p1) × (1 - 1/p2) × … × (1 - 1/pk)。例如,对于n=30,我们将其分解质因数得到30=2×3×5,则φ(30) = 30 × (1-1/2) × (1-1/3) × (1-1/5) = 8。 2. 筛法 我们可以使用筛法(Sieve)来计算欧拉函数。具体地,我们可以先将φ(1)至φ(n)全部初始化为其下标值,然后从2开始遍历到n,将所有能被当前遍历到的数整除的数的欧拉函数值减1即可。例如,对于n=6,我们先初始化φ(1)=1, φ(2)=2, φ(3)=3, φ(4)=4, φ(5)=5, φ(6)=6,然后从2开始遍历,将2的倍数的欧拉函数值减1,即φ(4)=φ(6)=2;然后遍历3,将3的倍数的欧拉函数值减1,即φ(6)=2。最终得到φ(1)=1, φ(2)=1, φ(3)=2, φ(4)=2, φ(5)=4, φ(6)=2。 欧拉函数在数论中有很重要的应用,例如RSA算法的安全性就基于欧拉函数的难解性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值