QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#314236#8176. Next TTPC 3ucup-team2303#WA 17ms56560kbC++143.3kb2024-01-25 14:51:102024-01-25 14:51:12

Judging History

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

  • [2024-01-25 14:51:12]
  • 评测
  • 测评结果:WA
  • 用时:17ms
  • 内存:56560kb
  • [2024-01-25 14:51:10]
  • 提交

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;
bool v[2000001][2],vi[2000001];
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);}
queue<p> qu;
void ins(int qq,int ww)
{
	if(ww==1&&!vi[qq]) return;
	if(v[qq][ww]) return;
	if(ww==1&&qq==d[a])
	{
		printf("Yes");
		exit(0);
	}
	v[qq][ww]=1;
	qu.push(p{qq,ww});
}
void work(long long qq,long long ww)
{
	if(ww==1)
	{
		int tt=qq,gg=1;cn=0;
		for(int i=0;i<13;i++)
		{
			st[cn++]=tt%3,tt/=3;
		}
		for(int i=0;i<13;i++)
		{
			ins(qq-st[i]*gg,0);
			gg=gg*3;
		}
	}
	else
	{
		ins(qq,1);
		int tt=qq,gg=1;cn=0;
		for(int i=0;i<13;i++)
		{
			st[cn++]=tt%3,tt/=3;
		}
		for(int i=0;i<13;i++)
		{
			if(st[i]<2)
			{
				ins(qq+gg,0);
			}
			gg=gg*3;
		}
	}
}
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;
		}
//		for(int j=0;j<qu1[i].size();j++)
//		{
//			for(int k=0;k<qu2[i].size();k++)
//			{
//				if((qu1[i][j]+qu2[i][k])%all<=qq) ++ann;
//			}
//		}
	}
	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;
	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;
		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: 8ms
memory: 56100kb

input:

3
TTPC
TLE
P
AC

output:

34

result:

ok "34"

Test #2:

score: 0
Accepted
time: 8ms
memory: 54712kb

input:

670055
TF
OITFKONTO
GFPPNPWTZP
CCZFB

output:

-1

result:

ok "-1"

Test #3:

score: 0
Accepted
time: 3ms
memory: 55420kb

input:

910359
TOKYO
TECH
PROGRAMMING
CONTEST

output:

1401951321

result:

ok "1401951321"

Test #4:

score: -100
Wrong Answer
time: 17ms
memory: 56560kb

input:

518530
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...

output:

513417

result:

wrong answer 1st words differ - expected: '518530', found: '513417'