题目描述
Given a base b and two non-negative base b integers p and m, compute p mod m and print the result as a base b integer. p mod m is defined as the smallest non-negative integer k such that p = a*m + k for some integer a.
输入
Input consists of a number of cases. Each case is represented by a line containing three unsigned integers. The first, b, is a decimal number between 2 and 10. The second, p, contains up to 1000 digits between 0 and b-1. The third,
m, contains up to 9 digits between 0 and b-1. The last case is followed by a line containing 0.
输出
For each test case, print a line giving p mod m as a base-b integer.
样例输入
2 1100 101 10 123456789123456789123456789 1000 0
样例输出
10
789
import java.math.BigInteger;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
if (n == 0)
break;
BigInteger aBigInteger = scanner.nextBigInteger();
BigInteger bBigInteger = scanner.nextBigInteger();
BigInteger a = new BigInteger(aBigInteger.toString(), n);
BigInteger b = new BigInteger(bBigInteger.toString(), n);
BigInteger cBigInteger = a.divide(b);
BigInteger mBigInteger = a.subtract(cBigInteger.multiply(b));
System.out.println(mBigInteger.toString(n));
}
}
}
本文介绍了一种使用Java进行大数运算的方法,通过实例演示了如何计算两个大数p和m之间的模运算p mod m,并展示了解决方案的代码实现。
1090

被折叠的 条评论
为什么被折叠?



