QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#865042#9738. Make It DivisibleDukerkiWA 1ms3712kbC++262.4kb2025-01-21 14:12:272025-01-21 14:12:27

Judging History

你现在查看的是最新测评结果

  • [2025-01-21 14:12:27]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3712kb
  • [2025-01-21 14:12:27]
  • 提交

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);
    }
}
void EachT()
{
    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 (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: 3712kb

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: 1ms
memory: 3584kb

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: 1ms
memory: 3712kb

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: '0 0'