思路:
这题需要找1到n-1之间与n互为质数的数的个数,我们可以使用c++自带的__gcd()函数判断是否互为质数。
__gcd():返回最大公约数,当为1时表示互为质数。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;cin >> t;
while(t--){
int ans = 0;
int n;cin >> n;
for(int i = 1; i <= n; i++){
if (__gcd(i, n) == 1)ans++; // __gcd():返回最大公约数,当为1时表示互为质数
}
cout << ans << '\n';
}
return 0;
}
知识点:
__gcd()