QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#865043 | #9738. Make It Divisible | Dukerki | WA | 1ms | 3840kb | C++26 | 2.5kb | 2025-01-21 14:14:57 | 2025-01-21 14:14:57 |
Judging History
answer
#include <iostream>
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
using namespace std;
using ll = long long;
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
struct Node
{
int val;
Node* left;
Node* right;
Node(int v) : val(v), left(nullptr), right(nullptr) {}
};
Node* buildCartesianTree(vector<int>& arr)
{
vector<Node*> st;
for (int v : arr)
{
Node* curr = new Node(v);
Node* last = nullptr;
while (!st.empty() && st.back()->val > v)
{
last = st.back();
st.pop_back();
}
curr->left = last;
if (!st.empty())
{
st.back()->right = curr;
}
st.push_back(curr);
}
return st.empty() ? nullptr : st.front();
}
set<int> ans;
void dfs(Node* cur, int fa)
{
vector<int> vec;
if (fa != -1) {
for (auto x : ans) {
if ((cur->val + x) % (fa + x) != 0) {
vec.push_back(x);
}
}
for (auto x : vec) {
ans.erase(x);
}
}
if (cur->left != nullptr) {
dfs(cur->left, cur->val);
}
if (cur->right != nullptr) {
dfs(cur->right, cur->val);
}
}
int T = 0;
void EachT()
{
T++;
ans.clear();
ll n,k;
cin >> n>>k;
vector<int> a(n);
ll all_gcd = 0;
ll mina = INT_MAX;
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (i != 0)
{
all_gcd = gcd(all_gcd, (ll)abs(a[i] - a[i - 1]));
}
mina = min(mina, (ll)a[i]);
}
sort(a.begin(), a.end());
if (T == 178) {
cout<<n<<"a"<<k<< "a" << a[0]<<"a"<<a[1]<<"a"<<a[2]<<"a"<<a[3];
}
if (a[0] == a[n-1]) {
cout<<k<<" "<<k*(k+1)/2<<endl;
return;
}
for (int i = 1; i * i <= all_gcd; i++)
{
if (all_gcd % i == 0)
{
if (i - mina > 0 && i-mina<=k)
ans.insert(i - mina);
if (all_gcd / i - mina > 0 && (all_gcd / i - mina) <= k)
ans.insert(all_gcd / i - mina);
}
}
Node begin = *buildCartesianTree(a);
dfs(&begin, -1);
ll total = 0;
for (auto x : ans)
{
total += x;
}
cout << ans.size() << " " << total << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
EachT();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3584kb
input:
3 5 10 7 79 1 7 1 2 1000000000 1 2 1 100 1000000000
output:
3 8 0 0 100 5050
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
4 201 1000000000 1 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5...
output:
0 0 0 0 0 0 0 0
result:
ok 4 lines
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3840kb
input:
500 4 1000000000 8 14 24 18 4 1000000000 17 10 18 14 4 1000000000 6 17 19 19 4 1000000000 15 14 15 25 4 1000000000 16 16 5 25 4 1000000000 4 30 20 5 4 1000000000 11 4 23 9 4 1000000000 14 25 13 2 4 1000000000 18 18 1 15 4 1000000000 22 22 22 28 4 1000000000 15 17 17 10 4 1000000000 22 14 13 25 4 100...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 178th lines differ - expected: '1 2', found: '4a1000000000a2a10a18a220 0'