結果
| 問題 |
No.265 数学のテスト
|
| コンテスト | |
| ユーザー |
yuruhiya
|
| 提出日時 | 2021-07-03 16:12:00 |
| 言語 | Crystal (1.14.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,416 bytes |
| コンパイル時間 | 14,902 ms |
| コンパイル使用メモリ | 296,472 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-30 08:23:43 |
| 合計ジャッジ時間 | 12,936 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 31 RE * 1 |
ソースコード
# require "/Scanner"
require "io/error"
class Scanner
private def self.skip_to_not_space
peek = STDIN.peek
not_space = peek.index { |x| x != 32 && x != 10 } || peek.size
STDIN.skip(not_space)
end
def self.s
skip_to_not_space
peek = STDIN.peek
if index = peek.index { |x| x == 32 || x == 10 }
STDIN.skip(index + 1)
return String.new(peek[0, index])
end
String.build do |buffer|
loop do
buffer.write peek
STDIN.skip(peek.size)
peek = STDIN.peek
break if peek.empty?
if index = peek.index { |x| x == 32 || x == 10 }
buffer.write peek[0, index]
STDIN.skip(index)
break
end
end
end
end
end
macro input_array(type, args)
Array.new({{args.first}}) do
{% if args.size == 1 %}
input({{type.id}})
{% else %}
input_array({{type}}, {{args[1...args.size]}})
{% end %}
end
end
macro input(type)
{% if type.is_a?(Path) %}
{{type}}.new(Scanner.s)
{% elsif type.is_a?(Var) || (type.is_a?(Call) && type.args.size == 0) %}
{% if Scanner.methods.includes?(type.id) %}
Scanner.{{type.id}}
{% else %}
Scanner.s.to_{{type.id}}
{% end %}
{% elsif type.name == "[]" %}
input_array("{{type.receiver}}", {{type.args}})
{% else %}
input_array("{{type.name}}", {{type.args}})
{% end %}
end
macro input(*types)
{
{% for type in types %}
input({{type}}),
{% end %}
}
end
class Polynomial
getter a : Array(Int32)
def initialize(d : Int)
@a = Array.new(d, 0)
end
def initialize(@a)
end
delegate size, to: @a
delegate :[], to: @a
delegate :[]=, to: @a
def d
Polynomial.new @a[1..].map_with_index(1) { |x, i| x * i } + [0]
end
def x
Polynomial.new [0] + @a[..-2]
end
def +(other : self)
Polynomial.new @a.zip(other.@a).map { |x, y| x + y }
end
def *(val : Int32)
Polynomial.new @a.map { |x| x * val }
end
def to_s(io)
io << @a
end
def inspect(io)
io << @a
end
end
class Solve
getter n : Int32, d : Int32, s : Array(Char), hash : Hash(Int32, Int32)
def initialize
@n, @d = input(i, i)
@d += 1
@s = input(s).chars
@hash = Hash(Int32, Int32).new
stack = Deque(Int32).new
s.each_with_index do |c, i|
if c == '{'
stack << i
elsif c == '}'
start = stack.pop
hash[start + 1] = i - 1
end
end
end
def detect(pos : Int32)
case s[pos]
when 'd'
end_pos = hash[pos + 2]
{solve(pos + 2..end_pos).d, end_pos + 2}
when 'x'
res = Polynomial.new(d)
res[1] = 1
{res, pos + 1}
when '1'..'9'
res = Polynomial.new(d)
res[0] = s[pos].to_i
{res, pos + 1}
else
raise "a"
end
end
def solve(range : Range = 0..n - 1)
first, pos = detect(range.begin)
stack = Deque{first}
while pos < range.end
case s[pos]
when '*'
pos += 1
case s[pos]
when 'x'
now = stack.pop
stack.push now.x
when '1'..'9'
now = stack.pop
stack.push now * s[pos].to_i
else
raise "b"
end
pos += 1
when '+'
now, pos = detect(pos + 1)
stack << now
else
raise "c"
end
end
stack.reduce { |x, y| x + y }
end
end
puts Solve.new.solve.a.join(' ')
yuruhiya