欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU1061 Rightmost Digit (快速幂)

程序员文章站 2022-07-05 17:01:25
...

Problem

快速幂模板

原理如下
HDU1061 Rightmost Digit (快速幂)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long n, T;

    public static void main(String[] args) throws IOException {

        T = Long.parseLong(br.readLine());
        for (int i = 0; i < T; i++) {
            n = Long.parseLong(br.readLine());
            System.out.println(qmi(n, n, 10));
        }

    }

    private static long qmi(long a, long k, int q) {
        long res = 1;
        while (k > 0) {
            if ((k & 1) == 1) res = res * a % q;
            k >>= 1;
            a = a * a % q;
        }
        return res;
    }


}

相关标签: 数学算法