873. 欧拉函数

给定 \(n\) 个正整数 \(a_i\),请你求出每个数的欧拉函数。

欧拉函数的定义

\(1∼N\) 中与 \(N\) 互质的数的个数被称为欧拉函数,记为 \(ϕ(N)\)
若在算数基本定理中,\(N=p^{a_1}_1p^{a_2}_2…p^{a_m}_m\),则:
\(ϕ(N) = N×\frac{p_1−1}{p1}×\frac{p_2−1}{p_2}×…×\frac{p_m−1}{p_m}\)

输入格式

第一行包含整数 \(n\)

接下来 \(n\) 行,每行包含一个正整数 \(a_i\)

输出格式

输出共 \(n\) 行,每行输出一个正整数 \(a_i\) 的欧拉函数。

数据范围

\(1≤n≤100\),
\(1≤a_i≤2×10^9\)

输入样例:

3
3
6
8

输出样例:

2
2
4

解题思路

分解质因数求解欧拉函数

时间复杂度:\(O(T\times \sqrt{n})\)

代码

// Problem: 欧拉函数
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/875/
// Memory Limit: 64 MB
// Time Limit: 1000 ms

// %%%Skyqwq
#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

int n;
int phi(int n)
{
	int res=n;
	for(int i=2;i<=sqrt(n);i++)
	{
		if(n%i==0)
		{
			res=res/i*(i-1);
			while(n%i==0)n/=i;
		}
	}
	if(n>1)res=res/n*(n-1);
	return res;
}
int main()
{
	for(scanf("%d",&n);n;n--)
	{
		int x;
		scanf("%d",&x);
		printf("%d\n",phi(x));
	}
	return 0;
}
posted @ 2022-01-25 10:54  zyy2001  阅读(52)  评论(0编辑  收藏  举报