[CF1114E] Arithmetic Progression

[CF1114E] Arithmetic Progression

Description

有一个长度为 n 的序列 a,从小到大排序后是一个等差数列。? i 询问 ai 的值,> x 询问序列中是否存在严格大于 x 的数,要求出首项和公差。询问个数不超过 60。

Solution

用第二种操作二分求出序列最大值,再用第一种操作随机询问若干个数,求 gcd 得到公差,由此求出首项和公差

#include <bits/stdc++.h>
using namespace std;

signed main()
{
    srand(20020128);
    int n;
    cin >> n;
    int l = 0, r = 1e9;
    while (l < r)
    {
        int mid = (l + r) / 2;
        cout << "> " << mid << endl;
        cout.flush();
        int x;
        cin >> x;
        if (x)
            l = mid + 1;
        else
            r = mid;
    }
    int max_item = l;
    int delta = 0;
    for (int i = 1; i <= 30; i++)
    {
        cout << "? " << rand() * rand() % n + 1 << endl;
        cout.flush();
        int x;
        cin >> x;
        delta = __gcd(delta, max_item - x);
    }
    int min_item = max_item - delta * (n - 1);
    cout << "! " << min_item << " " << delta << endl;
}
原文地址:https://www.cnblogs.com/mollnn/p/14398574.html