#P4282. [AHOI2008] 计算器

    ID: 3216 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>模拟高精度2008各省省选安徽进制

[AHOI2008] 计算器

Description

Keke’s happy journey on Joy Island continues. He wants to buy some souvenirs for his classmates, so he goes to the gift shop and finds an interesting calculator.

This calculator is a special one that supports addition and subtraction of mixed-radix integers (mixed-radix means each digit can have a different base. For example, if the least significant digit uses base 33 and the next digit uses base 55, then 4242 in this system converts to decimal as 4×3+2=144\times 3+2=14).

The shopkeeper notices Keke’s interest and asks: “Kid, if I tell you this calculator supports mixed-radix integers of up to NN digits, and the bases for each digit are x1,x2,,xnx_1,x_2,\ldots,x_n, do you know the maximum integer MM it can represent?” Keke thinks for a while and answers: “The maximum integer MM it can represent is (x1×x2××xn)1(x_1\times x_2\times \cdots\times x_n)-1.”

The shopkeeper is very happy and says: “You are a smart kid. If I give you two mixed-radix integers AA and BB of length NN, compute (A+B)mod(M+1)(A + B)\bmod(M+1) or (AB)mod(M+1)(A - B)\bmod(M+1) as required, and give the answer in the same mixed-radix system. If you get it right, I will give you this calculator.”

This stumps Keke, but he really wants the calculator. Can you help him?

Input Format

  • The first line contains an integer NN, the length of the mixed-radix numbers supported by the calculator.
  • The second line contains NN integers x1,x2,,xNx_1,x_2,\ldots,x_N, the bases of digits 1N1\sim N (from the most significant digit to the least significant digit).
  • The third line contains NN integers A1,A2,,ANA_1,A_2,\ldots,A_N, representing the first operand.
  • The fourth line contains a character opop, representing the operation type.
  • The fifth line contains NN integers B1,B2,,BNB_1,B_2,\ldots,B_N, representing the second operand.

Output Format

If opop is '+', output (A+B)mod(M+1)(A+B)\bmod(M+1). Otherwise, output (AB)mod(M+1)(A-B)\bmod(M+1). Separate digits with a single space, pad with leading zeros to NN digits, and do not print extra spaces before the most significant digit or after the least significant digit.

3
3 2 5
1 1 2
+
0 0 3
2 0 0


Hint

Constraints:

  • For 100%100\% of the testdata, 1N1051\le N \le 10^5, 1<x1,x2,,xN<1001 < x_1,x_2,\ldots,x_N<100.
  • For 30%30\% of the testdata, N9N \le 9, and x1=x2==xN=10x_1 = x_2 = \ldots = x_N = 10.

Translated by ChatGPT 5