D - Magic Multiplication(The 2018 ACM-ICPC Asia Qingdao Regional Contest)(找规律+构造)

D - Magic Multiplication(The 2018 ACM-ICPC Asia Qingdao Regional Contest)(找规律+构造)

Time limit:1000 ms
Memory limit:65536 kB
judge:
ZOJ 4061
vjudge

Description

BaoBao is now learning a new binary operation between two positive integers, represented by ⊗ \otimes , 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 A = a 1 a 2 … a n A = a_1a_2 \dots a_n A=a1a2an, where a i a_i ai indicates the i i i-th digit in A A A, and the second integer be B = b 1 b 2 … b m B = b_1b_2 \dots b_m B=b1b2bm, where b i b_i bi indicates the i i i-th digit in B B B. We have A ⊗ B = ∑ i = 1 n ∑ j = 1 m a i b j = a 1 b 1 + a 1 b 2 + ⋯ + a 1 b m + a 2 b 1 + ⋯ + a n b m A \otimes B = \sum\limits_{i=1}^n\sum\limits_{j=1}^m a_ib_j = a_1b_1 + a_1b_2 + \dots + a_1b_m + a_2b_1 + \dots + a_nb_m AB=i=1nj=1maibj=a1b1+a1b2++a1bm+a2b1++anbm Note that the result of a i b j a_ib_j aibj is considered to be a \textbf{string} (without leading zeros if a i b j > 0 a_ib_j > 0 aibj>0, or contains exactly one `0’ if a i b j = 0 a_ib_j = 0 aibj=0), NOT a normal integer. Also, the sum here means \textbf{string concatenation}, NOT the normal addition operation.

For example, 23 ⊗ \otimes 45 = 8101215. Because 8 = 2 × 4 8=2 \times 4 8=2×4, 10 = 2 × 5 10=2 \times 5 10=2×5, 12 = 3 × 4 12=3 \times 4 12=3×4 and 15 = 3 × 5 15=3 \times 5 15=3×5.

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

Input

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

The first line contains two positive integers n n n and m m m ( 1 ≤ n , m ≤ 2 × 1 0 5 1 \le n, m \le 2 \times 10^5 1n,m2×105), where n n n indicates the length of A A A and m m m indicates the length of B B B. 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 C C C without leading zeros, indicating the result of A ⊗ B A \otimes B AB. The length of C C C is no more than 2 × 1 0 5 2 \times 10^5 2×105.

It’s guaranteed that the sum of lengths of C C C over all test cases will not exceed 2 × 1 0 6 2 \times 10^6 2×106.

Output

For each test case output one line.

If there exist such A A A and B B B that A ⊗ B = C A \otimes B = C AB=C, output one line containing two integers A A A and B B B separated by one space. Note that A A A and B B B should be positive integers without leading zeros, the length of A A A should be exactly n n n, and the length of B B B should be exactly m m m.

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

If such A A A and B B B 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 , C A, B, C A,B,C。给你 A A A B B B 的长度和 C C C ,问你是否能还原出 A A A B B B ,如果能就输出 A A A B B B

有如下运算: A ⊗ B = ∑ i = 1 n ∑ j = 1 m a i b j = a 1 b 1 + a 1 b 2 + ⋯ + a 1 b m + a 2 b 1 + ⋯ + a n b m A \otimes B = \sum\limits_{i=1}^n\sum\limits_{j=1}^m a_ib_j = a_1b_1 + a_1b_2 + \dots + a_1b_m + a_2b_1 + \dots + a_nb_m AB=i=1nj=1maibj=a1b1+a1b2++a1bm+a2b1++anbm

例:23 ⊗ \otimes 45 = 8101215. Because 8 = 2 × 4 8=2 \times 4 8=2×4, 10 = 2 × 5 10=2 \times 5 10=2×5, 12 = 3 × 4 12=3 \times 4 12=3×4 and 15 = 3 × 5 15=3 \times 5 15=3×5

题解

难点在于我们在枚举 C C C 时无法确定该取一位数字还是两位数字。

但我们知道,两个长度为 1 1 1 的数字相乘,其最小值是 0 0 0 ,最大值是 81 81 81 ,即乘积的长度不超过 2 2 2 。其次,若乘积的长度是 2 2 2 ,则乘积的十位数字小于两个数字的任意一个;如果乘积的长度是 1 1 1 ,则乘积大于两个数字的任意一个。

所以我们在枚举数字时,只需要判断一下 C C C 的数字和我们枚举的数字的大小就可知道该取 1 1 1 位还是该取两位:如果 C C C 的数字小于枚举数字,就取两位,然后判断是否能整除;如果 C C C 的数字小于枚举数字,就取 1 1 1 位,然后判断是否能整除。

因为 A A A B B B 的长度是已知的,所以我们可以先枚举 A [ 0 ] A[0] A[0] ,然后利用 C C C 的前半部分和 A [ 0 ] A[0] A[0] 枚举 B B B (一个 A [ 0 ] A[0] A[0] 对应 1 1 1 个或 0 0 0 B B B ),然后再利用枚举的 B B B 去推导剩余的 A A A 。如果推导成功就得出了答案,而且因为推导的过程是由大到小的,所以第一个满足要求的答案就是最小的答案。

代码

#include <bits/stdc++.h>
#define maxn 200005
#define _for(i, a) for(int i = 0; i < (a); ++i)
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define scl(x) scanf("%lld", &x)
#define sc(x) scanf("%d", &x)
using namespace std;

int T, n, m;
int a[maxn], b[maxn];
char c[maxn];

bool getb() {
    
    
	int len = strlen(c), pos = 0;
	_for(i, m) {
    
    
		if (pos == len) return 0;					//C不够用了
		int x = c[pos++] - '0';
		if (pos < len && x && x < a[0]) x = x * 10 + c[pos++] - '0';
		if (x % a[0] || x / a[0] > 9) return 0;		//除不尽或者商是两位数
		b[i] = x / a[0];
	}
	_rep(i, 1, n - 1) {
    
    
		_for(j, m) {
    
    
			if (pos == len) return 0;				//C不够用了
			int x = c[pos++] - '0';
			if (pos < len && x && x < b[j]) x = x * 10 + c[pos++] - '0';
			if (x && (b[j] == 0 || j && a[i] == 0)) return 0;	//a和b都为0但c不为0
			if (x == 0) {
    
    
				if (j && a[i] && b[j]) return 0;	//c为0但a和b都不为0;j不为0代表a已经被赋值
				if (!j) a[i] = 0;
			}
			else {
    
    
				if (x % b[j] || j && x / b[j] != a[i] || x / b[j] > 9) return 0;	//除不尽或者商是两位数
				a[i] = x / b[j];
			}
		}
	}
	return pos == len;
}

bool sol() {
    
    
	sc(n), sc(m);
	scanf("%s", c);
	int x = c[0] - '0';
	_rep(i, 1, 9) {
    
    		//利用a[0]找出b
		if (x % i == 0) {
    
    
			a[0] = i;
			if (getb()) return 1;
		}
	}
	x = x * 10 + c[1] - '0';
	_rep(i, 1, 9) {
    
    		//利用b找出a
		if (x % i == 0) {
    
    
			a[0] = i;
			if (getb()) return 1;
		}
	}
	return 0;
}

int main() {
    
    
	while (cin >> T) {
    
    
		_for(i, T) {
    
    
			if (sol()) {
    
    
				_for(j, n) printf("%d", a[j]);
				printf(" ");
				_for(j, m) printf("%d", b[j]);
				printf("\n");
			}
			else printf("Impossible\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42856843/article/details/105452821