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<bits/stdc++.h>
#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;
vector<int>vc[maxn];
char s[maxn];
void print()
{
    flag=1;
    for(int i=0;i<n;i++)
    printf("%d",a[i]);
    printf(" ");
    for(int i=0;i<m;i++)
    printf("%d",b[i]);
    puts("");
}
void add(int &aa,int &bb)
{
    if(bb==m-1) {bb=0;aa++;}
    else bb++;
}
void jud()
{
    int pos=0,ni=0,nj=0;
    while(ni<n&&pos<len)
    {
        int num=(s[pos]&15);
        int cnt=num*10+(s[pos+1]&15);
        if(a[ni]!=-1&&b[nj]!=-1)
        {
            if(a[ni]*b[nj]==num){pos++;add(ni,nj);}
            else if(pos+1<len&&a[ni]*b[nj]==cnt){pos+=2;add(ni,nj);}
            else return;
        }
        else if(a[ni]!=-1&&b[nj]==-1)
        {
            if(a[ni]==0)
            {
                if(num==0) {pos++;add(ni,nj);}
                else return;
            }
            else if(num==0) {b[nj]=0;pos++;add(ni,nj);}
            else if(num%a[ni]==0&&(num/a[ni]<=9)) {b[nj]=num/a[ni];pos++;add(ni,nj);}
            else if(pos+1<len&&cnt%a[ni]==0)
            {
                b[nj]=cnt/a[ni];
                if(b[nj]>9) 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+1<len&&cnt%b[nj]==0)
            {
                a[ni]=cnt/b[nj];
                if(a[ni]>9) 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;i<vc[tp].size()&&!flag;i++)
        {
            for(int j=0;j<=max(m,n);j++)
            a[j]=b[j]=-1;
            a[0]=vc[tp][i];
            b[0]=tp/a[0];
            jud();
        }
        if(len>1)
        for(int i=0;i<vc[tmp].size()&&!flag;i++)
        {
            for(int j=0;j<=max(m,n);j++)
            a[j]=b[j]=-1;
            a[0]=vc[tmp][i];
            b[0]=tmp/a[0];
            jud();
        }
        if(!flag) puts("Impossible");
    }
    return 0;
}
/*
4
2 2
8101215
3 4
100000001000
2 2
80101215
3 4
1000000010000
*/

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值