QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#383620 | #8560. Just Shuffle the Input | bulijiojiodibuliduo# | WA | 736ms | 141424kb | C++17 | 4.3kb | 2024-04-09 16:00:23 | 2024-04-09 16:00:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef long double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int L=19, N=(1<<L);
struct comp {
db x, y;
comp(): x(0), y(0) { }
comp(const db &_x, const db &_y): x(_x), y(_y) { }
};
inline comp operator+(const comp &a, const comp &b) { return comp(a.x + b.x, a.y + b.y); }
inline comp operator-(const comp &a, const comp &b) { return comp(a.x - b.x, a.y - b.y); }
inline comp operator*(const comp &a, const comp &b) { return comp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
inline comp conj(const comp &a) { return comp(a.x, -a.y); }
const db PI = acosl(-1);
comp w[N + 5];
int bitrev[N + 5];
void fft(comp *a, const int &n) {
rep(i, 0, n) if (i < bitrev[i]) swap(a[i], a[bitrev[i]]);
for (int i = 2, lyc = n >> 1; i <= n; i <<= 1, lyc >>= 1)
for (int j = 0; j < n; j += i)
{
comp *l = a + j, *r = a + j + (i >> 1), *p = w;
rep(k, 0, i >> 1)
{
comp tmp = *r * *p;
*r = *l - tmp, *l = *l + tmp;
++l, ++r, p += lyc;
}
}
}
inline void fft_prepare()
{
rep(i, 0, N) bitrev[i] = bitrev[i >> 1] >> 1 | ((i & 1) << (L - 1));
rep(i, 0, N) w[i] = comp(cosl(2 * PI * i / N), sinl(2 * PI * i / N));
}
void conv(int *x, int *y, int *z,int K,int mod) {
static comp a[N + 5], b[N + 5];
static comp dfta[N + 5], dftb[N + 5], dftc[N + 5], dftd[N + 5];
rep(i, 0, K + 1) a[i] = comp(x[i] & 32767, x[i] >> 15);
rep(i, 0, K + 1) b[i] = comp(y[i] & 32767, y[i] >> 15);
rep(i, K + 1, N) a[i] = b[i] = comp(0, 0);
fft(a, N), fft(b, N);
rep(i, 0, N)
{
int j = (N - i) & (N - 1);
static comp da, db, dc, dd;
da = (a[i] + conj(a[j])) * comp(0.5, 0);
db = (a[i] - conj(a[j])) * comp(0, -0.5);
dc = (b[i] + conj(b[j])) * comp(0.5, 0);
dd = (b[i] - conj(b[j])) * comp(0, -0.5);
dfta[j] = da * dc;
dftb[j] = da * dd;
dftc[j] = db * dc;
dftd[j] = db * dd;
}
rep(i, 0, N) a[i] = dfta[i] + dftb[i] * comp(0, 1);
rep(i, 0, N) b[i] = dftc[i] + dftd[i] * comp(0, 1);
fft(a, N), fft(b, N);
rep(i, 0, N)
{
int da = (ll)(a[i].x / N + 0.5) % mod;
int db = (ll)(a[i].y / N + 0.5) % mod;
int dc = (ll)(b[i].x / N + 0.5) % mod;
int dd = (ll)(b[i].y / N + 0.5) % mod;
z[i] = (da + ((ll)(db + dc) << 15) + ((ll)dd << 30)) % mod;
}
}
typedef pair<int,int> hashv;
const ll mod1=1000000007;
const ll mod2=1000000009;
hashv operator + (hashv a,hashv b) {
int c1=a.fi+b.fi,c2=a.se+b.se;
if (c1>=mod1) c1-=mod1;
if (c2>=mod2) c2-=mod2;
return mp(c1,c2);
}
hashv operator - (hashv a,hashv b) {
int c1=a.fi-b.fi,c2=a.se-b.se;
if (c1<0) c1+=mod1;
if (c2<0) c2+=mod2;
return mp(c1,c2);
}
hashv operator * (hashv a,hashv b) {
return mp(1ll*a.fi*b.fi%mod1,1ll*a.se*b.se%mod2);
}
hashv base(13331,23333);
//const int N=201000;
int n,m,p[N],cyc[N],rk[N],fa[N],fb[N],fc[N];
hashv pw[N],hs[N],ps[N],qs[N],ret[N];
char s[N],t[N];
int main() {
fft_prepare();
scanf("%d%d",&n,&m);
rep(i,0,m) {
scanf("%d",&p[i]); --p[i];
}
int u=0;
rep(i,0,m) {
cyc[i]=u;
rk[u]=i;
u=p[u];
}
scanf("%s",s);
scanf("%s",t);
pw[0]=mp(1,1);
rep(i,1,n+1) pw[i]=pw[i-1]*base;
rep(i,0,n) {
hs[i+1]=hs[i]*base+mp(s[i],s[i]);
}
set<hashv> st;
for (int i=0;i+m<=n;i++) {
st.insert(hs[i+m]-hs[i]*pw[m]);
}
rep(i,0,m) {
ps[m-i-1]=pw[m-1-cyc[i]];
qs[i]=mp(t[cyc[i]],t[cyc[i]]);
}
rep(i,0,m) fa[i]=ps[i].fi,fb[i]=qs[i].fi;
conv(fa,fb,fc,m-1,mod1);
rep(i,0,m*2-1) ret[i%m]=ret[i%m]+mp(fc[i],0);
rep(i,0,m) fa[i]=ps[i].se,fb[i]=qs[i].se;
conv(fa,fb,fc,m-1,mod2);
rep(i,0,m*2-1) ret[i%m]=ret[i%m]+mp(0,fc[i]);
rep(i,0,m) {
hashv c=ret[i];
//rep(j,0,m) c=c+qs[j]*ps[(i-j+m)%m];
if (st.count(c)) {
printf("%d\n",i);
return 0;
}
}
puts("-1");
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 736ms
memory: 141272kb
input:
3 2 2 1 aba ba
output:
0
result:
ok 1 number(s): "0"
Test #2:
score: 0
Accepted
time: 682ms
memory: 139336kb
input:
7 4 3 4 2 1 dcabadc abcd
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: -100
Wrong Answer
time: 706ms
memory: 141424kb
input:
100 10 5 3 8 10 7 1 4 9 6 2 dkrwvaottzwtarzokdvtwtarzokdvtzadtkwrvotottkwvzadrazkodtvrtwkdvtrztowakdvtrztowaazkodtvrtwzadtkwrvot dkrwvaottz
output:
1
result:
wrong answer 1st numbers differ - expected: '0', found: '1'