1 solutions

  • 0
    @ 2024-1-9 15:45:38

    本题考察初等数论的gcd算法。推荐使用欧几里得算法求最大公约数,也就是我们熟悉的辗转相除法。该算法相对简单,比赛容易出题,大家可以自行看书或者看b站视频教程学习。 对于python,直接导入math模块,调用里面的gcd()函数就可以求两个或者多个数的最大公约数。 C++可以自己手写一个gcd,代码很简单,如下:

    int gcd(int x, int y)
    {
         return !y ? x : gcd(y, x % y);
    }
    

    当然只要是linux的编译器就可以直接调用__gcd()函数,可以求两个数的gcd,注意前面是两个下划线。这个函数是GNU夹带的私货,他不属于任何一个C++标准库,它属于编译器自带的函数。 C++核心参考代码:

    int gcd(int x, int y)
    {
    	return !y ? x : gcd(y, x % y);
    }
    
    inline void solve()
    {
    	long long ans = 0;
    	for (int i = 1; i <= 2024; ++i)
    		for (int j = 1; j <= 2024; ++j)
    		{
    			if (gcd(i, j) == 1)
    				//或者 if (__gcd(i, j) == 1)
    				++ans;
    		}
    	cout << ans << '\n';
    }
    

    python参考代码:

    ans = 0
    for i in range(1, 2025):
        for j in range(1, 2025):
            from math import gcd
            if (gcd(i, j) == 1):
                ans += 1
    print(ans)
    

    答案为2491447

    • 1

    Information

    ID
    15240
    Time
    60000ms
    Memory
    256MiB
    Difficulty
    7
    Tags
    # Submissions
    22
    Accepted
    8
    Uploaded By