QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#488857#7956. Walk Swappingarnold518Compile Error//C++173.6kb2024-07-24 15:52:572024-07-24 15:52:58

Judging History

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

  • [2024-07-24 15:52:58]
  • 评测
  • [2024-07-24 15:52:57]
  • 提交

answer

#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("fma,avx,avx2")

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 3000;
const int INF = 1e9;

template<int MOD>
struct mint
{
    int x;
    mint() : x(0) {}
    mint(int _x) : x(_x) {}
    mint operator + (int ot) const { return x+ot>=MOD ? x+ot-MOD : x+ot; }
    mint operator - (int ot) const { return x<ot ? x+MOD-ot : x-ot; }
    mint operator - () const { return x ? MOD-x : 0; }
    mint operator * (int ot) const { return 1ll*x*ot%MOD; }
    mint operator += (int ot) { return *this = *this + ot; }
    mint operator -= (int ot) { return *this = *this - ot; }
    mint operator *= (int ot) { return *this = *this * ot; }
    operator int() const { return x; }
    bool operator < (int ot) const { return x<ot; }
};

template<int MOD>
mint<MOD> mpow(mint<MOD> a, ll b)
{
    mint<MOD> ret=1;
    while(b)
    {
        if(b&1) ret*=a;
        b>>=1; a=a*a;
    }
    return ret;
}
template<int MOD>
mint<MOD> inv(mint<MOD> a) { return mpow<MOD>(a, MOD-2); }

const int MOD1 = 1e9+7;
const int MOD2 = 998244353;
const int P1 = 3;
const int P2 = 7;

typedef pair<mint<MOD1>, mint<MOD2>> pmm;

pmm operator + (const pmm &p, const pmm &q) { return {p.first+q.first, p.second+q.second}; }
pmm operator - (const pmm &p, const pmm &q) { return {p.first-q.first, p.second-q.second}; }
pmm operator * (const pmm &p, const pmm &q) { return {p.first*q.first, p.second*q.second}; }
pmm operator - (const pmm &p) { return {-p.first, -p.second}; }
pmm& operator += (pmm &p, const pmm &q) { p=p+q; return p; }
pmm& operator -= (pmm &p, const pmm &q) { p=p-q; return p; }
pmm& operator *= (pmm &p, const pmm &q) { p=p*q; return p; }
pmm mpow(pmm a, ll b) { return {mpow(a.first, b), mpow(a.second, b)}; }
pmm inv(pmm a) { return {inv(a.first), inv(a.second)}; }

int N, A[MAXN+10], B[MAXN+10];
pmm PP[MAXN+10], IP[MAXN+10];
pmm HA[MAXN+10], HB[MAXN+10];
int ans=INF;

void solve()
{
    for(int cyc=0; cyc<N; cyc++)
    {
        vector<pair<pmm, int>> V, V2;
        for(int i=1; i<=N; i++) HA[i]=HA[i-1]+PP[i]*pmm{A[i], A[i]};
        for(int i=1; i<=N; i++) HB[i]=HB[i-1]+PP[i]*pmm{B[i], B[i]};
        for(int i=1; i<=N; i++) V.push_back({HB[i-1]+(HB[N]-HB[i])*IP[1], i});
        sort(V.begin(), V.end());

        for(int i=2; i<=N; i++)
        {
            pmm val = (HA[N]-HA[i-1])*IP[i-1] + (HA[i-1]-HA[1])*PP[N-i];
            V2.push_back({val, i});
        }
        sort(V2.begin(), V2.end());

        for(int l=0, r, j=0; l<V2.size(); l=r)
        {
            for(r=l; r<V2.size() && V2[r].first==V2[l].first; r++);
            int val=INF;
            for(; j<V.size() && V[j].first==V2[l].first; j++) val=min(val, V[j].second);
            if(val!=INF) for(int i=l; i<r; i++) ans=min(ans, (V2[i].second-2)*N+val-1);
        }
        rotate(A+1, A+2, A+N+1);
        rotate(B+1, B+2, B+N+1);
    }
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    
    scanf("%d", &N);
    for(int i=1; i<=N; i++) scanf("%d", &A[i]);
    for(int i=1; i<=N; i++) scanf("%d", &B[i]);

    if(N==1)
    {
        if(A[1]==B[1]) printf("0\n");
        else printf("-1\n");
        return 0;
    }

    PP[0]={1, 1}; IP[0]={1, 1};
    PP[1]={P1, P1}; IP[1]=inv(PP[1]);
    for(int i=2; i<=N; i++) PP[i]=PP[i-1]*PP[1];
    for(int i=2; i<=N; i++) IP[i]=IP[i-1]*IP[1];

    solve();
    reverse(A+1, A+N+1);
    reverse(B+1, B+N+1);
    solve();

    if(ans==INF) printf("-1\n");
    else printf("%d\n", ans);
}

詳細信息

answer.code: In function ‘int main()’:
answer.code:101:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  101 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~
answer.code:102:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  102 |     for(int i=1; i<=N; i++) scanf("%d", &A[i]);
      |                             ~~~~~^~~~~~~~~~~~~
answer.code:103:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  103 |     for(int i=1; i<=N; i++) scanf("%d", &B[i]);
      |                             ~~~~~^~~~~~~~~~~~~
In file included from /usr/include/c++/13/string:43,
                 from /usr/include/c++/13/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
                 from answer.code:5:
/usr/include/c++/13/bits/allocator.h: In destructor ‘std::_Vector_base<std::pair<std::pair<mint<1000000007>, mint<998244353> >, int>, std::allocator<std::pair<std::pair<mint<1000000007>, mint<998244353> >, int> > >::_Vector_impl::~_Vector_impl()’:
/usr/include/c++/13/bits/allocator.h:184:7: error: inlining failed in call to ‘always_inline’ ‘std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = std::pair<std::pair<mint<1000000007>, mint<998244353> >, int>]’: target specific option mismatch
  184 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/13/vector:66,
                 from /usr/include/c++/13/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:53:
/usr/include/c++/13/bits/stl_vector.h:133:14: note: called from here
  133 |       struct _Vector_impl
      |              ^~~~~~~~~~~~