hdu2035快速幂取模

与hdu1061快速幂取模一致。

import java.util.Scanner;

public class hdu2035 {
    public static int quickmi(int x,int y){
        int ans = 1;
        while (y!=0){
            if (y%2==1){
                ans = (ans%1000)*(x%1000)%1000;
                x = (x%1000)*(x%1000)%1000;
                y = y/2;
            }else{
                x = (x%1000)*(x%1000)%1000;
                y = y/2;
            }
        }
        return ans;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
            if (n==0&&m==0){
                break;
            }
            System.out.println(quickmi(n,m));
        }
    }
}

 

posted @ 2025-02-18 13:48  XiaohuangTX  阅读(2)  评论(0)    收藏  举报