QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#229770#6129. Magic MultiplicationISYRHHAC ✓29ms10968kbC++142.8kb2023-10-28 16:52:192023-10-28 16:52:20

Judging History

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

  • [2023-10-28 16:52:20]
  • 评测
  • 测评结果:AC
  • 用时:29ms
  • 内存:10968kb
  • [2023-10-28 16:52:19]
  • 提交

answer

#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
char in[200010];
int a[200010],b[200010],dat[200010],ra[200010],rb[200010];
int T,n,m,l;
bool ot;
inline int gt(int x,int y)
{
	return y==1?in[x]^48:(in[x]^48)*10+(in[x+1]^48);
}
inline int test(int x)
{
	for(int i=2;i<=9;i++)
	{
		if(x%i==0&&x/i<=9)return true;
	}
	return x==1;
}
inline int gcd(int x,int y)
{
	return x%y?gcd(y,x%y):y;
}
void cmp()
{
	if(ot)
	{
		for(int i=1;i<=n;i++)
		{
			if(ra[i]<a[i])return;
			if(ra[i]>a[i])
			{
				memcpy(ra,a,sizeof(int)*(n+1));
				memcpy(rb,b,sizeof(int)*(m+1));
				return;
			}
		}
		for(int i=1;i<=m;i++)
		{
			if(rb[i]<b[i])return;
			if(rb[i]>b[i])
			{
				memcpy(ra,a,sizeof(int)*(n+1));
				memcpy(rb,b,sizeof(int)*(m+1));
				return;
			}
		}
	}
	else
	{
		ot=true;
		memcpy(ra,a,sizeof(int)*(n+1));
		memcpy(rb,b,sizeof(int)*(m+1));
	}
}
void wkot(int x,int y)
{
	if(x>n)
	{
		if(y==l+1)cmp();
	}
	else
	{
		if(y!=l&&in[y]!='0')
		{
			int nww=gt(y,2);
			if(nww%b[1]==0&&nww/b[1]<=9)
			{
				a[x]=nww/b[1];
				int yy=y+2;
				bool can=true;
				for(int i=2;i<=m;i++)
				{
					if(a[x]*b[i]>9)
					{
						if(a[x]*b[i]!=gt(yy,2))
						{
							can=false;
							break;
						}
						yy+=2;
					}
					else
					{
						if(a[x]*b[i]!=gt(yy,1))
						{
							can=false;
							break;
						}
						yy++;
					}
				}
				if(can)
				{
					wkot(x+1,yy);
				}
			}
		}
		int nww=gt(y,1);
		if(nww%b[1]==0)
		{
			a[x]=nww/b[1];
			y++;
			bool can=true;
			for(int i=2;i<=m;i++)
			{
				if(a[x]*b[i]>9)
				{
					if(a[x]*b[i]!=gt(y,2))
					{
						can=false;
						break;
					}
					y+=2;
				}
				else
				{
					if(a[x]*b[i]!=gt(y,1))
					{
						can=false;
						break;
					}
					y++;
				}
			}
			if(can)
			{
				wkot(x+1,y);
			}
		}
	}
}
void dfs(int i,int nw,int x,int y)
{
	if(i>m)
	{
		for(int j=max(1,(y+8)/9);j<=x&&j<=9;j++)
		{
			if(x%j==0&&x/j<=9)
			{
				a[1]=j;
				for(int k=1;k<=m;k++)
				{
					b[k]=dat[k]/j;
				}
				wkot(2,nw);
			}
		}
	}
	else if(nw>l)return;
	else
	{
		if(nw!=l&&in[nw]!='0')
		{
			int nww=gt(nw,2);
			dat[i]=nww;
			if(test(nww))
			{
				int xx=gcd(x,nww);
				int yy=max(y,nww);
				if(yy<=xx*9)dfs(i+1,nw+2,xx,yy);
			}
		}
		int nww=gt(nw,1);
		dat[i]=nww;
		if(nww)x=gcd(x,nww);
		y=max(y,nww);
		if(y<=x*9)dfs(i+1,nw+1,x,y);
	}
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		ot=false;
		scanf("%d%d",&n,&m);
		scanf("%s",in+1);
		l=strlen(in+1);
		dfs(1,1,0,9);
		if(ot)
		{
			for(int i=1;i<=n;i++)
			{
				printf("%d",ra[i]);
			}
			printf(" ");
			for(int i=1;i<=m;i++)
			{
				printf("%d",rb[i]);
			}
			printf("\n");
		}
		else
		{
			printf("Impossible\n");
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3648kb

input:

4
2 2
8101215
3 4
100000001000
2 2
80101215
3 4
1000000010000

output:

23 45
101 1000
Impossible
Impossible

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 29ms
memory: 10968kb

input:

1025
11 18
1461416814188088414188241035153540203545200202010354520510254921495628496328028281449632871435351535402035452002020103545205102500000000000000000000000000004000000063276372366381360363618638136918454921495628496328028281449632871435492149562849632802828144963287143514614168141880884141882...

output:

Impossible
3583 5
161650357972 65354104569
597523997017 7693
Impossible
406723924695110 973937089831524
59331138450754 554
4 189401911962950
980565699171 84748728972992
Impossible
62155650672 4241405
9458752764004792353 8717596993614
Impossible
941952596 49242258343771276739
Impossible
64053045751 4...

result:

ok 1025 lines