結果

問題 No.265 数学のテスト
ユーザー uafr_csuafr_cs
提出日時 2017-11-03 20:25:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 750 ms / 2,000 ms
コード長 2,849 bytes
コンパイル時間 3,080 ms
コンパイル使用メモリ 79,188 KB
実行使用メモリ 67,036 KB
最終ジャッジ日時 2023-08-16 02:12:18
合計ジャッジ時間 13,647 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 750 ms
61,196 KB
testcase_01 AC 599 ms
61,288 KB
testcase_02 AC 249 ms
61,612 KB
testcase_03 AC 264 ms
67,036 KB
testcase_04 AC 257 ms
66,148 KB
testcase_05 AC 263 ms
64,468 KB
testcase_06 AC 258 ms
65,364 KB
testcase_07 AC 260 ms
64,704 KB
testcase_08 AC 258 ms
62,000 KB
testcase_09 AC 255 ms
64,180 KB
testcase_10 AC 247 ms
63,372 KB
testcase_11 AC 260 ms
64,516 KB
testcase_12 AC 161 ms
58,552 KB
testcase_13 AC 229 ms
64,104 KB
testcase_14 AC 223 ms
63,056 KB
testcase_15 AC 161 ms
58,924 KB
testcase_16 AC 176 ms
58,912 KB
testcase_17 AC 157 ms
58,772 KB
testcase_18 AC 160 ms
58,524 KB
testcase_19 AC 162 ms
58,544 KB
testcase_20 AC 165 ms
58,964 KB
testcase_21 AC 160 ms
58,776 KB
testcase_22 AC 230 ms
65,196 KB
testcase_23 AC 161 ms
58,512 KB
testcase_24 AC 159 ms
58,680 KB
testcase_25 AC 235 ms
62,032 KB
testcase_26 AC 159 ms
58,532 KB
testcase_27 AC 161 ms
58,676 KB
testcase_28 AC 169 ms
58,880 KB
testcase_29 AC 159 ms
58,952 KB
testcase_30 AC 169 ms
58,876 KB
testcase_31 AC 160 ms
58,744 KB
testcase_32 AC 160 ms
58,772 KB
testcase_33 AC 160 ms
58,772 KB
testcase_34 AC 159 ms
60,768 KB
testcase_35 AC 160 ms
58,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static long[] make_x(int D, int deg, long a){
		long[] ret = new long[D];
		ret[deg] = a;
		return ret;
	}
	
	public static long[] diff(int D, long[] term){
		long[] ret = new long[D];
		
		for(int i = 1; i < D; i++){
			ret[i - 1] = i * term[i];
		}
		
		return ret;
	}
	
	public static long[] add(int D, long[] term1, long[] term2){
		long[] ret = new long[D];
		
		for(int i = 0; i < D; i++){
			ret[i] = term1[i] + term2[i];
		}
		
		return ret;
	}
	
	public static long[] parse(int D, char[] chs, int start, int last){
		//System.out.println(start + " " + last + " : " + chs[start]);
		
		if(chs[start] == 'd'){
			// d{ ___ } を評価する
			int kakko_lv = 0, kakko_start = -1, kakko_last = -1;
			for(int pos = start + 1; pos <= last; pos++){
				if(chs[pos] == '{'){ 
					if(kakko_lv == 0){ kakko_start = pos; }
					kakko_lv += 1;
				}else if(chs[pos] == '}'){
					kakko_lv -= 1;
					if(kakko_lv == 0){ kakko_last = pos; break; }
				}
			}
	
			//System.out.println(kakko_start + " " + kakko_last);
			long[] curr_x = diff(D, parse(D, chs, kakko_start + 1, kakko_last - 1));
			//System.out.println(Arrays.toString(curr_x));
			
			if(kakko_last == last){
				return curr_x;
			}else{ // d{ ___ } の後は + しかありえない 
				return add(D, curr_x, parse(D, chs, kakko_last + 2, last));
			}
			
		}else if(chs[start] == 'x' || Character.isDigit(chs[start])){
			int x_count = 0;
			long a_count = 1;
			if(chs[start] == 'x'){
				x_count++;
			}else{
				a_count = Character.getNumericValue(chs[start]);
			}
			
			int plus_begin = -1;
			for(int pos = start + 1; pos <= last; pos++){
				if(chs[pos] == '+'){
					plus_begin = pos; break; 
				}else if(chs[pos] == 'x'){
					x_count++;
				}else if(Character.isDigit(chs[pos])){
					a_count *= Character.getNumericValue(chs[pos]);
				}
			}
			
			final long[] curr_x = make_x(D, x_count, a_count);
			//System.out.println("x : " + Arrays.toString(curr_x));
			
			if(plus_begin < 0){
				return curr_x;
			}else{
				return add(D, curr_x, parse(D, chs, plus_begin + 1, last));
			}
		}else{
			/* ここで error にはなっていないらしい */
			System.out.println("error");
			return make_x(D, 0, 0);
		}
	}
	
	public static void main(final String[] args){
		new Thread(null, new Runnable(){
			@Override
			public void run() {
				_main(args);
			}
		}, "", 100 * 1024 * 1024).start();
	}
	
	public static void _main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int D = sc.nextInt();
		final char[] chs = sc.next().toCharArray();
		
		long[] ret = parse(D + 1, chs, 0, N - 1);
		for(int i = 0; i <= D; i++){
			System.out.print((i == 0 ? "" : " ") + ret[i]);
		}
		System.out.println();
		
	}
}
0