题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061
题意:
定义一种新型运算符:
也就是说Gao(23, 45) = 8101215,因为2*4=8,2*5=10,3*4=12,3*5=15,连在一起刚好就是8101215
每次询问给你2个数字n和m和一个结果S,求满足Gao(x, y) = s,且x长度为n,y长度为m的一组合法的x, y(n,m,|S|≤100000)
要求x尽可能小
思路:
暴力枚举x的第一位,那么y的所有位就都唯一确定了,然后x的第2位到第n位也就唯一确定了
所以其实只要模拟就可以了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<assert.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
char temp[500508];
int n, m, len, str[500508], A[200508], B[200508];
int Jud()
{
int i, j, now, p;
p = now = 0;
for(i=1;i<=m;i++)
{
now = str[++p];
if(now!=0 && now<A[1])
now = now*10+str[++p];
if(now%A[1]==0 && now/A[1]<=9)
B[i] = now/A[1];
else
return 0;
}
for(i=2;i<=n;i++)
{
now = str[++p];
if(now!=0 && now<B[1])
now = now*10+str[++p];
if(now%B[1]==0 && now/B[1]<=9)
A[i] = now/B[1];
else
return 0;
for(j=2;j<=m;j++)
{
now = str[++p];
if(now!=0 && now<A[i])
now = now*10+str[++p];
if(now!=A[i]*B[j])
return 0;
}
}
if(p==len)
return 1;
return 0;
}
int Gao()
{
int i;
for(i=1;i<=9;i++)
{
A[1] = i;
if(Jud())
return 1;
}
return 0;
}
int main(void)
{
int T, i;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%s", &n, &m, temp+1);
len = strlen(temp+1);
for(i=1;i<=len;i++)
str[i] = temp[i]-'0';
if(Gao())
{
for(i=1;i<=n;i++)
printf("%d", A[i]);
printf(" ");
for(i=1;i<=m;i++)
printf("%d", B[i]);
puts("");
}
else
printf("Impossible\n");
}
return 0;
}