太水了, 我都不忍心发题解, 但毕竟是sgu上一道题, 我试试能不能一直这么写下去,就是求phi,上代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 10010
using namespace std;int get_phi(int n)
{
int m = sqrt(n+0.5);
int ans = n;
for (int i = 2; i <= m; ++i)
if (n % i == 0)
{
ans = ans / i * (i-1);
while (n % i == 0) n /= i;
}
if (n > 1) ans = ans / n * (n-1);
return ans;
}int main()
{
int n;
scanf("%d", &n);
printf("%d\n", get_phi(n));
}
sgu 102 Coprimes
时间: 2024-11-29 06:20:04