結果

問題 No.265 数学のテスト
ユーザー tnakao0123tnakao0123
提出日時 2016-03-31 13:58:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,155 bytes
コンパイル時間 560 ms
コンパイル使用メモリ 85,684 KB
実行使用メモリ 8,800 KB
最終ジャッジ日時 2023-08-16 02:08:48
合計ジャッジ時間 1,825 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
8,800 KB
testcase_01 AC 4 ms
6,340 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,388 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 265.cc: No.265 数学のテスト - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_D = 10;

/* typedef */

typedef long long ll;
typedef vector<ll> vl;

struct Func {
  int d;
  ll as[MAX_D + 1];
  Func() {}
  Func(int _d): d(_d) { memset(as, 0, sizeof(as)); }
  Func(int _d, ll _k): d(_d) { memset(as, 0, sizeof(as)); as[0] = _k; }

  void plus(const Func &f) {
    for (int i = 0; i <= d; i++) as[i] += f.as[i];
  }
  void mul(ll k) {
    for (int i = 0; i <= d; i++) as[i] *= k;
  }
  void mul_x() {
    for (int i = d; i >= 0; i--) as[i + 1] = as[i];
    as[0] = 0;
  }
  void diff() {
    for (int i = 1; i <= d; i++) as[i - 1] = i * as[i];
    as[d] = 0;
  }
  void print() {
    for (int i = 0; i <= d; i++) {
      if (i > 0) putchar(' ');
      printf("%lld", as[i]);
    }
    putchar('\n');
  }
};

/* global variables */

/* subroutines */

Func expr(string &s, int &pos, int d);

ll num(string &s, int &pos) {
  ll r = 0;
  while (pos < s.size() && s[pos] >= '0' && s[pos] <= '9')
    r = r * 10 + s[pos++] - '0';
  return r;
}

Func term(string &s, int &pos, int d) {
  if (s[pos] == 'd') {
    pos += 2; // 'd{'
    Func t0 = expr(s, pos, d);
    pos++; // '}'
    t0.diff();
    return t0;
  }

  Func t0(d, 1);

  if (s[pos] == 'x') t0.mul_x(), pos++;
  else t0.mul(num(s, pos));

  while (pos < s.size() && s[pos] == '*') {
    pos++; // '*'
    if (s[pos] == 'x') t0.mul_x(), pos++;
    else t0.mul(num(s, pos));
  }

  return t0;
}

Func expr(string &s, int &pos, int d) {
  Func e0 = term(s, pos, d);
  while (pos < s.size() && s[pos] == '+') {
    pos++; // '+'
    Func e1 = term(s, pos, d);
    e0.plus(e1);
  }
  return e0;
}

/* main */

int main() {
  int n, d;
  string s;
  cin >> n >> d >> s;

  int pos = 0;
  Func e = expr(s, pos, d);
  e.print();
  return 0;
}
0