結果
問題 | No.2744 Power! or +1 |
ユーザー |
![]() |
提出日時 | 2024-04-22 17:05:13 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,289 bytes |
コンパイル時間 | 2,553 ms |
コンパイル使用メモリ | 80,096 KB |
実行使用メモリ | 142,380 KB |
最終ジャッジ日時 | 2024-10-14 16:13:45 |
合計ジャッジ時間 | 11,277 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 2 |
other | TLE * 1 -- * 8 |
ソースコード
import java.io.*;import java.util.*;import java.util.function.*;import java.util.stream.*;public class Main {public static void main(String[] args) throws Exception {Scanner sc = new Scanner();int n = sc.nextInt();long a = sc.nextInt();long b = sc.nextInt();long c = sc.nextInt();int max = 1;for (int i = 2; pow(b, i) < (n - 1) * a; i++) {max = i;}PriorityQueue<Path> queue = new PriorityQueue<>();queue.add(new Path(1, 0));long[] costs = new long[n * 2];Arrays.fill(costs, Long.MAX_VALUE);while (queue.size() > 0) {Path p = queue.poll();if (costs[p.idx] <= p.value) {continue;}costs[p.idx] = p.value;if (p.idx == 0) {break;}if (p.idx == n) {queue.add(new Path(0, p.value));continue;}queue.add(new Path((p.idx + 1) % (n * 2), p.value + a));if (p.idx >= 2) {for (int i = 2; i <= max; i++) {queue.add(new Path(powmod(p.idx, i, n * 2), p.value + pow(b, i)));}}if (p.idx > 2 && p.idx < n) {queue.add(new Path(factmod(p.idx, n * 2), p.value + c));} else if (p.idx >= n) {queue.add(new Path(0, p.value + c));}}System.out.println(costs[0]);}static long pow(long x, int p) {long ans = 1;for (int i = 1; i <= p; i++) {ans *= x;}return ans;}static int powmod(long x, int p, int mod) {long ans = 1;for (int i = 1; i <= p; i++) {ans *= x;ans %= mod;}return (int)ans;}static int factmod(int x, int mod) {long ans = 1;for (int i = 2; i <= x; i++) {ans *= i;ans %= mod;}return (int)ans;}static class Path implements Comparable<Path> {int idx;long value;public Path(int idx, long value) {this.idx = idx;this.value = value;}public int compareTo(Path another) {return Long.compare(value, another.value);}}}class Scanner {BufferedReader br;StringTokenizer st = new StringTokenizer("");StringBuilder sb = new StringBuilder();public Scanner() {try {br = new BufferedReader(new InputStreamReader(System.in));} catch (Exception e) {}}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}public double nextDouble() {return Double.parseDouble(next());}public String next() {try {while (!st.hasMoreTokens()) {st = new StringTokenizer(br.readLine());}} catch (Exception e) {e.printStackTrace();} finally {return st.nextToken();}}}