QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#100491#6129. Magic Multiplicationmaomao90#AC ✓18ms3492kbC++172.8kb2023-04-26 16:24:542023-04-26 16:24:57

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-26 16:24:57]
  • 评测
  • 测评结果:AC
  • 用时:18ms
  • 内存:3492kb
  • [2023-04-26 16:24:54]
  • 提交

answer


// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;

int t;
int n, m;
string c;

int getc(int i, int l) {
    int res = 0;
    while (l--) {
        res *= 10;
        res += c[i] - '0';
        i++;
    }
    return res;
}

string a, b;
bool solve(int d) {
    int p = 0;
    a = ""; b = "";
    REP (i, 0, m) {
        int gd = -1;
        if (p < SZ(c) && getc(p, 1) % d == 0) {
            gd = getc(p, 1) / d;
            p++;
        } else if (p + 1 < SZ(c) && getc(p, 2) % d == 0 && getc(p, 2) / d < 10) {
            gd = getc(p, 2) / d;
            p += 2;
        }
        if (gd == -1) {
            return 0;
        }
        b += gd + '0';
    }
    a += d + '0';
    REP (i, 1, n) {
        if (getc(p, 1) == 0) {
            a += '0';
        } else {
            int gd = -1;
            int d = b[0] - '0';
            if (p < SZ(c) && getc(p, 1) % d == 0) {
                gd = getc(p, 1) / d;
            } else if (p + 1 < SZ(c) && getc(p, 2) % d == 0 && getc(p, 2) / d < 10) {
                gd = getc(p, 2) / d;
            }
            if (gd == -1) {
                return 0;
            }
            a += gd + '0';
        }
        REP (j, 0, m) {
            string tmp = to_string((a[i] - '0') * (b[j] - '0'));
            REP (k, 0, SZ(tmp)) {
                if (p >= SZ(c) || tmp[k] != c[p]) {
                    return 0;
                }
                p++;
            }
        }
    }
    return p == SZ(c);
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> t;
    while (t--) {
        cin >> n >> m;
        cin >> c;
        bool fnd = 0;
        REP (d, 1, 10) {
            if (solve(d)) {
                fnd = 1;
                break;
            }
        }
        if (fnd) {
            cout << a << ' ' << b << '\n';
        } else {
            cout << "Impossible\n";
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3260kb

input:

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

output:

23 45
101 1000
Impossible
Impossible

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 18ms
memory: 3492kb

input:

1025
11 18
1461416814188088414188241035153540203545200202010354520510254921495628496328028281449632871435351535402035452002020103545205102500000000000000000000000000004000000063276372366381360363618638136918454921495628496328028281449632871435492149562849632802828144963287143514614168141880884141882...

output:

Impossible
3583 5
161650357972 65354104569
597523997017 7693
Impossible
406723924695110 973937089831524
59331138450754 554
4 189401911962950
980565699171 84748728972992
Impossible
62155650672 4241405
9458752764004792353 8717596993614
Impossible
941952596 49242258343771276739
Impossible
64053045751 4...

result:

ok 1025 lines