D. Magic Multiplication

                                                        D. Magic Multiplication

Problem - D - Codeforces

 

 思路:我们可以先枚举a1,因为如果a1确定了,那么我们可以求出来b1~bn,并且是唯一的,因为假设a1==1,则b1可取0,1 2 3 4 5 6 7 8 9,那么他对应的str为,0 1 2 3 4 5 6 7 8 9,如果a1==2,那么他对应的str为,0 2 4 6 8 10 12 14 16 18,因为这几个数没有一个数是另一个数的前缀,所以说

他对应的数是唯一的,同理其他a1等于其他数的时候也能够得到这个结论,所以我们就暴力算出来b1~bn然后再判断的图中顺便计算出a2~an同时进行一些细节的判断

// Problem: D. Magic Multiplication
// Contest: Codeforces - The 2018 ICPC Asia Qingdao Regional Programming Contest (The 1st Universal Cup, Stage 9: Qingdao)
// URL: https://codeforces.com/gym/104270/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms

#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<vector> 
#include<set>
#include<unordered_map>
#include<ctime>
#include<cstdlib>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int> > PIII;
const double eps=1e-7;
const int N=2e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
char str[N];
int a[N],b[N];

bool get(int n,int m,int len) {
	int idx=1;
	int last=0;
	for(int i=1;i<=len;i++) {
		last=last*10+str[i]-'0';
		if(last%a[1]==0) {
			b[idx++]=last/a[1];
			last=0;
			if(idx>m) break;
		}
		if(last/10!=0) return false;
	}
	
	if(idx<m) return false;
	
	idx=1;
	for(int i=1;i<=n;i++) {
		if(i==1) {
			int last=a[1]*b[1];
			if(last/10==0) {
				if(str[idx]!=last+'0') return false;
				else idx++;
			}else {
				if(str[idx]!=last/10+'0') return false;
				idx++;
				if(str[idx]!=last%10+'0') return false;
				idx++;
			}
		}else {
			if(idx>len) return false;
			int last=str[idx]-'0';
			idx++;
			if(last%b[1]==0) {
				a[i]=last/b[1];
			}else {
				last=last*10+str[idx]-'0';
				idx++;
				if(last%b[1]==0) {
					a[i]=last/b[1];
				}else return false;
			}
		}
		for(int j=2;j<=m;j++) {
			int last=a[i]*b[j];
			if(last/10==0) {
				if(str[idx]!=last+'0') return false;
				else idx++;
			}else {
				if(str[idx]!=last/10+'0') return false;
				idx++;
				if(str[idx]!=last%10+'0') return false;
				idx++;
			}
			
			if(idx>len&&(i<n||(i==n&&j<m))) return false;
		}
	}
	for(int i=1;i<=n;i++) if(a[i]<0||a[i]>=10) return false;
	for(int i=1;i<=m;i++) if(b[i]<0||b[i]>=10) return false;
	if(idx<=len) return false;
	
	return true;
}

void solve() {
	n=read(),m=read();
	scanf("%s",str+1);
	int len=strlen(str+1);
	
	for(int i=1;i<=9;i++) {
		a[1]=i;
		if(get(n,m,len)) {
			for(int j=1;j<=n;j++) printf("%d",a[j]);
			printf(" ");
			for(int j=1;j<=m;j++) printf("%d",b[j]);
			printf("\n");
			
			return ;
		}
	}
	
	printf("Impossible\n");
}   

int main() {
    // init();
    // stin();

    scanf("%d",&T);
    // T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值