2012年12月17日

UVa 10551 - Basic Remains

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
    while (cin.hasNext()) {
      BigInteger b = cin.nextBigInteger();
      if (b.compareTo(BigInteger.ZERO) == 0) break;
      String s1 = cin.next(), s2 = cin.next();
      BigInteger p = BigInteger.ZERO, m = BigInteger.ZERO;
      for (int i = 0; i < s1.length(); i++)
        p = p.multiply(b).add(BigInteger.valueOf(s1.charAt(i) - '0'));
      for (int i = 0; i < s2.length(); i++)
        m = m.multiply(b).add(BigInteger.valueOf(s2.charAt(i) - '0'));
      BigInteger ans = p.mod(m);
      System.out.println(ans.toString(b.intValue()));
    }
  }
}

沒有留言:

張貼留言