

#include <bits/stdc++.h>
using namespace std;
//Plan2:自己写gcd函数
//int gcd(int a, int b){
//
// if(a % b == 0) return b;
// else return gcd(b, a % b);
//
//}
int main(){
int ans = 0;
for(int i = 1; i <= 2020; i++){
for(int j = 1; j <= 2020; j++){
if(__gcd(i,j) == 1){ //采用C++内置函数,记得包含算法头文件或万能头文件
ans++;
}
// if(gcd(i,j) == 1){ //Plan2:
// ans++;
// }
}
}
cout << ans << endl; //2481215
return 0;
}
该程序使用C++实现,通过遍历1到2020的所有整数对,计算其中互质(最大公约数为1)的数对数量。程序中使用了C++的__gcd内置函数来求两个数的最大公约数。
8474

被折叠的 条评论
为什么被折叠?



