1. 程式人生 > >HDU - 1061-快速冪簽到題

HDU - 1061-快速冪簽到題

快速冪百度百科:快速冪就是快速算底數的n次冪。其時間複雜度為 O(log₂N), 與樸素的O(N)相比效率有了極大的提高。

HDU - 1061

程式碼實現如下:

import java.util.Scanner;

public class Main
{
    public static void main(String []args)
    {
        Scanner cin = new Scanner(System.in);
        int T = cin.nextInt();
        for(int i = 0; i < T; i++)
        {
            
int N = cin.nextInt(); System.out.println(Search(N)); } } static int Search(int N)//快速冪的模板一般都與下面的程式碼相同 { int ans = 1; int temp = N; while(N != 0) { if((N & 1) != 0) { ans = (ans%10)*(temp%10); } temp
= (temp%10) * (temp%10); N = N>>1; } ans = ans%10; return ans; } }