답안 #599185

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
599185 2022-07-19T11:25:13 Z _martynas ASM (LMIO18_asm) C++11
100 / 100
114 ms 428 KB
#include <bits/stdc++.h>

using namespace std;

#define DEBUG(x) cerr << #x << " = " << x << "\n";

using ll = long long;

const int MXN = 55;

int n;
ll A[MXN];
string B[MXN];
ll X[MXN];

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> A[i] >> B[i];
    }

    if(n == 1) {
        ll x = A[0];
        ll x0 = stoll(B[0]);
        stringstream sstr;
        int commands = 0;
        if(x0 != x) {
            if(x0 > x) {
                sstr << "add " << x0-x << "\n";
                commands++;
            }
            else {
                sstr << "multiply " << 0 << "\n";
                commands++;
                if(x0) {
                    sstr << "add " << x0 << "\n";
                    commands++;
                }
            }
        }
        commands++;
        sstr << "print\n";
        cout << commands << "\n";
        cout << sstr.str();
        return 0;
    }

    const int L = 17;
    string best = "";
    int best_commands = 10000;
    int cnt = 0, ccnt = 0;

    vector<int> bits;

    // b0, b1 - spliting (printing) points
    // never makes sense to multiply by 0:
    // can be changed to mupltiply by 10^x and addition then print
    for(int b = 0; b < (1<<L); b++) {
        int temp = b;
        vector<int> gaps;
        while(temp) {
            gaps.push_back(__builtin_ctz(temp));
            temp >>= __builtin_ctz(temp)+1;
        }
        if(!is_sorted(gaps.begin(), gaps.end())) continue;
        bits.push_back(b);
    }

    cerr << bits.size() << "\n";

    for(int b0 : bits) {
        for(int b1 : bits) {
            for(int k = 0; k < n; k++) {
                X[k] = A[k];
            }
            // ax+b=x0
            // ay+b=y0
            bool flag = false;
            int commands = 0;
            vector<pair<ll, ll>> C;
            int i = 0, j = 0;
            ll x = A[0], y = A[1];
            while(i < B[0].size() || j < B[1].size()) {
                ll x0 = 0, y0 = 0;
                // construct numbers until break or end
                while(i < B[0].size()) {
                    x0 *= 10;
                    x0 += B[0][i]-'0';
                    i++;
                    if(((b0 >> (i-1)) & 1)) {
                        break;
                    }
                }
                while(j < B[1].size()) {
                    y0 *= 10;
                    y0 += B[1][j]-'0';
                    j++;
                    if(((b1 >> (j-1)) & 1)) {
                        break;
                    }
                }
                // ax+b=x0
                // ay+b=y0 | (-1)
                // a(x-y)=x0-y0
                // a = x0-y0/(x-y)
                // b = x0-ax
                // (visos A[i] reiksmes skirtingos)
                if(x == y || (x0-y0)%(x-y) != 0) {
                    flag = true; break;
                }
                ll a = (x0-y0)/(x-y);
                ll b = x0-a*x;
                if(a < 0 || b < 0) {
                    flag = true; break;
                }
                if(a != 1) {
                    commands++;
                }
                if(b != 0) {
                    commands++;
                }
                commands++;
                if(1.0*a*x > 2e18 || 1.0*a*y > 2e18) {
                    flag = true; break;
                }
                x *= a; x += b;
                y *= a; y += b;
                C.push_back({a, b});
            }
            if(flag) continue;
            if(commands < best_commands) {
                for(int k = 0; k < n; k++) {
                    string s = "";
                    for(auto p : C) {
                        if(1.0*X[k]*p.first > 2e18) {
                            flag = true;
                            break;
                        }
                        X[k] *= p.first, X[k] += p.second;
                        s += to_string(X[k]);
                    }
                    if(B[k] != s) {
                        flag = true;
                    }
                    if(flag) break;
                }
                if(flag) continue;
                best_commands = commands;
                best = "";
                for(auto p : C) {
                    if(p.first != 1) {
                        best += "multiply " + to_string(p.first) + "\n";
                    }
                    if(p.second != 0) {
                        best += "add " + to_string(p.second) + "\n";
                    }
                    best += "print\n";
                }
            }
            // found answer
            //return 0;
        }
    }

    if(best == "") {
        cout << "-1\n";
    }
    else {
        cout << best_commands << "\n";
        cout << best;
    }

    return 0;
}
/*
2
1 2
2 3

2
11 1122
22 2244

3
12 783206442672750426
0 6326442672750426
3 241046442672750426
*/

Compilation message

asm.cpp: In function 'int main()':
asm.cpp:84:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |             while(i < B[0].size() || j < B[1].size()) {
      |                   ~~^~~~~~~~~~~~~
asm.cpp:84:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |             while(i < B[0].size() || j < B[1].size()) {
      |                                      ~~^~~~~~~~~~~~~
asm.cpp:87:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |                 while(i < B[0].size()) {
      |                       ~~^~~~~~~~~~~~~
asm.cpp:95:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |                 while(j < B[1].size()) {
      |                       ~~^~~~~~~~~~~~~
asm.cpp:52:9: warning: unused variable 'cnt' [-Wunused-variable]
   52 |     int cnt = 0, ccnt = 0;
      |         ^~~
asm.cpp:52:18: warning: unused variable 'ccnt' [-Wunused-variable]
   52 |     int cnt = 0, ccnt = 0;
      |                  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 71 ms 316 KB Output is correct
6 Correct 36 ms 304 KB Output is correct
7 Correct 65 ms 308 KB Output is correct
8 Correct 70 ms 316 KB Output is correct
9 Correct 91 ms 212 KB Output is correct
10 Correct 70 ms 300 KB Output is correct
11 Correct 66 ms 212 KB Output is correct
12 Correct 73 ms 308 KB Output is correct
13 Correct 90 ms 312 KB Output is correct
14 Correct 84 ms 316 KB Output is correct
15 Correct 66 ms 304 KB Output is correct
16 Correct 67 ms 304 KB Output is correct
17 Correct 38 ms 312 KB Output is correct
18 Correct 64 ms 300 KB Output is correct
19 Correct 61 ms 212 KB Output is correct
20 Correct 38 ms 308 KB Output is correct
21 Correct 44 ms 316 KB Output is correct
22 Correct 55 ms 308 KB Output is correct
23 Correct 44 ms 312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 316 KB Output is correct
2 Correct 36 ms 304 KB Output is correct
3 Correct 65 ms 308 KB Output is correct
4 Correct 70 ms 316 KB Output is correct
5 Correct 91 ms 212 KB Output is correct
6 Correct 70 ms 300 KB Output is correct
7 Correct 66 ms 212 KB Output is correct
8 Correct 73 ms 308 KB Output is correct
9 Correct 90 ms 312 KB Output is correct
10 Correct 84 ms 316 KB Output is correct
11 Correct 66 ms 304 KB Output is correct
12 Correct 67 ms 304 KB Output is correct
13 Correct 38 ms 312 KB Output is correct
14 Correct 64 ms 300 KB Output is correct
15 Correct 61 ms 212 KB Output is correct
16 Correct 38 ms 308 KB Output is correct
17 Correct 44 ms 316 KB Output is correct
18 Correct 55 ms 308 KB Output is correct
19 Correct 44 ms 312 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 57 ms 308 KB Output is correct
31 Correct 42 ms 324 KB Output is correct
32 Correct 68 ms 312 KB Output is correct
33 Correct 68 ms 304 KB Output is correct
34 Correct 43 ms 304 KB Output is correct
35 Correct 68 ms 304 KB Output is correct
36 Correct 69 ms 340 KB Output is correct
37 Correct 42 ms 424 KB Output is correct
38 Correct 40 ms 312 KB Output is correct
39 Correct 38 ms 308 KB Output is correct
40 Correct 45 ms 304 KB Output is correct
41 Correct 50 ms 308 KB Output is correct
42 Correct 41 ms 304 KB Output is correct
43 Correct 68 ms 300 KB Output is correct
44 Correct 67 ms 308 KB Output is correct
45 Correct 41 ms 312 KB Output is correct
46 Correct 40 ms 300 KB Output is correct
47 Correct 52 ms 312 KB Output is correct
48 Correct 40 ms 320 KB Output is correct
49 Correct 44 ms 428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 308 KB Output is correct
2 Correct 42 ms 324 KB Output is correct
3 Correct 68 ms 312 KB Output is correct
4 Correct 68 ms 304 KB Output is correct
5 Correct 43 ms 304 KB Output is correct
6 Correct 68 ms 304 KB Output is correct
7 Correct 69 ms 340 KB Output is correct
8 Correct 42 ms 424 KB Output is correct
9 Correct 40 ms 312 KB Output is correct
10 Correct 38 ms 308 KB Output is correct
11 Correct 45 ms 304 KB Output is correct
12 Correct 50 ms 308 KB Output is correct
13 Correct 41 ms 304 KB Output is correct
14 Correct 68 ms 300 KB Output is correct
15 Correct 67 ms 308 KB Output is correct
16 Correct 41 ms 312 KB Output is correct
17 Correct 40 ms 300 KB Output is correct
18 Correct 52 ms 312 KB Output is correct
19 Correct 40 ms 320 KB Output is correct
20 Correct 44 ms 428 KB Output is correct
21 Correct 71 ms 316 KB Output is correct
22 Correct 36 ms 304 KB Output is correct
23 Correct 65 ms 308 KB Output is correct
24 Correct 70 ms 316 KB Output is correct
25 Correct 91 ms 212 KB Output is correct
26 Correct 70 ms 300 KB Output is correct
27 Correct 66 ms 212 KB Output is correct
28 Correct 73 ms 308 KB Output is correct
29 Correct 90 ms 312 KB Output is correct
30 Correct 84 ms 316 KB Output is correct
31 Correct 66 ms 304 KB Output is correct
32 Correct 67 ms 304 KB Output is correct
33 Correct 38 ms 312 KB Output is correct
34 Correct 64 ms 300 KB Output is correct
35 Correct 61 ms 212 KB Output is correct
36 Correct 38 ms 308 KB Output is correct
37 Correct 44 ms 316 KB Output is correct
38 Correct 55 ms 308 KB Output is correct
39 Correct 44 ms 312 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 0 ms 212 KB Output is correct
56 Correct 70 ms 316 KB Output is correct
57 Correct 66 ms 308 KB Output is correct
58 Correct 81 ms 316 KB Output is correct
59 Correct 39 ms 312 KB Output is correct
60 Correct 74 ms 312 KB Output is correct
61 Correct 79 ms 212 KB Output is correct
62 Correct 40 ms 212 KB Output is correct
63 Correct 42 ms 312 KB Output is correct
64 Correct 68 ms 320 KB Output is correct
65 Correct 44 ms 324 KB Output is correct
66 Correct 68 ms 304 KB Output is correct
67 Correct 76 ms 308 KB Output is correct
68 Correct 68 ms 308 KB Output is correct
69 Correct 42 ms 212 KB Output is correct
70 Correct 41 ms 320 KB Output is correct
71 Correct 43 ms 304 KB Output is correct
72 Correct 46 ms 320 KB Output is correct
73 Correct 69 ms 312 KB Output is correct
74 Correct 40 ms 212 KB Output is correct
75 Correct 40 ms 304 KB Output is correct
76 Correct 51 ms 316 KB Output is correct
77 Correct 40 ms 428 KB Output is correct
78 Correct 46 ms 312 KB Output is correct
79 Correct 40 ms 212 KB Output is correct
80 Correct 49 ms 308 KB Output is correct
81 Correct 69 ms 308 KB Output is correct
82 Correct 114 ms 324 KB Output is correct
83 Correct 55 ms 316 KB Output is correct
84 Correct 50 ms 328 KB Output is correct
85 Correct 60 ms 328 KB Output is correct
86 Correct 47 ms 304 KB Output is correct