結果

問題 No.2060 AND Sequence
ユーザー tentententen
提出日時 2023-07-25 09:47:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 2,701 bytes
コンパイル時間 3,649 ms
コンパイル使用メモリ 97,152 KB
実行使用メモリ 50,696 KB
最終ジャッジ日時 2024-04-10 02:54:55
合計ジャッジ時間 7,101 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,180 KB
testcase_01 AC 54 ms
50,188 KB
testcase_02 AC 54 ms
50,540 KB
testcase_03 AC 54 ms
50,052 KB
testcase_04 AC 54 ms
50,420 KB
testcase_05 AC 55 ms
50,036 KB
testcase_06 AC 54 ms
50,456 KB
testcase_07 AC 53 ms
50,272 KB
testcase_08 AC 53 ms
50,568 KB
testcase_09 AC 55 ms
50,280 KB
testcase_10 AC 54 ms
50,440 KB
testcase_11 AC 55 ms
50,464 KB
testcase_12 AC 53 ms
50,244 KB
testcase_13 AC 55 ms
50,600 KB
testcase_14 AC 55 ms
50,636 KB
testcase_15 AC 56 ms
50,540 KB
testcase_16 AC 55 ms
50,608 KB
testcase_17 AC 54 ms
50,196 KB
testcase_18 AC 56 ms
50,460 KB
testcase_19 AC 57 ms
50,572 KB
testcase_20 AC 54 ms
50,448 KB
testcase_21 AC 54 ms
50,608 KB
testcase_22 AC 54 ms
50,192 KB
testcase_23 AC 53 ms
50,696 KB
testcase_24 AC 54 ms
50,484 KB
testcase_25 AC 54 ms
50,560 KB
testcase_26 AC 54 ms
50,608 KB
testcase_27 AC 54 ms
50,184 KB
testcase_28 AC 53 ms
50,184 KB
testcase_29 AC 53 ms
50,280 KB
testcase_30 AC 55 ms
50,180 KB
testcase_31 AC 54 ms
50,656 KB
testcase_32 AC 54 ms
50,416 KB
testcase_33 AC 54 ms
50,528 KB
testcase_34 AC 57 ms
50,304 KB
testcase_35 AC 57 ms
50,452 KB
testcase_36 AC 54 ms
50,248 KB
testcase_37 AC 57 ms
50,460 KB
testcase_38 AC 56 ms
50,440 KB
testcase_39 AC 57 ms
50,552 KB
testcase_40 AC 54 ms
50,452 KB
testcase_41 AC 54 ms
50,572 KB
testcase_42 AC 54 ms
50,356 KB
testcase_43 AC 54 ms
50,576 KB
testcase_44 AC 55 ms
50,148 KB
testcase_45 AC 54 ms
50,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        StringBuilder tmp = new StringBuilder();
        while (m > 0) {
            tmp.append(m % 2);
            m >>= 1;
        }
        char[] values = new String(tmp.reverse()).toCharArray();
        int length = values.length;
        int[][] dp = new int[length + 1][length + 1];
        int count = 0;
        for (int i = 1; i <= length; i++) {
            for (int j = 0; j < length; j++) {
                dp[i][j + 1] += dp[i - 1][j];
                dp[i][j + 1] %= MOD;
                dp[i][j] += dp[i - 1][j];
                dp[i][j] %= MOD;
            }
            if (values[i - 1] == '1') {
                dp[i][count]++;
                dp[i][count] %= MOD;
                count++;
            }
        }
        dp[length][count]++;
        long ans = 0;
        for (int i = 0; i <= length; i++) {
            ans += pow(n, i) * dp[length][i] % MOD;
            ans %= MOD;
        }
        System.out.println(ans);
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1) * x % MOD;
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0