Submission #598371

# Submission time Handle Problem Language Result Execution time Memory
598371 2022-07-18T07:54:21 Z _martynas ASM (LMIO18_asm) C++11
26 / 100
14 ms 316 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];
int it[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 = 7;
    string best = "";
    int best_commands = 10000;
    // 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 b0 = 0; b0 < (1<<L); b0++) {
        for(int b1 = 0; b1 < (1<<L); b1++) {
            // ax+b=x0
            // ay+b=y0
            bool flag = false;
            int i = 0, j = 0;
            ll x = A[0], y = A[1];
            stringstream sstr;
            int commands = 0;
            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((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;
                }
//                DEBUG(x);
//                DEBUG(y);
//                DEBUG(x0);
//                DEBUG(y0);
                if(a != 1) {
                    sstr << "multiply " << a << "\n";
                    commands++;
                }
                if(b != 0) {
                    sstr << "add " << b << "\n";
                    commands++;
                }
                sstr << "print\n";
                commands++;
            }
            if(flag) continue;
            if(commands < best_commands) {
                best_commands = commands;
                best = sstr.str();
            }
            // 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
*/

Compilation message

asm.cpp: In function 'int main()':
asm.cpp:65:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             while(i < B[0].size() || j < B[1].size()) {
      |                   ~~^~~~~~~~~~~~~
asm.cpp:65:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             while(i < B[0].size() || j < B[1].size()) {
      |                                      ~~^~~~~~~~~~~~~
asm.cpp:68:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |                 while(i < B[0].size()) {
      |                       ~~^~~~~~~~~~~~~
asm.cpp:76:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |                 while(j < B[1].size()) {
      |                       ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 308 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 304 KB Output is correct
6 Correct 7 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 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 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 308 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 8 ms 212 KB Output is correct
6 Correct 10 ms 312 KB Output is correct
7 Correct 9 ms 212 KB Output is correct
8 Correct 11 ms 212 KB Output is correct
9 Correct 11 ms 212 KB Output is correct
10 Correct 10 ms 304 KB Output is correct
11 Correct 10 ms 212 KB Output is correct
12 Correct 10 ms 212 KB Output is correct
13 Correct 11 ms 308 KB Output is correct
14 Correct 9 ms 212 KB Output is correct
15 Correct 9 ms 212 KB Output is correct
16 Correct 14 ms 312 KB Output is correct
17 Correct 7 ms 212 KB Output is correct
18 Correct 8 ms 212 KB Output is correct
19 Correct 7 ms 212 KB Output is correct
20 Correct 9 ms 212 KB Output is correct
21 Correct 9 ms 308 KB Output is correct
22 Correct 9 ms 300 KB Output is correct
23 Correct 7 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 212 KB Output is correct
2 Correct 10 ms 312 KB Output is correct
3 Correct 9 ms 212 KB Output is correct
4 Correct 11 ms 212 KB Output is correct
5 Correct 11 ms 212 KB Output is correct
6 Correct 10 ms 304 KB Output is correct
7 Correct 10 ms 212 KB Output is correct
8 Correct 10 ms 212 KB Output is correct
9 Correct 11 ms 308 KB Output is correct
10 Correct 9 ms 212 KB Output is correct
11 Correct 9 ms 212 KB Output is correct
12 Correct 14 ms 312 KB Output is correct
13 Correct 7 ms 212 KB Output is correct
14 Correct 8 ms 212 KB Output is correct
15 Correct 7 ms 212 KB Output is correct
16 Correct 9 ms 212 KB Output is correct
17 Correct 9 ms 308 KB Output is correct
18 Correct 9 ms 300 KB Output is correct
19 Correct 7 ms 316 KB Output is correct
20 Correct 0 ms 308 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 304 KB Output is correct
25 Correct 7 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 304 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Incorrect 7 ms 308 KB Output isn't correct
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 308 KB Output isn't correct
2 Halted 0 ms 0 KB -