QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359868 | #8176. Next TTPC 3 | Delay_for_five_minutes# | WA | 446ms | 108112kb | C++20 | 3.7kb | 2024-03-20 22:38:59 | 2024-03-20 22:38:59 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int N=1e6+3;
ll lcm(ll x,ll y){
return x*y/__gcd(x,y);
}
ll exgcd(ll a,ll b,ll& x,ll& y){
if(!b)return x=1,y=0,a;
ll d=exgcd(b,a%b,y,x);
y-=x*(a/b);
return d;
}
pll excrt(ll m1,ll m2,ll x1,ll x2,ll x,ll y,ll d){
ll m,c=(x2-x1%m2+m2)%m2;
ll t=m2/d;
if(c%d)return {-1,-1};
x=(__int128)x*c/d%t;
x=m1*x+x1,m=m1*t,x=(x%m+m)%m;
return {x,m};
}
vector<ll> mrg(vector<ll> a,vector<ll> b,ll m1,ll m2){
ll x,y;
ll d=exgcd(m1,m2,x,y);
vector<ll> res;
for(ll x1:a)for(ll x2:b){
pll t=excrt(m1,m2,x1,x2,x,y,d);
if(t==pll(-1,-1))continue;
res.push_back(t.first);
}
sort(res.begin(),res.end());
return res;
}
namespace NTT{
const int L=1<<21,mod=998244353;
ll qpow(ll x,int y){
ll r=1;
for(;y;y>>=1){
if(y&1)r=r*x%mod;
x=x*x%mod;
}
return r;
}
ll inv(ll x){
return qpow(x,mod-2);
}
int re[L];
ll w[2][L];
int init(int n){
int len=1,bit=0;
while(len<n)len<<=1,++bit;
for(int i=1;i<len;++i)re[i]=(re[i>>1]>>1)|((i&1)<<(bit-1));
w[0][0]=w[1][0]=1;
w[0][1]=qpow(3,(mod-1)/len),w[1][1]=inv(w[0][1]);
for(int o=0;o<2;++o)
for(int i=2;i<len;++i)
w[o][i]=w[o][i-1]*w[o][1]%mod;
return len;
}
void NTT(ll* a,int n,int o=0){
for(int i=1;i<n;++i)if(i<re[i])swap(a[i],a[re[i]]);
ll R;
for(int k=1;k<n;k<<=1)
for(int i=0,t=k<<1,st=n/t;i<n;i+=t)
for(int j=0,nw=0;j<k;++j,nw+=st){
R=a[i+j+k]*w[o][nw]%mod;
a[i+j+k]=(a[i+j]-R+mod)%mod;
a[i+j]=(a[i+j]+R)%mod;
}
if(o){
R=inv(n);
for(int i=0;i<n;++i)a[i]=a[i]*R%mod;
}
}
ll t0[L],t1[L];
void multi(ll* a,ll* b,ll* c,int n,int m){
int len=init(n+m+1);
memset(t0,0,sizeof(ll)*len);
memcpy(t0,a,sizeof(ll)*(n+1));
memset(t1,0,sizeof(ll)*len);
memcpy(t1,b,sizeof(ll)*(m+1));
NTT(t0,len),NTT(t1,len);
for(int i=0;i<len;++i)t0[i]=t0[i]*t1[i]%mod;
NTT(t0,len,1);
memcpy(c,t0,sizeof(ll)*(n+m+1));
}
}
int n,l[4];
string s[4];
vector<ll> a[4];
vector<ll> b[2];
vector<pll> k;
ll f[N],g[N],h[N],cnt[N];
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>n,--n;
for(int i=0;i<4;++i)cin>>s[i],l[i]=s[i].length();
for(int i=0;s[0][i];++i)if(s[0][i]=='T')a[0].push_back(i);
for(int i=0;s[1][i];++i)if(s[1][i]=='T')a[1].push_back(i);
for(int i=0;s[2][i];++i)if(s[2][i]=='P')a[2].push_back(i);
for(int i=0;s[3][i];++i)if(s[3][i]=='C')a[3].push_back(i);
b[0]=mrg(a[0],a[1],l[0],l[1]);
b[1]=mrg(a[2],a[3],l[2],l[3]);
if(!b[0].size()||!b[1].size()){
cout<<"-1\n";
return 0;
}
ll L[2]={lcm(l[0],l[1]),lcm(l[2],l[3])};
ll x,y;
ll d=exgcd(L[0],L[1],x,y);
for(int i=0;i<L[1];i+=d){
ll c=i/d;
k.push_back({(c*x%(L[1]/d)+L[1]/d)%(L[1]/d),i});
}
sort(k.begin(),k.end());
for(ll x:b[0])f[L[0]-x-1]=1;
for(ll x:b[1])g[x]=1;
NTT::multi(f,g,h,L[0]-1,L[1]-1);
ll sum=0;
for(int i=-(L[0]-1);i<L[1];++i)if(!(i%d)){
sum+=h[L[0]-1+i];
cnt[(i%L[1]+L[1])%L[1]]+=h[L[0]-1+i];
}
if(!sum){
cout<<"-1\n";
return 0;
}
ll T=lcm(L[0],L[1]);
ll ans=T*(n/sum);
n%=sum;
for(auto&[k1,i]:k){
if(n>cnt[i])n-=cnt[i];
else{
for(ll x:b[0]){
if(g[(x+i)%L[1]])--n;
if(n<0){
cout<<ans+x+k1*L[0]+1<<'\n';
return 0;
}
}
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17964kb
input:
3 TTPC TLE P AC
output:
34
result:
ok "34"
Test #2:
score: 0
Accepted
time: 0ms
memory: 18008kb
input:
670055 TF OITFKONTO GFPPNPWTZP CCZFB
output:
-1
result:
ok "-1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 17852kb
input:
910359 TOKYO TECH PROGRAMMING CONTEST
output:
1401951321
result:
ok "1401951321"
Test #4:
score: 0
Accepted
time: 19ms
memory: 21608kb
input:
518530 TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...
output:
518530
result:
ok "518530"
Test #5:
score: 0
Accepted
time: 124ms
memory: 41476kb
input:
252288 TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...
output:
252288
result:
ok "252288"
Test #6:
score: 0
Accepted
time: 110ms
memory: 40324kb
input:
1000000 TTJLTTPNTTQNTTBJTTHZTTJSTTRUTTVWTTJXTTMGTTZHTTRWTTNHTTDWTTMGTTIDTTSYTTPPTTGQTTXYTTOETTYMTTZATTAOTTJNTTHHTTYLTTJBTTLLTTNNTTCITTCBTTDOTTNGTTPZTTUETTFHTTODTTCETTKXTTGUTTZSTTECTTFSTTHOTTESTTNQTTHJTTDPTTFKTTCMTTNMTTGRTTYPTTKQTTYCTTUITTYCTTWWTTZDTTCPTTRSTTLFTTPKTTEXTTUPTTXWTTDATTVUTTLCTTEGTTOLTTRI...
output:
-1
result:
ok "-1"
Test #7:
score: 0
Accepted
time: 139ms
memory: 39836kb
input:
359869 TTXTTTYTTTITTTHTTTRTTTOTTTETTTKTTTZTTTYTTTBTTTUTTTBTTTJTTTDTTTYTTTJTTTKTTTYTTTLTTTATTTLTTTITTTHTTTUTTTMTTTDTTTITTTPTTTITTTCTTTCTTTRTTTZTTTVTTTETTTGTTTNTTTUTTTGTTTYTTTJTTTKTTTQTTTSTTTPTTTWTTTPTTTZTTTETTTJTTTVTTTGTTTUTTTBTTTCTTTCTTTXTTTWTTTETTTFTTTGTTTDTTTATTTCTTTITTTWTTTWTTTITTTSTTTRTTTYTTTETT...
output:
-1
result:
ok "-1"
Test #8:
score: 0
Accepted
time: 114ms
memory: 39096kb
input:
856943 QNTJMTFRTNDTXOTJRTNNTCXTZNTQMTSBTMRTDMTMOTEATOXTHWTOJTQQTLITKRTNSTNITSMTRVTLLTKBTSATYVTHXTEZTRXTPNTEQTQDTVZTMOTJVTESTRQTBBTAXTMNTUBTULTFPTXQTIBTCMTXXTMZTOWTYVTPVTDSTMWTZDTGQTVMTRFTKDTQITKSTCQTOPTSRTJWTFCTEFTCKTISTELTWGTHOTODTQHTLETRCTSHTJITQGTSUTFDTFZTFYTBJTKNTMHTHWTOZTARTZGTQETRITMSTXVTZATKJ...
output:
-1
result:
ok "-1"
Test #9:
score: 0
Accepted
time: 15ms
memory: 8476kb
input:
12 TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...
output:
-1
result:
ok "-1"
Test #10:
score: 0
Accepted
time: 105ms
memory: 40644kb
input:
1000000 WTTTNTTTLTTTZTTTWTTTKTTTDTTTATTTNTTTMTTTZTTTHTTTVTTTVTTTITTTSTTTNTTTSTTTPTTTVTTTWTTTJTTTRTTTQTTTFTTTQTTTMTTTMTTTYTTTWTTTLTTTMTTTNTTTCTTTWTTTSTTTVTTTDTTTUTTTLTTTCTTTCTTTZTTTQTTTQTTTWTTTDTTTQTTTZTTTUTTTQTTTITTTYTTTATTTATTTGTTTITTTWTTTYTTTYTTTFTTTHTTTPTTTDTTTWTTTRTTTLTTTNTTTFTTTNTTTNTTTCTTTBTTT...
output:
-1
result:
ok "-1"
Test #11:
score: 0
Accepted
time: 113ms
memory: 39136kb
input:
506084 TPTGTATITNTJTPTBTZTDTRTVTJTVTSTFTWTNTATYTKTJTCTBTPTPTJTSTQTGTPTKTYTBTMTXTVTMTUTFTPTRTYTVTFTATCTLTITVTUTNTATCTJTYTOTOTMTKTBTMTMTKTQTWTBTHTITATUTYTNTETRTYTWTDTLTCTPTGTKTMTETGTCTWTMTNTKTMTATDTQTWTWTPTMTRTYTQTMTJTCTGTXTDTUTSTBTYTFTKTGTYTHTNTOTOTXTQTITITSTGTUTMTNTPTETJTDTJTRTBTKTXTCTSTRTZTSTWTUTJT...
output:
-1
result:
ok "-1"
Test #12:
score: 0
Accepted
time: 98ms
memory: 38516kb
input:
95378 JTTGTTYTTMTTFTTSTTUTTBTTNTTSTTVTTKTTYTTUTTITTFTTYTTKTTSTTSTTATTMTTQTTMTTLTTRTTWTTQTTLTTWTTPTTYTTGTTPTTNTTUTTRTTKTTWTTHTTVTTGTTMTTQTTUTTXTTCTTZTTUTTBTTDTTPTTJTTETTZTTNTTOTTKTTITTOTTGTTSTTQTTVTTZTTATTBTTATTOTTYTTZTTZTTZTTDTTPTTHTTMTTDTTLTTNTTNTTITTETTRTTYTTOTTWTTZTTGTTGTTRTTXTTKTTGTTVTTDTTSTTWTT...
output:
27097032
result:
ok "27097032"
Test #13:
score: 0
Accepted
time: 102ms
memory: 40484kb
input:
12 TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...
output:
10636
result:
ok "10636"
Test #14:
score: 0
Accepted
time: 112ms
memory: 40196kb
input:
1000000 LTTTWTTTLTTTWTTTITTTQTTTWTTTKTTTSTTTGTTTLTTTHTTTXTTTFTTTYTTTPTTTBTTTFTTTYTTTBTTTZTTTYTTTHTTTNTTTVTTTKTTTMTTTITTTQTTTYTTTYTTTZTTTGTTTBTTTLTTTMTTTJTTTYTTTVTTTITTTYTTTRTTTCTTTHTTTRTTTUTTTWTTTQTTTITTTVTTTWTTTMTTTRTTTFTTTITTTYTTTETTTFTTTHTTTOTTTYTTTOTTTXTTTFTTTLTTTRTTTFTTTGTTTETTTBTTTVTTTETTTWTTT...
output:
907999396
result:
ok "907999396"
Test #15:
score: 0
Accepted
time: 110ms
memory: 39824kb
input:
713716 BTTCTTOTTZTTWTTOTTHTTITTITTITTWTTGTTFTTJTTMTTWTTRTTGTTFTTLTTJTTDTTWTTITTRTTMTTQTTDTTJTTWTTHTTKTTRTTQTTSTTETTKTTYTTRTTSTTGTTQTTYTTOTTCTTLTTSTTOTTETTUTTRTTETTLTTQTTYTTYTTITTUTTXTTZTTXTTJTTQTTWTTWTTMTTRTTATTNTTSTTJTTKTTGTTHTTVTTSTTRTTXTTMTTUTTNTTPTTQTTHTTJTTHTTETTQTTGTTGTTATTITTATTBTTQTTWTTHTTWT...
output:
563121569
result:
ok "563121569"
Test #16:
score: 0
Accepted
time: 105ms
memory: 37972kb
input:
11522 TXTTTHTTTVTTTOTTTFTTTZTTTKTTTZTTTJTTTDTTTPTTTRTTTHTTTOTTTXTTTCTTTWTTTNTTTDTTTSTTTWTTTCTTTHTTTXTTTFTTTSTTTOTTTHTTTCTTTWTTTUTTTXTTTSTTTZTTTXTTTLTTTETTTKTTTLTTTXTTTLTTTVTTTATTTLTTTHTTTDTTTITTTQTTTLTTTKTTTETTTATTTHTTTYTTTOTTTDTTTSTTTCTTTVTTTRTTTITTTITTTMTTTPTTTNTTTJTTTXTTTKTTTXTTTGTTTCTTTUTTTCTTTW...
output:
11152921
result:
ok "11152921"
Test #17:
score: 0
Accepted
time: 94ms
memory: 39516kb
input:
1000000 YHTTKJTTFNTTMITTNWTTOETTWJTTCFTTIRTTVJTTJMTTHOTTBITTLXTTRLTTVLTTOFTTMYTTOGTTAYTTVLTTCRTTENTTLKTTKETTXPTTSFTTCZTTGLTTJZTTGFTTOYTTSFTTLHTTNMTTANTTSGTTKKTTXETTABTTSBTTLLTTQOTTNDTTWATTERTTAUTTMWTTISTTASTTZJTTPXTTONTTVGTTEATTVBTTXGTTSITTPNTTLITTLQTTHVTTKOTTQGTTVLTTDQTTLHTTJETTWFTTDFTTIQTTIETTKHTT...
output:
-1
result:
ok "-1"
Test #18:
score: 0
Accepted
time: 87ms
memory: 36140kb
input:
1000000 TTXZTTYMTTVYTTASTTUPTTABTTLRTTEPTTQPTTMKTTCOTTPKTTPGTTSITTDRTTQSTTCPTTXWTTXBTTJDTTZNTTIPTTIOTTKJTTQITTEJTTJRTTAQTTIBTTAETTUOTTUETTDRTTNFTTPZTTYHTTMZTTQDTTCDTTVDTTHNTTXKTTUPTTFPTTYFTTIUTTUBTTJATTYFTTJWTTGUTTPXTTIPTTHSTTVATTHWTTWZTTMSTTJRTTYWTTSRTTUPTTDQTTKJTTIUTTVBTTNOTTBGTTINTTAMTTRNTTAXTTDW...
output:
-1
result:
ok "-1"
Test #19:
score: 0
Accepted
time: 100ms
memory: 38372kb
input:
367325 TSITTGUTTPJTTVDTTSSTTIJTTZATTKGTTPATTCKTTBWTTYKTTRUTTMOTTUQTTEDTTFLTTYOTTRITTYVTTGXTTVJTTWPTTVDTTAFTTSOTTJZTTDKTTBUTTSPTTFSTTHXTTKDTTVJTTCUTTALTTRHTTAJTTZHTTZDTTAOTTJVTTLKTTSXTTFITTDPTTGSTTXRTTEITTOQTTEPTTVCTTSITTJUTTPXTTZWTTPKTTUZTTLWTTYBTTFETTJYTTLDTTGSTTIITTKFTTKHTTUGTTVPTTXNTTFCTTLZTTYXTT...
output:
-1
result:
ok "-1"
Test #20:
score: 0
Accepted
time: 115ms
memory: 41496kb
input:
1000000 TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...
output:
-1
result:
ok "-1"
Test #21:
score: 0
Accepted
time: 91ms
memory: 39824kb
input:
1000000 NTTNWTTJFTTLMTTSATTRWTTEXTTBETTPBTTEBTTXPTTUDTTWVTTWMTTWZTTCATTSBTTKBTTEKTTNHTTPBTTCATTVITTPJTTCYTTAPTTVLTTDHTTVQTTJUTTCFTTLXTTODTTFMTTXRTTRLTTNPTTVNTTYMTTOXTTDUTTVZTTSDTTMPTTYCTTLKTTFATTCETTUFTTGDTTKVTTGYTTVATTYZTTQRTTWQTTMGTTFXTTLDTTXRTTGFTTPLTTQQTTOVTTEOTTJSTTXSTTQKTTRVTTWZTTKXTTRBTTRRTTM...
output:
-1
result:
ok "-1"
Test #22:
score: 0
Accepted
time: 103ms
memory: 39940kb
input:
1000000 DTTTATTTBTTTUTTTUTTTNTTTDTTTITTTATTTRTTTFTTTJTTTZTTTDTTTRTTTETTTWTTTCTTTHTTTFTTTBTTTJTTTUTTTWTTTJTTTCTTTETTTYTTTHTTTWTTTETTTWTTTCTTTZTTTWTTTYTTTKTTTRTTTLTTTVTTTETTTOTTTDTTTPTTTLTTTMTTTWTTTCTTTXTTTDTTTPTTTVTTTFTTTATTTKTTTWTTTOTTTUTTTPTTTZTTTMTTTETTTWTTTRTTTPTTTRTTTHTTTFTTTYTTTRTTTKTTTJTTTETTT...
output:
452272320
result:
ok "452272320"
Test #23:
score: 0
Accepted
time: 130ms
memory: 40892kb
input:
1000000 CTITITNTHTTTBTZTNTMTFTCTZTJTRTBTUTPTWTWTNTUTMTUTETITITATKTMTRTPTVTXTPTHTNTGTSTMTUTWTKTWTITBTCTHTFTWTDTHTWTBTSTNTNTCTGTVTVTVTMTVTITWTJTRTYTETMTKTFTGTPTXTHTGTXTVTPTHTKTSTMTUTMTLTCTDTHTGTUTVTPTYTQTOTBTMTZTZTWTOTFTGTATBTFTQTNTPTGTLTYTKTPTCTFTATSTDTQTBTXTATVTRTGTITSTITDTKTPTNTKTKTBTATWTKTRTBTMTDT...
output:
975999035
result:
ok "975999035"
Test #24:
score: 0
Accepted
time: 209ms
memory: 63664kb
input:
11 WHLKKWTICHLBXNJKGGFGQLTLEHQUIYKLQZVWFFAVIDVCVJHUSJTYQTIJECJNQCLEJNZPKMPVBPKZHENYFXCSXZEUVJAFLMWIZPDWQDNRPBIGBTMNQKQOGJFEQYDHQYIBZXHBMWTJZJJWYWCQLEDVCUVCDMPEWOXGRADJQGRANTLNMTMHALLKTZYLFZPOPTOGKSTJBGJEGKBONVGJUSQDPBTECDEDGHAZCZOMZBMXVVTNMLPPLBVHLZXASGFRAIBAULIYDEHASZNGQDUBZCRTZYIXBDYWAGOIGCOAAPPON...
output:
6385324
result:
ok "6385324"
Test #25:
score: -100
Wrong Answer
time: 446ms
memory: 108112kb
input:
14 UCBNFDDWTZCYVRYRQCEWBIVBQADDGNHJDJIPZMHCDWLAJDEJXIJIQTUOSNEUONNQIMOCRLSQXBPHDPQYBJLRQMANSCKNMCORGLIGAVBZQWQJKZFYDARHAZWYQOWYBKTWYESJAXNWDEEITRMCAPWAVVQQCCKQBSIPEGYZQXHYLWFOIYQBFESGUABSXJTOLACYNAUGFXJIBICEDPFLCUGQPDVZVGXRBKSHIDMFGEAAAPVCEBZJIZJQJTXQEPLZXFUQYWWPBQIDQDAXCGSBJNMBVOQTPWMTVVIZZTZZNOLBN...
output:
3639736
result:
wrong answer 1st words differ - expected: '3768074', found: '3639736'