QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#521554#5045. KingAndyqian7WA 3ms14032kbC++172.0kb2024-08-16 12:06:172024-08-16 12:06:21

Judging History

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

  • [2024-08-16 12:06:21]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:14032kb
  • [2024-08-16 12:06:17]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define rep(i, s, e) for (int i = s; i <= e; i++)
using namespace std;
const int MAX = 2e5 + 10;
int n, p, b[MAX], ib[MAX], fa[MAX], len[MAX];
vector<int> sons[MAX];
int Find(int x)
{
    return fa[x] == x ? x : (fa[x] = Find(fa[x]));
}
int fp(int x, int k)
{
    if (!k)
        return 1;
    if (k & 1)
        return x * fp(x, k - 1) % p;
    int tmp = fp(x, k >> 1);
    return tmp * tmp % p;
}
int inv(int x)
{
    return fp(x, p - 2);
}
void dfs(int s)
{
    len[s] = 1;
    int m = 0;
    for (int son : sons[s])
    {
        dfs(son);
        m = max(m, len[son]);
    }
    len[s] += m;
}
int check(int q)
{
    int INV = inv(q);
    unordered_map<int, int> lst;
    rep(i, 1, n)
    {
        sons[i].clear(), len[i] = 0, fa[i] = i;
    }
    rep(i, 1, n)
    {
        if (lst.count(b[i] * INV % p) && !fa[lst[b[i] * INV % p]])
        {
            fa[lst[b[i] * INV % p]] = i;
            sons[i].push_back(lst[b[i] * INV % p]);
        }
        lst[b[i]] = i;
    }
    int ans = 0;
    rep(i, 1, n)
    {
        if (Find(i) == i)
        {
            dfs(i);
            ans = max(ans, len[i]);
        }
    }
    return ans;
}
signed main()
{
    ios::sync_with_stdio(0);
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> p;
        rep(i, 1, n) cin >> b[i], ib[i] = inv(b[i]);
        vector<int> v;
        rep(i, 1, n - 1) v.push_back(b[i + 1] * ib[i] % p);
        rep(i, 1, n - 2) v.push_back(b[i + 2] * ib[i] % p);
        sort(v.begin(), v.end());
        int ans = 0;
        for (int l = 0, r; l < v.size(); l = r + 1)
        {
            r = l;
            while (r < v.size() && v[r] == v[l])
            {
                r++;
            }
            r--;
            if ((r - l + 2) * 4 >= n)
                ans = max(ans, check(v[l]));
        }
        if (ans * 2 < n)
            cout << "-1" << endl;
        else
            cout << ans << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 14032kb

input:

4
6 1000000007
1 1 2 4 8 16
6 1000000007
597337906 816043578 617563954 668607211 89163513 464203601
5 1000000007
2 4 5 6 8
5 1000000007
2 4 5 6 7

output:

-1
-1
-1
-1

result:

wrong answer 1st numbers differ - expected: '5', found: '-1'