2018年 ACM/ICPC亚洲区域赛 青岛赛区现场赛 D题(ZOJ 4061 枚举)

Magic Multiplication


Time Limit: 1 Second      Memory Limit: 65536 KB


BaoBao is now learning a new binary operation between two positive integers, represented by , in his magic book. The book tells him that the result of such operation is calculated by concatenating all multiple results of each digit in the two integers.

Formally speaking, let the first integer be , where  indicates the -th digit in , and the second integer be , where  indicates the -th digit in . We have

Note that the result of  is considered to be a \textbf{string} (without leading zeros if , or contains exactly one `0' if ), NOT a normal integer. Also, the sum here means \textbf{string concatenation}, NOT the normal addition operation.

 

For example, 23  45 = 8101215. Because , ,  and .

BaoBao is very smart and soon knows how to do the inverse operation of . Now he gives you the result of a  operation and the numbers of digits in the two original integers. Please help him to restore the two original integers  and .

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two positive integers  and  (), where  indicates the length of  and  indicates the length of . Here length of an integer means the length of the string when writing the number in decimal notation without leading zeros.

The second line contains only one positive integer  without leading zeros, indicating the result of . The length of  is no more than .

It's guaranteed that the sum of lengths of  over all test cases will not exceed .

Output

For each test case output one line.

If there exist such  and  that , output one line containing two integers  and  separated by one space. Note that  and  should be positive integers without leading zeros, the length of  should be exactly , and the length of  should be exactly .

If there are multiple valid answers, output the answer with the smallest ; If there are still more than one answer, output one of them with the smallest .

If such  and  do not exist, print "Impossible" (without quotes) on a single line.

Sample Input

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

Sample Output

23 45
101 1000
Impossible
Impossible

题意:有两个整数A,B,A的每一位记作a1,a2,...,an。B的每一位记作b1,b2,...,bn。

定义A※B=a1*b1 a1*b2 ... a1*bm a2*b1 a2*b2 a2*b3 ... an*bm (每一位的运算结果拼成一个字符串)

现在给你这个最后的字符串以及n和m,让你判断是否存在A,B(长度分别为n,m),使A※B的结果为给你的这个字符串。

如果不存在输出Impossible。否则输出A,B。

思路:

首先注意,A,B都是一个整数,整数的特点是没有前导0,这就意味着,如果存在,a1和b1肯定不是0。

一旦确定a1之后,b1~bm就可以依次从给你的串中找(注意一位还是两位),如果满足条件,则只能是一位或两位其一(仔细想想),从而a2~an也就被确定了。因此,确定了a1,整个字符串实际上也相当于被确定。(想想是不是)

于是,我们初始化a1~an,b1~bm全为-1,枚举a1,然后从给你那个串的起始位置找,如果ai和bj都已经确定就只需要判断一下,如果确定两者其一,则根据是否整除求出另一个(注意判断是否<=9)。

注意边界条件。如果有符合的,立刻跳出。

好好思考一下上面的思路。

代码:

#pragma comment(linker,"/STACK:10240000,10240000") //·À±¬Õ»
#include
#define ll long long
#define maxn 300010
using namespace std;
int n,m,k;
int a[maxn],b[maxn];
int ans,tmp,cnt,flag,len;
vectorvc[maxn];
char s[maxn];
void print()
{
    flag=1;
    for(int i=0;i9) return;
                pos+=2;add(ni,nj);
            }
            else return;
        }
        else if(a[ni]==-1&&b[nj]!=-1)
        {
            if(b[nj]==0)
            {
                if(num==0) {pos++;add(ni,nj);}
                else return;
            }
            else if(num==0) {a[ni]=0;pos++;add(ni,nj);}
            else if(num%b[nj]==0&&(num/b[nj]<=9)) {a[ni]=num/b[nj];pos++;add(ni,nj);}
            else if(pos+19) return;
                pos+=2;add(ni,nj);
            }
            else return;
        }
        else return;
    }
    if(ni==n&&pos==len) print();
}
int main()
{
    for(int i=0;i<=81;i++)
    vc[i].clear();
    vc[0].push_back(0);
    for(int i=0;i<=81;i++)
    for(int j=1;j<=9;j++)
    if(i%j==0&&((i/j)<=9))vc[i].push_back(j);
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        //getchar();
        scanf("%s",s);
        len=strlen(s);
        flag=0;
        int tp=(s[0]&15);
        int tmp=tp*10+(s[1]&15);
        for(int i=0;i1)
        for(int i=0;i

 

你可能感兴趣的:(思维,枚举)