結果

問題 No.3476 {2^n-1}-gon
コンテスト
ユーザー 37zigen
提出日時 2026-03-21 00:01:17
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 10,178 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,280 ms
コンパイル使用メモリ 105,196 KB
実行使用メモリ 71,768 KB
最終ジャッジ日時 2026-03-21 00:01:27
合計ジャッジ時間 5,140 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntFunction;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.LongToDoubleFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import java.util.random.RandomGenerator;
import java.util.stream.IntStream;
import java.util.stream.Stream;

class FastScanner {
    private static FastScanner instance = null;

    private final InputStream in = System.in;

    private final byte[] buffer = new byte[1024];

    private int ptr = 0;

    private int buflen = 0;

    private FastScanner() {
    }

    public static FastScanner getInstance() {
        if (instance == null) {
            instance = new FastScanner();
        }
        return instance;
    }

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }
        ptr = 0;
        try {
            buflen = in.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buflen > 0;
    }

    private int readByte() {
        if (hasNextByte()) {
            return buffer[ptr++];
        } else {
            return -1;
        }
    }

    private boolean isPrintableChar(int c) {
        return (33 <= c) && (c <= 126);
    }

    public boolean hasNext() {
        while (hasNextByte() && (!isPrintableChar(buffer[ptr]))) {
            ptr++;
        } 
        return hasNextByte();
    }

    public long nextLong() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        while ((b >= '0') && (b <= '9')) {
            // n = n * 10 + (b - '0');
            n = ((n << 1) + (n << 3)) + (b - '0');
            b = readByte();
        } 
        return minus ? -n : n;
    }

    public int nextInt() {
        return ((int) (nextLong()));
    }
}

class MergeFiles {}

class Zn {
    final long mod;

    public Zn(long mod) {
        this.mod = mod;
    }

    /**
     * *
     * 剰余を取り、0以上mod未満の値を返す。
     *
     * @param a
     * @return  */
    public long reduce(long a) {
        a %= mod;
        if (a < 0) {
            a += mod;
        }
        return a;
    }
}

class ArrayUtils {
    public static void swap(long[] A, long[] B) {
        if (A.length != B.length) {
            throw new AssertionError();
        }
        for (int i = 0; i < A.length; i++) {
            long tmp = A[i];
            A[i] = B[i];
            B[i] = tmp;
        }
    }
}

class MyPrintWriter extends PrintWriter {
    private static MyPrintWriter instance = null;

    private MyPrintWriter() {
        super(System.out);
    }

    public static MyPrintWriter getInstance() {
        if (instance == null) {
            instance = new MyPrintWriter();
        }
        return instance;
    }

    public void println(boolean[][] a) {
        for (int i = 0; i < a.length; i++) {
            println(a[i], " ");
        }
    }

    public void println(boolean[] a, String separator) {
        for (int i = 0; i < a.length; ++i) {
            super.print((a[i] ? 1 : 0) + (i == (a.length - 1) ? "\n" : separator));
        }
    }
}

class Fp extends Zn {
    public Fp(long mod) {
        super(mod);
    }

    long[] fac = new long[0];

    long[] ifac = new long[0];

    long[] inv = new long[0];

    public void expand(int n) {
        fac = new long[n];
        ifac = new long[n];
        inv = new long[n];
        Arrays.fill(fac, 1);
        Arrays.fill(ifac, 1);
        Arrays.fill(inv, 1);
        for (int i = 2; i < n; ++i) {
            fac[i] = (i * fac[i - 1]) % mod;
            inv[i] = mod - (((mod / i) * inv[((int) (mod % i))]) % mod);
            ifac[i] = (inv[i] * ifac[i - 1]) % mod;
        }
    }

    public long ifac(int n) {
        if (ifac.length <= n) {
            expand(Math.max(2 * ifac.length, n + 1));
        }
        return ifac[n];
    }

    public long pow(long a, long n) {
        return MathUtils.modPow(a, n, mod);
    }
}

class MathUtils {
    public static long modPow(long a, long n, long mod) {
        if (n < 0) {
            long inv = MathUtils.modInv(a, mod);
            return MathUtils.modPow(inv, -n, mod);
        }
        if (n == 0) {
            return 1;
        }
        return (MathUtils.modPow((a * a) % mod, n / 2, mod) * ((n % 2) == 1 ? a : 1)) % mod;
    }

    /**
     * 拡張ユークリッドの互除法で逆元を求める。
     *
     * @param a
     * @param mod
     * @return  */
    public static long modInv(long a, long mod) {
        a = ((a % mod) + mod) % mod;
        long[] f0 = new long[]{ 1, 0, mod };
        long[] f1 = new long[]{ 0, 1, a };
        while (f1[2] != 0) {
            long q = f0[2] / f1[2];
            for (int i = 0; i < 3; i++) {
                f0[i] -= q * f1[i];
            }
            ArrayUtils.swap(f0, f1);
        } 
        return f0[1] < 0 ? mod + f0[1] : f0[1];
    }
}

public class Main implements Runnable {
    public static void main(String[] args) throws IOException {
        Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1));
        // Runtime runtime = Runtime.getRuntime();
        // new Thread(null, new Main(), "MainThreadWithLargeStack", (1024 * 1024) * 1024).start();
        // new Main().test();
        // new Main().gen();
        new Main().run();
        // long usedMemory	 = runtime.totalMemory() - runtime.freeMemory();
        // System.err.printf("使用メモリ: %.2f MB%n", usedMemory / 1024.0 / 1024.0);
        MyPrintWriter.getInstance().flush();
    }

    @Override
    public void run() {
        FastScanner sc = FastScanner.getInstance();
        MyPrintWriter pw = MyPrintWriter.getInstance();
        long mod = 998244353;
        int N = sc.nextInt();
        int M = sc.nextInt();
        Fp fp = new Fp(mod);
        long ans = 1;
        long q = fp.pow(2, N) - 1;
        q = fp.reduce(q);
        for (int i = 0; i < M; i++) {
            ans = (ans * (q - i)) % mod;
        }
        ans = (ans * fp.ifac(M)) % mod;
        long n = fp.pow(2, N - 1);// (2^n)-1で2^(n-1)-1がすでに占有済み。残り2^(n-1)

        long x = 1;
        for (int i = 1; i < M; i++) {
            x = (x * (n - i)) % mod;
        }
        x = (x * q) % mod;
        x = (x * fp.ifac(M)) % mod;
        ans -= x * M;
        ans = fp.reduce(ans);
        pw.println(ans);
    }
}


// --- Original Code ---
// package template;
// 
// import java.io.IOException;
// import java.nio.file.Files;
// import java.nio.file.Path;
// import java.util.Arrays;
// import java.util.List;
// import java.util.Random;
// 
// import library.tools.FastScanner;
// import library.tools.MergeFiles;
// import library.tools.MyPrintWriter;
// import library.util.Fp;
// import library.util.MathUtils;
// import library.util.polynomial.PolynomialFp;
// 
// public class Main implements Runnable {
// 
// 	public static void main(String[] args) throws IOException {
// //		Runtime runtime = Runtime.getRuntime();
// //		new Thread(null, new Main(), "MainThreadWithLargeStack", (1024 * 1024) * 1024).start();
// //		new Main().test();
// //        new Main().gen();
// 		new Main().run();
// //        long usedMemory	 = runtime.totalMemory() - runtime.freeMemory();
// //        System.err.printf("使用メモリ: %.2f MB%n", usedMemory / 1024.0 / 1024.0);
// 		MyPrintWriter.getInstance().flush();
// 		MergeFiles.export();
// 	}
// 
// 	@Override
// 	public void run() {
// 		FastScanner sc = FastScanner.getInstance();
// 		MyPrintWriter pw = MyPrintWriter.getInstance();
// 		long mod=998244353;
// 		
// 		
// 		int N=sc.nextInt();
// 		int M=sc.nextInt();
// 		
// 		Fp fp=new Fp(mod);
// 		
// 		long ans=1;
// 		long q=fp.pow(2, N)-1;
// 		q=fp.reduce(q);
// 		for (int i = 0; i < M; i++) {
// 			ans=ans*(q-i)%mod;
// 		}
// 		ans=ans*fp.ifac(M)%mod;
// 		long n=fp.pow(2, N-1);//(2^n)-1で2^(n-1)-1がすでに占有済み。残り2^(n-1)
// 		long x=1;
// 		for (int i = 1; i < M; i++) {
// 			x=x*(n-i)%mod;
// 		}
// 		x=x*q%mod;
// 		x=x*fp.ifac(M)%mod;
// 		ans-=x*M;
// 		ans=fp.reduce(ans);
// 		pw.println(ans);
// 	}
// 
// 	void abc() {
// 		Random rnd = new Random();
// 		try {
// 			List<String> candidates = Files.readAllLines(Path.of("problems.txt")).stream()
// 					.filter(line -> !line.contains("o")).map(line -> line.split("\\s+")[0]).toList();
// 
// 			String problem = candidates.get(rnd.nextInt(candidates.size()));
// 			System.out.println(problem);
// 
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 	}
// 
// 	void tr(Object... objects) {
// 		System.out.println(Arrays.deepToString(objects));
// 	}
// }
// 
0