結果
問題 | No.1017 Reiwa Sequence |
ユーザー |
![]() |
提出日時 | 2022-08-30 15:36:15 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 647 ms / 2,000 ms |
コード長 | 2,430 bytes |
コンパイル時間 | 2,423 ms |
コンパイル使用メモリ | 78,644 KB |
実行使用メモリ | 117,344 KB |
最終ジャッジ日時 | 2024-11-07 10:11:13 |
合計ジャッジ時間 | 47,639 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 50 |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int size = Math.min(22, n); int[] values = new int[size]; for (int i = 0; i < size; i++) { values[i] = sc.nextInt(); } int[] dp = new int[1 << size]; HashMap<Integer, Integer> exist = new HashMap<>(); for (int i = 1; i < (1 << size); i++) { for (int j = 0; j < size; j++) { if ((i & (1 << j)) == 0) { continue; } dp[i] = dp[i ^ (1 << j)] + values[j]; break; } if (exist.containsKey(dp[i])) { int x = exist.get(dp[i]); StringBuilder sb = new StringBuilder(); for (int j = 0; j < size; j++) { if (j > 0) { sb.append(" "); } if ((x & (1 << j)) == 0 && (i & (1 << j)) != 0) { sb.append(-values[j]); } else if ((x & (1 << j)) != 0 && (i & (1 << j)) == 0) { sb.append(values[j]); } else { sb.append("0"); } } for (int j = size; j < n; j++) { sb.append(" 0"); } System.out.println("Yes"); System.out.println(sb); return; } else { exist.put(dp[i], i); } } System.out.println("No"); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }