結果

問題 No.265 数学のテスト
ユーザー tnakao0123tnakao0123
提出日時 2016-03-31 13:51:10
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,077 bytes
コンパイル時間 686 ms
コンパイル使用メモリ 87,900 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-04-10 06:45:19
合計ジャッジ時間 2,565 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,680 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,944 KB
testcase_04 RE -
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 RE -
testcase_10 AC 3 ms
6,944 KB
testcase_11 RE -
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 RE -
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1 ms
6,944 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,944 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 */

/* typedef */

typedef vector<int> vi;

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

  void plus(const Func &f) {
    for (int i = 0; i <= d; i++) as[i] += f.as[i];
  }
  void mul(int 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("%d", as[i]);
    }
    putchar('\n');
  }
};

/* global variables */

/* subroutines */

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

int num(string &s, int &pos) {
  int 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