QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#947094#9078. Greatest Common Divisorasaltfish#AC ✓107ms5012kbC++142.0kb2025-03-22 13:55:212025-03-22 13:55:21

Judging History

This is the latest submission verdict.

  • [2025-03-22 13:55:21]
  • Judged
  • Verdict: AC
  • Time: 107ms
  • Memory: 5012kb
  • [2025-03-22 13:55:21]
  • Submitted

answer

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
using namespace std;
using ll = long long;
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int T = 0;
void EachT()
{
    T++;
    cout << "Case " << T << ": ";
    int n;
    cin >> n;
    vector<ll> a(n), div(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    ll total = 0;
    for (int i = 1; i < n; i++)
    {
        div[i] = a[i] - a[i - 1];
        // cout<<div[i]<<' ';
        total = gcd(total, div[i]);
        // cout<<total<<'\n';
    }
    if (total == 0)
    {
        if (a[n - 1] == 1)
        {
            cout << 1 << '\n';
            return;
        }
        cout << 0 << '\n';
        return;
    }
    if (total == 1)
    {
        cout << "-1" << '\n';
    }
    else
    {
        ll ans = 1e10;
        for (int i = 2; i * i <= total; i++)
        {
            if (total % i == 0)
            {
                ll fac = i;
                if (a[0] % fac == 0)
                {
                    ans = 0;
                }
                else
                {
                    ans = min(ans, fac - a[0] % fac);
                }
                fac = total / i;
                if (a[0] % fac == 0)
                {
                    ans = 0;
                }
                else
                {
                    ans = min(ans, fac - a[0] % fac);
                }
            }
        }
        ll fac = total;
        if (a[0] % fac == 0)
        {
            ans = 0;
        }
        else
        {
            ans = min(ans, fac - a[0] % fac);
        }
        cout << ans << '\n';
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        EachT();
    }
    return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3712kb

input:

3
1
2
5
2 5 9 5 7
5
3 5 7 9 11

output:

Case 1: 0
Case 2: -1
Case 3: 1

result:

ok 9 tokens

Test #2:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

6
1
1
3
1 1 1
3
2 2 2
3
1 2 3
3
1 3 5
3
1 10 19

output:

Case 1: 1
Case 2: 1
Case 3: 0
Case 4: -1
Case 5: 1
Case 6: 2

result:

ok 18 tokens

Test #3:

score: 0
Accepted
time: 107ms
memory: 5012kb

input:

100
1
1
1
2
5
879961169 879961169 879961169 879961169 152615033
8
876139349 292671665 876139349 876139349 876139349 876139349 876139349 876139349
10
825359939 825359939 825359939 825359939 825359939 825359939 594330487 825359939 825359939 825359939
5
985688421 985688421 718069623 985688421 985688421...

output:

Case 1: 1
Case 2: 0
Case 3: 1
Case 4: 1
Case 5: 1
Case 6: 0
Case 7: 1
Case 8: -1
Case 9: -1
Case 10: 0
Case 11: 0
Case 12: 0
Case 13: 1
Case 14: 0
Case 15: 45
Case 16: 11
Case 17: 1
Case 18: -1
Case 19: -1
Case 20: 855585752
Case 21: 1982
Case 22: 260
Case 23: 0
Case 24: 0
Case 25: 0
Case 26: 0
Case...

result:

ok 300 tokens

Extra Test:

score: 0
Extra Test Passed