Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:算法导论(影印版)P.879[第31章,第6节]

Posted by idgnarn at 2010-11-10 15:20:08 on Problem 1995
In Reply To:算法导论(影印版)P.879[第31章,第6节] Posted by:yark at 2008-03-12 20:56:39
import java.util.Scanner;


public class Main {
    public static int expmod(int n, int e, int m) {
	int r = n % m;
	int rr = 1;
	while(e > 0) {
	    if((e & 1) == 1)
	    	rr = rr * r % m;
	    r = r * r % m;
	    
	    e >>= 1;
	}
	
	
	return rr;
    }
    
    public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	int z = in.nextInt();
	while(z-->0) {
	    int r = 0;
	    int m = in.nextInt();
	    int g = in.nextInt();
	    while(g-->0) {
		int n = in.nextInt();
		int e = in.nextInt();
		r += expmod(n, e, m);
	    }
	    System.out.println(r % m);
	}
	
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator