結果
| 問題 |
No.265 数学のテスト
|
| コンテスト | |
| ユーザー |
anta
|
| 提出日時 | 2015-08-07 22:36:48 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 6,615 bytes |
| コンパイル時間 | 1,032 ms |
| コンパイル使用メモリ | 92,444 KB |
| 実行使用メモリ | 12,032 KB |
| 最終ジャッジ日時 | 2024-07-18 05:11:05 |
| 合計ジャッジ時間 | 3,670 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 15 RE * 17 |
ソースコード
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
namespace parse {
typedef const char *Pos;
struct ParseError {
Pos p_;
std::stringstream *ss_;
ParseError(const Pos &p): p_(p), ss_(new std::stringstream()) { }
ParseError(const ParseError &that): p_(0), ss_(0) { *this = that; }
ParseError &operator=(const ParseError &that) {
delete ss_;
p_ = that.p_;
ss_ = new std::stringstream(that.ss_->str());
return *this;
}
~ParseError() { delete ss_; }
template<typename T> ParseError &operator<<(const T &t) {
*ss_ << t;
return *this;
}
friend std::ostream &operator<<(std::ostream &o, const ParseError &e) {
o << e.ss_->str() << " at: ";
Pos q = e.p_;
for(int k = 0; *q && k < 20; ++ q, ++ k)
o << *q;
if(*q) o << "...";
else o << "(end)";
return o;
}
};
inline bool cond(bool b, Pos &p) {
if(b) ++ p;
return b;
}
inline bool rewind(int k, bool b, Pos &p) {
if(!b) p -= k;
return b;
}
inline bool rw0(bool b, Pos &p) { return b; }
inline bool rw1(bool b, Pos &p) { return rewind(1, b, p); }
inline bool rw2(bool b, Pos &p) { return rewind(2, b, p); }
inline bool optional(bool) { return true; }
inline bool expect(bool b, const Pos &p) {
if(!b) throw ParseError(p) << "parse error";
return b;
}
inline bool char_(char c, Pos &p) {
return cond(c == *p, p);
}
inline bool string_(const char *str, Pos &p) {
Pos o = p;
for(const char *s = str; *s; ++ s, ++ p) {
if(*s != *p) {
p = o;
return false;
}
}
return true;
}
};
using namespace parse;
bool natural(Pos &p, int &res) {
if(!isdigit(*p)) return false;
int c = 0;
while(isdigit(*p)) {
c = c * 10 + (*p - '0');
++ p;
}
res = c;
return true;
}
bool number(Pos &p, int &res) {
if(char_('-', p)) {
expect(natural(p, res), p);
res = -res;
return true;
}else
return natural(p, res);
}
struct Polynomial {
typedef long long Coef; typedef Coef Val;
vector<Coef> coef; //... + coef[2] x^2 + coef[1] x + coef[0]
Polynomial() {}
explicit Polynomial(int n): coef(n) {}
static Polynomial One() {
Polynomial r(1);
r.coef[0] = 1;
return r;
}
bool iszero() const { return coef.empty(); }
int degree1() const { return coef.size(); } //degree + 1
int resize(int d) { if(degree1() < d) coef.resize(d); return d; }
const Coef operator[](int i) const {
return i >= degree1() ? Coef() : coef[i];
}
void canonicalize() {
int i = coef.size();
while(i > 0 && coef[i-1] == Coef()) i --;
coef.resize(i);
}
Val evalute(Val x) const {
int d = degree1();
Val t = 0, y = 1;
rep(i, d) {
t += y * coef[i];
y *= x;
}
return t;
}
Polynomial &operator+=(const Polynomial &that) {
int d = resize(that.degree1());
for(int i = 0; i < d; i ++) coef[i] += that[i];
canonicalize();
return *this;
}
Polynomial operator+(const Polynomial &that) const { return Polynomial(*this) += that; }
Polynomial &operator-=(const Polynomial &that) {
int d = resize(that.degree1());
for(int i = 0; i < d; i ++) coef[i] -= that[i];
canonicalize();
return *this;
}
Polynomial operator-(const Polynomial &that) const { return Polynomial(*this) -= that; }
Polynomial operator-() const {
int d = degree1();
Polynomial res(d);
for(int i = 0; i < d; i ++) res.coef[i] = - coef[i];
return res;
}
//naive
Polynomial operator*(const Polynomial &that) const {
if(iszero() || that.iszero()) return Polynomial();
int x = degree1(), y = that.degree1(), d = x + y - 1;
Polynomial res(d);
rep(i, x) rep(j, y)
res.coef[i+j] += coef[i] * that.coef[j];
res.canonicalize();
return res;
}
//long division
pair<Polynomial, Polynomial> divmod(const Polynomial &that) const {
int x = degree1() - 1, y = that.degree1() - 1;
int d = max(0, x - y);
Polynomial q(d + 1), r = *this;
for(int i = x; i >= y; i --) {
Coef t = r.coef[i] / that.coef[y];
q.coef[i - y] = t;
assert(t * that.coef[y] == r.coef[i]);
r.coef[i] = 0;
if(t == 0) continue;
for(int j = 0; j < y; j ++)
r.coef[i - y + j] -= t * that.coef[j];
}
q.canonicalize(); r.canonicalize();
return make_pair(q, r);
}
Polynomial operator/(const Polynomial &that) const { return divmod(that).first; }
Polynomial operator%(const Polynomial &that) const { return divmod(that).second; }
Polynomial differentiate() const {
int d = degree1();
Polynomial q(d - 1);
for(int i = 0; i < d - 1; i ++)
q.coef[i] = coef[i+1] * Coef(i+1);
return q;
}
};
bool expr(Pos &p, Polynomial &res);
bool factor2(Pos &p, Polynomial &res) {
int x;
if(char_('d', p)) {
expect(char_('{', p), p);
expect(expr(p, res), p);
expect(char_('}', p), p);
res = res.differentiate();
return true;
}else if(char_('x', p)) {
res.coef.assign(2, 0);
res.coef[1] = 1;
return true;
}else if(natural(p, x)) {
res.coef.assign(1, 0);
res.coef[0] = x;
return true;
}else {
return false;
}
}
bool factor(Pos &p, Polynomial &res) {
Polynomial r;
if(!factor2(p, res)) return false;
while(char_('*', p)) {
expect(factor2(p, r), p);
res = res * r;
}
return true;
}
bool expr(Pos &p, Polynomial &res) {
Polynomial r;
if(!factor(p, res)) return false;
while(char_('+', p)) {
expect(factor(p, r), p);
res += r;
}
return true;
}
int main() {
int N, d;
while(cin >> N >> d) {
string S;
cin >> S;
Pos p = S.c_str();
Polynomial r;
expect(expr(p, r), p);
r.canonicalize();
rep(i, d+1) {
if(i != 0) putchar(' ');
printf("%lld", r[i]);
}
puts("");
}
return 0;
}
anta