您的位置:首页 > 其它

普及练习场 分治算法 取余运算与快速幂

2018-01-09 17:27 295 查看
题目链接

题意理解

这条题目就是用来验板子的

代码

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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: