普及練習場 分治演算法 取餘運算與快速冪
阿新 • • 發佈:2019-02-06
題意理解
這條題目就是用來驗板子的
程式碼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static class FastScanner {
private BufferedReader br;
private StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String nextToken() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.valueOf(nextToken());
}
}
public static void main(String[] args) {
FastScanner fs = new FastScanner();
long b = fs.nextLong();
long p = fs.nextLong();
long k = fs.nextLong();
System.out.println(b + "^" + p + " mod " + k + "=" + powMod(b, p, k));
}
static long powMod(long b, long p, long k) {
if(p == 0) {
return 1;
}
long res = powMod(b, p / 2, k);
res = res * res % k;
if(p % 2 == 1) {
res = res * b % k;
}
return res;
}
}