
考察gcd模板求解最大公约数。由于我是+2去做的,实际上当i=j=1的时候,能构成的分数只能是一种情况,所以最后的res需要减去1!!!
- #include
- using namespace std;
-
- int gcd(int a, int b){
- return b? gcd(b, a % b): a;
- }
-
-
- int main()
- {
- long long res = 0;
- for(int i = 1 ; i <= 2020; i ++)
- for(int j = i; j <= 2020; j ++)
- if(gcd(i, j) == 1)
- res += 2;
- cout << res - 1 << endl; // 注意1/1只能有一个,1/2和2/1可以有两个
- // 本题也可以j从1开始循环,每次符合要求res++
- return 0;
- }