QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#314275 | #8176. Next TTPC 3 | ucup-team2303# | WA | 16ms | 51272kb | C++14 | 2.6kb | 2024-01-25 15:11:27 | 2024-01-25 15:11:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long T,a,b,c,d[1000001],o,h[1000001],fa[1000001],q,w,e,an,cn,st[1000001],u[1000001],st1[1000001],st2[1000001],cn1,cn2;
struct p{int q,w;};
long long pow_(long long qq,long long ww){long long ee=1;while(ww){if(ww&1) ee*=qq,ee%=mod;qq*=qq,qq%=mod,ww>>=1;}return ee%mod;}
inline int read(){int x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}return x*f;}
long long gcd(long long qq,long long ww){return !ww?qq:gcd(ww,qq%ww);}
inline void exgcd(long long a,long long b,long long &x,long long &y)
{
if(b==0) x=1,y=0;
else
{
exgcd(b,a%b,x,y);
long long d=x;
x=y,y=d-a/b*y;
}
}
char s[4][100001];
long long all,lm1,lm2,g1,g2,tt;
vector<long long> qu1[1000001],qu2[1000001];
long long get(long long qq)
{
long long ann=0;
for(int i=0;i<tt;i++)
{
long long gg=-1;
for(int j=0;j<qu1[i].size();j++)
{
while(gg+1<qu2[i].size()&&qu2[i][gg+1]+qu1[i][j]<=qq) ++gg;
ann+=gg+1;
}
gg=-1;long long g1=-1;
for(int j=0;j<qu1[i].size();j++)
{
while(gg+1<qu2[i].size()&&qu2[i][gg+1]+qu1[i][j]<=qq+all) ++gg;
while(g1+1<qu2[i].size()&&qu2[i][g1+1]+qu1[i][j]<all) ++g1;
ann+=gg-g1;
}
}
return ann;
}
int main()
{
scanf("%lld",&a);
for(int i=0;i<4;i++)
{
scanf("%s",s[i]);
d[i]=strlen(s[i]);
}
long long t1=d[0]*d[1]/gcd(d[0],d[1]),t2=d[2]*d[3]/gcd(d[2],d[3]);
tt=gcd(t1,t2);
for(int i=0;i<t1;i++)
{
if(s[0][i%d[0]]=='T'&&s[1][i%d[1]]=='T')
{
qu1[i%tt].push_back(i);
}
}
for(int i=0;i<t2;i++)
{
if(s[2][i%d[2]]=='P'&&s[3][i%d[3]]=='C')
{
qu2[i%tt].push_back(i);
}
}
all=t1*t2/tt;
long long hh=0;exgcd(t1/tt,t2/tt,lm1,lm2);
g1=((1-(t1/tt)*lm1)%all+all)%all,g2=((t1/tt)*lm1%all+all)%all;
for(int i=0;i<tt;i++) hh=hh+(long long)(qu1[i].size())*(long long)(qu2[i].size());
if(hh==0)
{
printf("-1");return 0;
}
long long ff=a/hh;a%=hh;
if(a==0) --ff,a+=hh;
an+=ff*t1*t2/tt;
long long ll=0,rr=t1*t2/tt-1,ann=0;
for(int i=0;i<tt;i++)
{
for(int j=0;j<qu1[i].size();j++) qu1[i][j]=((__int128)(qu1[i][j])*g1)%all;
for(int j=0;j<qu2[i].size();j++) qu2[i][j]=((__int128)(qu2[i][j])*g2)%all;
for(int j=0;j<qu1[i].size();j++) qu1[i][j]=(qu1[i][j]%all+all)%all;
for(int j=0;j<qu2[i].size();j++) qu2[i][j]=(qu2[i][j]%all+all)%all;
sort(qu1[i].begin(),qu1[i].end());
sort(qu2[i].begin(),qu2[i].end());
}
while(ll<=rr)
{
long long mid=((ll+rr)>>1);
if(get(mid)>=a) rr=mid-1,ann=mid;
else ll=mid+1;
}
an+=ann+1;
printf("%lld",an);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 9ms
memory: 50896kb
input:
3 TTPC TLE P AC
output:
34
result:
ok "34"
Test #2:
score: 0
Accepted
time: 4ms
memory: 50608kb
input:
670055 TF OITFKONTO GFPPNPWTZP CCZFB
output:
-1
result:
ok "-1"
Test #3:
score: 0
Accepted
time: 9ms
memory: 50632kb
input:
910359 TOKYO TECH PROGRAMMING CONTEST
output:
1401951321
result:
ok "1401951321"
Test #4:
score: -100
Wrong Answer
time: 16ms
memory: 51272kb
input:
518530 TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...
output:
513417
result:
wrong answer 1st words differ - expected: '518530', found: '513417'