QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#520497#5045. KingmanhaoWA 18ms3624kbC++203.7kb2024-08-15 13:59:132024-08-15 13:59:13

Judging History

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

  • [2024-08-15 13:59:13]
  • 评测
  • 测评结果:WA
  • 用时:18ms
  • 内存:3624kb
  • [2024-08-15 13:59:13]
  • 提交

answer

#include <bits/stdc++.h>
#define ppc __builtin_popcount
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define endl '\n'
#define lowbit(x) (x) & (-x)
#define pii pair<int, int>
#define all(s) s.begin(), s.end()
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define here system("pause")
using namespace std;
template <class T>
inline void read(T &x)
{
    x = 0;
    char c = getchar();
    bool f = 0;
    for (; !isdigit(c); c = getchar())
        f ^= (c == '-');
    for (; isdigit(c); c = getchar())
        x = (x << 3) + (x << 1) + (c ^ 48);
    x = f ? -x : x;
}
template <class T>
inline void write(T x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x < 10)
        putchar(x + 48);
    else
        write(x / 10), putchar(x % 10 + 48);
}

const int N = 2e6 + 5;
const int INF = 1e18;
int mod;

inline int pow(int a, int b, int p)
{
    int res = 1 % p;
    while (b)
    {
        if (b & 1)
            res = res * a % p;
        a = a * a % p, b >>= 1;
    }
    return res;
}
inline int inv(int a, int p) { return pow(a, p - 2, p) % p; }
int b[N]={0};

// 714285720
int n;
int flag=0;
int search(int idx, int num)
{ // cout<<idx<<"yes"<<endl;
    int cntt = 0;
    if (idx == n)
        return 0;
    int p1 = (b[idx + 1] * inv(b[idx], mod) )% mod;
    cntt += 2;
    for (int i = idx - 1; i >= 1; i--)
    { // if(idx==2)cout<<b[i] * p1 % mod<<"num="<<num<<endl;
        if ((b[i] * p1) % mod == num)
        {
            cntt++;
            num = b[i];
        }
    }
    num = b[idx + 1];
    for (int i = idx + 2; i <= n; i++)
    { // if(idx==2)cout<<num * p1 % mod<<"num="<<num<<endl;
        if ((num * p1) % mod == b[i])
        {
            cntt++;
            num = b[i];
        }
    }
    if (idx == n - 1)
    {
        if (cntt >= ceil(n * 1.0 / 2))
            flag = max(flag,cntt);
        return cntt >= ceil(n * 1.0 / 2);
    }
    int cnttt = 0;
    num = b[idx];
    int p2 = b[idx + 2] * inv(b[idx], mod) % mod;
    cnttt += 2;
    for (int i = idx - 1; i >= 1; i--)
    { // if(idx==2)cout<<b[i] * p1 % mod<<"num="<<num<<endl;
        if ((b[i] * p2) % mod == num)
        {
            cnttt++;
            num = b[i];
        }
    }
    num = b[idx + 2];
    for (int i = idx + 3; i <= n; i++)
    { // if(idx==2)cout<<num * p1 % mod<<"num="<<num<<endl;
        if ((num * p2)% mod == b[i])
        {
            cnttt++;
            num = b[i];
        }
    }

    cntt = max(cntt, cnttt);
    //if (cntt >= (n * 1.0 / 2))
        flag = max(flag,cntt);
    return cntt >= (n * 1.0 / 2);
}
void scan(int l, int r)
{
  
    if (l >= r)
        return;
    queue<pii> q;
    q.push({1, n});
    int cnt = 0;
    while (!q.empty() && cnt <= 400)
    {
        pii c = q.front();
        cnt++;
        q.pop();
        int mid = (c.first + c.second) >> 1;
       
            q.push({mid, r});
            q.push({l, mid});
        
        // else
        //     return;
        // if (flag != 0)
        //     return;
        // if (!search(r, b[r]))
        //     scan(mid + 1, r - 1);
    }
    return;
}
signed main()
{
    IOS;
    int t;
    cin >> t;
    // cout<<inv(7,1000000007)*5<<endl;
    // cout<<(714285720ll*7)%1000000007;
    while (t--)
    {

        cin >> n >> mod;
        for (int i = 1; i <= n; i++)
            cin >> b[i];
        if (n == 2)
        {
            cout << "2" << endl;
            continue;
        }
        flag = 0;
        if (n <= 40)
        {
            for (int i = 1; i <= n; i++)
                search(i, b[i]);
        }
        else
            scan(1, n);
        cout << ((flag <(n*1.0/2)) ? -1 : flag) << endl;
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3584kb

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:

5
-1
3
-1

result:

ok 4 number(s): "5 -1 3 -1"

Test #2:

score: -100
Wrong Answer
time: 18ms
memory: 3624kb

input:

1000
200 495189361
193302375 262009153 248101278 250233641 303504256 426913173 23261177 206011896 214770731 286184509 492688635 207979481 282629026 450810670 41818047 359796006 445343921 241742611 249404909 41291916 392252331 125287519 92825425 162555413 371172157 420486666 270651384 309213995 11709...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer 2nd numbers differ - expected: '133', found: '-1'