0
点赞
收藏
分享

微信扫一扫

873. 欧拉函数

玩物励志老乐 2022-01-22 阅读 94

873. 欧拉函数

给定 n 个正整数 ai,请你求出每个数的欧拉函数。

欧拉函数的定义

1∼N 中与 N 互质的数的个数被称为欧拉函数,记为 ϕ(N)。
若在算数基本定理中,N=pa11pa22…pamm,则:
ϕ(N) = N×p1−1p1×p2−1p2×…×pm−1pm
输入格式
第一行包含整数 n。

接下来 n 行,每行包含一个正整数 ai。

输出格式

输出共 n 行,每行输出一个正整数 ai 的欧拉函数。

数据范围

1≤n≤100,
1≤ai≤2×109

输入样例:

3
3
6
8

输出样例:

2
2
4

B14795A834246EE83DE49F13AED79FF1.png

16E3D970E29CD784C881636CEE45942C.png

代码:

#include <bits/stdc++.h>

using namespace std;
int phi(int x)
{
    int res = x;
    for (int i = 2; i <= x / i; i++)
    {
        if (x % i == 0)
        {
            res = res / i * (i - 1);
            while (x % i == 0)
                x /= i;
        }
    }
    if (x > 1)
        res = res / x * (x - 1);
    return res;
}
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        int temp;
        cin >> temp;
        cout << phi(temp) << endl;
    }
    return 0;
in >> temp;
        cout << phi(temp) << endl;
    }
    return 0;
}
举报

相关推荐

0 条评论