提出 #65880118


ソースコード 拡げる

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
// typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>,
//             rb_tree_tag, tree_order_statistics_node_update>  ordered_set;
//member functions : order_of_key(k){no_of_ele < k} find_by_order(k){kth ele}

const ll M = 1e9 + 7;
const ll N = 1e5 + 69;
const ll lmax = 2e18;
const ll lmin = -2e18;

string multiplyStrings(string s1, string s2) {
    int n1 = s1.size(), n2 = s2.size();
    if (n1 == 0 || n2 == 0)
        return "0";
    int nn = 1, mm = 1;
    if (s1[0] == '-')
        nn = -1;
    if (s2[0] == '-')
        mm = -1;

    int isNeg = nn * mm;
    vector<int> result(n1 + n2, 0);
    int i1 = 0; 
    int i2 = 0; 
    for (int i = n1 - 1; i >= 0; i--) {
        if (s1[i] == '-')
            continue;
        int carry = 0;
        int n1 = s1[i] - '0';
        i2 = 0;
        for (int j = n2 - 1; j >= 0; j--) {
            if (s2[j] == '-')
                continue;
            int n2 = s2[j] - '0';
            int sum = n1 * n2 + result[i1 + i2] + carry;
            carry = sum / 10;
            result[i1 + i2] = sum % 10;
            i2++;
        }
        if (carry > 0)
            result[i1 + i2] += carry;
        i1++;
    }
    int i = result.size() - 1;
    while (i >= 0 && result[i] == 0)
        i--;
    if (i == -1)
        return "0";
    string s = "";

    while (i >= 0)
    s += to_string(result[i--]);
    if (isNeg == -1)
        s = "-" + s;

    return s;
}

string to_str(long long a) {
    string ans = "";
    while (a > 0) {
        char curr = (a % 10) + '0';
        ans += curr;
        a /= 10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

void solve() {
    long long n, k, curr = 1;
    cin >> n >> k;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        string res = multiplyStrings(to_str(curr), to_str(a[i]));
        int limit = res.size();
        if(limit > k) {
            curr = 1;
        } else {
            curr = stoll(res);
        }
    }
    cout << curr;
    return;
}

int main() {
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    // freopen("bunny_hopscotch_input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    solve();
    return 0;
}

提出情報

提出日時
問題 B - Product Calculator
ユーザ jeal0uspengu1n
言語 C++ 20 (gcc 12.2)
得点 200
コード長 2514 Byte
結果 AC
実行時間 1 ms
メモリ 3672 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 200 / 200
結果
AC × 2
AC × 32
セット名 テストケース
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 1 ms 3440 KiB
example_01.txt AC 1 ms 3536 KiB
hand_00.txt AC 1 ms 3536 KiB
hand_01.txt AC 1 ms 3528 KiB
hand_02.txt AC 1 ms 3544 KiB
hand_03.txt AC 1 ms 3392 KiB
hand_04.txt AC 1 ms 3356 KiB
random_00.txt AC 1 ms 3480 KiB
random_01.txt AC 1 ms 3384 KiB
random_02.txt AC 1 ms 3488 KiB
random_03.txt AC 1 ms 3672 KiB
random_04.txt AC 1 ms 3548 KiB
random_05.txt AC 1 ms 3544 KiB
random_06.txt AC 1 ms 3532 KiB
random_07.txt AC 1 ms 3480 KiB
random_08.txt AC 1 ms 3436 KiB
random_09.txt AC 1 ms 3532 KiB
random_10.txt AC 1 ms 3404 KiB
random_11.txt AC 1 ms 3528 KiB
random_12.txt AC 1 ms 3392 KiB
random_13.txt AC 1 ms 3372 KiB
random_14.txt AC 1 ms 3596 KiB
random_15.txt AC 1 ms 3544 KiB
random_16.txt AC 1 ms 3532 KiB
random_17.txt AC 1 ms 3488 KiB
random_18.txt AC 1 ms 3600 KiB
random_19.txt AC 1 ms 3408 KiB
random_20.txt AC 1 ms 3520 KiB
random_21.txt AC 1 ms 3484 KiB
random_22.txt AC 1 ms 3528 KiB
random_23.txt AC 1 ms 3552 KiB
random_24.txt AC 1 ms 3548 KiB