QOJ.ac
QOJ
QOJ is currently under a maintenance. It might be unavailable in the following a few hours.
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#842363 | #9078. Greatest Common Divisor | 1903331632 | AC ✓ | 154ms | 5868kb | C++23 | 1.7kb | 2025-01-04 12:00:51 | 2025-01-04 12:00:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int op;
vector<int> z;
bool st[N];
void init()
{
for (int i = 2; i < N; i++)
{
if (st[i])
continue;
z.push_back(i);
for (int j = i + i; j < N; j += i)
{
st[j] = 1;
}
}
}
void solve()
{
int n;
cin >> n;
vector<int> v(n);
int temp;
for (int i = 0; i < n; i++)
{
cin >> v[i];
if (i == 0)
temp = v[i];
else
temp = gcd(temp, v[i]);
}
if (temp > 1)
{
cout << "Case " << ++op << ": " << 0 << endl;
return;
}
sort(v.begin(), v.end());
auto it = std::unique(v.begin(), v.end());
v.erase(it, v.end());
if (v.size() == 1)
{
if (v[0] == 1)
cout << "Case " << ++op << ": " << 1 << endl;
else
cout << "Case " << ++op << ": " << 0 << endl;
return;
}
int gc;
for (int i = 1; i < v.size(); i++)
{
if (i == 1)
gc = v[i] - v[i - 1];
else
gc = gcd(gc, v[i] - v[i - 1]);
}
int res;
if (gc == 1)
res = -1;
else
{
for (int o : z)
{
if (gc % o == 0)
{
gc = o;
break;
}
}
res = gc - (v.back() % gc);
}
cout << "Case " << ++op << ": " << res << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init();
int t = 1;
cin >> t;
while (t--)
solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 5128kb
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: 5ms
memory: 5124kb
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: 154ms
memory: 5868kb
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