結果

問題 No.265 数学のテスト
ユーザー maine_honzuki
提出日時 2021-05-06 10:18:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,738 bytes
コンパイル時間 2,122 ms
コンパイル使用メモリ 198,712 KB
最終ジャッジ日時 2025-01-21 07:44:23
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://ncode.syosetu.com/n4830bu/265/
#include <bits/stdc++.h>
using namespace std;
using State = string::const_iterator;
using ll = long long;

int N, d;
string S;

vector<ll> expression(State& begin);
vector<ll> term(State& begin);
int number(State& begin);

vector<ll> add(vector<ll> a, vector<ll> b) {
    for (int i = 0; i <= d; i++) {
        a[i] += b[i];
    }
    return a;
}
vector<ll> mul(vector<ll> a, ll b) {
    if (b >= 0)
        for (int i = 0; i <= d; i++) {
            a[i] *= b;
        }
    else {
        for (int i = d; i >= 1; i--) {
            a[i] = a[i - 1];
        }
        a[0] = 0;
    }
    return a;
}
vector<ll> dif(vector<ll> a) {
    for (int i = 0; i < d; i++) {
        a[i] = a[i + 1] * (i + 1);
    }
    a[d] = 0;
    return a;
}

vector<ll> expression(State& begin) {
    vector<ll> ret = term(begin);
    while (*begin == '+') {
        begin++;
        ret = add(ret, term(begin));
    }
    return ret;
}

vector<ll> term(State& begin) {
    vector<ll> ret(d + 1);
    int n = number(begin);
    if (n == -2) {
        begin++;
        ret = dif(expression(begin));
        begin++;
        return ret;
    }
    if (n >= 0)
        ret[0] = n;
    else if (n == -1)
        ret[1] = 1;
    while (*begin == '*') {
        begin++;
        ret = mul(ret, number(begin));
    }
    return ret;
}

int number(State& begin) {
    auto c = *(begin++);
    if (isdigit(c))
        return c - '0';
    else if (c == 'x')
        return -1;
    else if (c == 'd')
        return -2;
    assert(false);
}


int main() {
    cin >> N >> d >> S;
    State state = S.begin();
    auto maine = expression(state);
    for (int i = 0; i <= d; i++) {
        cout << maine[i] << " \n"[i == d];
    }
}
0