結果

問題 No.2744 Power! or +1
ユーザー tentententen
提出日時 2024-04-22 17:05:13
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,289 bytes
コンパイル時間 3,060 ms
コンパイル使用メモリ 79,216 KB
実行使用メモリ 78,076 KB
最終ジャッジ日時 2024-04-22 17:05:28
合計ジャッジ時間 11,271 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
        }
    }
    
}
0