結果

問題 No.2649 [Cherry 6th Tune C] Anthem Flower
ユーザー tenten
提出日時 2024-08-01 14:43:48
言語 Java
(openjdk 23)
結果
AC  
実行時間 689 ms / 2,000 ms
コード長 2,301 bytes
コンパイル時間 5,678 ms
コンパイル使用メモリ 92,372 KB
実行使用メモリ 74,048 KB
最終ジャッジ日時 2024-08-01 14:44:06
合計ジャッジ時間 14,801 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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 t = sc.nextInt();
        String result = IntStream.range(0, t).mapToObj(a -> {
            char[] n = sc.next().toCharArray();
            int mod = sc.nextInt();
            long ans;
            if ((n[n.length - 1] - '0') % 2 == 0) {
                ans = halfValue(n, mod);
                n[n.length - 1]++;
                ans *= fullValue(n, mod);
                ans %= mod;
            } else {
                ans = fullValue(n, mod);
                n[n.length - 1]++;
                ans *= halfValue(n, mod);
                ans %= mod;
            }
            return String.valueOf(ans);
        }).collect(Collectors.joining("\n"));
        System.out.println(result);
    }
    
    static long halfValue(char[] arr, int mod) {
        long ans = 0;
        int next = 0;
        for (char c : arr) {
            int x = c - '0' + next;
            ans *= 10;
            ans += x / 2;
            ans %=  mod;
            next = x % 2 * 10;
        }
        return ans;
    }
    
    static long fullValue(char[] arr, int mod) {
        long ans = 0;
        for (char c : arr) {
            ans *= 10;
            ans += c - '0';
            ans %= mod;
        }
        return ans;
    }
    
}
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