結果

問題 No.265 数学のテスト
コンテスト
ユーザー omu
提出日時 2015-08-09 00:53:43
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 1,305 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,673 ms
コンパイル使用メモリ 82,840 KB
実行使用メモリ 50,048 KB
最終ジャッジ日時 2026-05-18 11:04:47
合計ジャッジ時間 6,191 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package tmp;

import java.util.Scanner;

public class Main {

	static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		solve();
	}
	static long[] ans;
	static void cast(int D,int d,long num){
		for(int j = D;j > 0;j--){
			num *= d;
			d--;
		}
		if(d < 0)return;
		ans[d] += num;
	}
		
	
	static void solve(){
		String s;
		int n;
		int l = sc.nextInt();
		n = sc.nextInt();
		n++;
		s = sc.next();
		ans = new long[n];
		
		int D = 0;
		
		for(int i = 0;i < l;i++){
			if(i < l-1 && s.substring(i,i+2).equals("d{")){
				D++;i++;continue;
			}
			if(i < l   && s.substring(i,i+1).equals("}")){
				D--; continue;
			}
			int d = 0;
			int num = 1;
			while(true){
				String c = s.substring(i,i+1);
				if(c.equals("d")){
					i--;
					break;
				}
				if(c.equals("+")){
					i++;
					continue;
				}
				if(c.equals("x")){
					d++;
				}else{
					num *= Integer.parseInt(c);
				}
				if(i == s.length()-1 || s.substring(i+1,i+2).equals("}")){
					cast(D,d,num);
					break;
				}
				c = s.substring(i + 1,i + 2);
				if(c.equals("*")){
					i += 2;
				}
				if(c.equals("+")){
					cast(D,d,num);
					i++;
					break;
				}
			}
		}
			
		for(int j = 0;j < n;j++){
			System.out.print(ans[j]);
			System.out.print(" ");
		}
		System.out.println();
	}

}
0