• <input id="auww4"></input>
  • <input id="auww4"><acronym id="auww4"></acronym></input>
  • <input id="auww4"><u id="auww4"></u></input>
    <object id="auww4"><acronym id="auww4"></acronym></object>
    <menu id="auww4"></menu><input id="auww4"><u id="auww4"></u></input>
    <input id="auww4"><u id="auww4"></u></input>
  • F.A.Q
    Hand In Hand
    Online Acmers
    Forum | Discuss
    Statistical Charts
    Problem Archive
    Realtime Judge Status
    Authors Ranklist
     
         C/C++/Java Exams     
    ACM Steps
    Go to Job
    Contest LiveCast
    ICPC@China
    Best Coder beta
    VIP | STD Contests
    Virtual Contests
        DIY | Web-DIY beta
    Recent Contests
    Author ID 
    Password 
     Register new ID

    Re:java 快速冪路過

    Posted by Fanick at 2020-10-07 15:23:31 on Problem 1061
    (5)  


    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner sc = new Scanner(System.in);
    
    		while (sc.hasNext()) {
    			int a = sc.nextInt();
    			for (int i = 0; i < a; i++) {
    				long w = sc.nextLong();
    				long q = km(w, w, 10);
    				System.out.println(q);
    			}
    		}
    
    	}
    
    	static long km(long a, long b, int c) {//快速冪 a的b次方對c取余
    		a = a % c;
    		long sum = 1;
    		while (true) {
    			if (b == 0) {
    				break;
    			}
    			if (b % 2 == 1) {
    				sum = (sum * a) % c;
    			}
    
    			a = (a * a) % c;
    			b /= 2;
    
    		}
    
    		return sum;
    
    	}
    }
    ----------------------------------------------------------------------------------

    Followed by:


    Post your reply here:

    Author ID
    Password
    Title
    Content  
     
    Hangzhou Dianzi University Online Judge 3.0
    Copyright © 2005-2020 HDU ACM Team. All Rights Reserved.
    Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
    Total 0.015600(s) query 5, Server time : 2020-10-14 09:17:32, Gzip enabled
    棋牌