← 所有笔记

C. Common Divisors

C. Common Divisors codeforces 思维 gcd

题目传送门

找到思路就不难了,先找到最大公因数gcd,然后找到gcd的所有约数就是答案了

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
map<int, int>mp;
const int mod = 1e9 + 7;
int gcd(int a, int b)
{
    return b > 0 ? gcd(b, a % b) : a;
}

int main()
{
    int n;
    cin >> n;
    ll temp=1;
    for (int i = 1; i <= n; i++)
    {
        ll k;
        cin >> k;
        if (i == 1) temp = k;
        else
        {
            temp = gcd(temp, k);
        }
    }
    ll ans = 0;
    for (int i = 1; i <= temp / i; i++)
    {
        if (temp % i == 0)
        {
            if (temp / i != i) ans += 2;
            else ans += 1;
        }
    }
    cout << ans << endl;
    return 0;
}
✳

谢谢你读到这里。

更新于 2022-08-12

搜索笔记

搜索文章标题、标签和正文。

↑ ↓ 选择 · Enter 阅读Esc 关闭