QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#943844 | #10041. Periodic Sequence | BreakPlus | WA | 1ms | 9676kb | C++14 | 1.9kb | 2025-03-20 07:30:04 | 2025-03-20 07:30:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define mkp make_pair
#define pb emplace_back
#define popcnt __builtin_popcountll
inline ll read(){
ll x=0, f=1; char ch=getchar();
while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0' && ch<='9') x=x*10+ch-'0', ch=getchar();
return x*f;
}
inline int lg2(int x){ return 31^__builtin_clz(x); }
inline ll lg2(ll x){ return 63^__builtin_clzll(x); }
int n,m,a[500005],b[500005],fa[500005],fb[500005];
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
ll rng(ll x,ll y){ return x+rnd()%(y-x+1); }
const ll mod = (ll)1e18 + 3, base = rng(5e7, 1e8);
ll hsh[1000005], pw[1000005];
inline ll gethsh(int l,int r){
return (hsh[r] + (__int128)(mod-hsh[l-1]) * pw[r-l+1]) % mod;
}
void procedure(){
n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<=m;i++) b[i]=read();
for(int i=2;i<=n;i++){
fa[i]=fa[i-1];
while(fa[i] && a[1+fa[i]] != a[i]) fa[i]=fa[fa[i]];
if(a[1+fa[i]] == a[i]) ++fa[i];
}
for(int i=2;i<=m;i++){
fb[i]=fb[i-1];
while(fb[i] && b[1+fb[i]] != b[i]) fb[i]=fb[fb[i]];
if(b[1+fb[i]] == b[i]) ++fb[i];
}
if(n%(n-fa[n])==0) n-=fa[n];
if(m%(m-fa[m])==0) m-=fa[m];
if(n!=m){
puts("No");
return;
}
for(int i=n+1;i<2*n;i++) b[i]=b[i-n];
pw[0]=1;
for(int i=1;i<2*n;i++)
pw[i]=(__int128)pw[i-1]*base%mod;
for(int i=1;i<2*n;i++)
hsh[i]=((__int128)hsh[i-1]*base+b[i])%mod;
ll st=0;
for(int i=1;i<=n;i++)
st=((__int128)st*base+a[i])%mod;
for(int i=1;i<n;i++){
if(gethsh(i,i+n-1)==st){
puts("Yes");
return;
}
}
puts("No");
}
int main(){
#ifdef LOCAL
assert(freopen("input.txt","r",stdin));
assert(freopen("output.txt","w",stdout));
#endif
ll T=1;
// math_init();
// NTT::init();
while(T--) procedure();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9664kb
input:
6 3 1 5 6 1 5 6 6 1 5
output:
Yes
result:
ok answer is YES
Test #2:
score: 0
Accepted
time: 0ms
memory: 7760kb
input:
7 3 1 5 6 1 5 6 7 5 6 7
output:
No
result:
ok answer is NO
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 9676kb
input:
2 3 3 3 3 3 3
output:
No
result:
wrong answer expected YES, found NO