1020. Big Integer
Description
Long long ago, there was a super computer that could deal with VeryLongIntegers(no VeryLongInteger will be negative). Do you know how this computer stores the VeryLongIntegers? This computer has a set of n positive integers: b1,b2,...,bn, which is called a basis for the computer.
The basis satisfies two properties:
1) 1 < bi <= 1000 (1 <= i <= n),
2) gcd(bi,bj) = 1 (1 <= i,j <= n, i ≠ j).
Let M = b1*b2*...*bn
Given an integer x, which is nonegative and less than M, the ordered n-tuples (x mod b1, x mod b2, ..., x mod bn), which is called the representation of x, will be put into the computer.
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains three lines.
The first line contains an integer n(<=100).
The second line contains n integers: b1,b2,...,bn, which is the basis of the computer.
The third line contains a single VeryLongInteger x.
Each VeryLongInteger will be 400 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).
Output
For each test case, print exactly one line -- the representation of x.
The output format is:(r1,r2,...,rn)
Sample Input
2 3 2 3 5 10 4 2 3 5 7 13
Sample Output
(0,1,0) (1,1,3,6)
Problem Source
ZSUACM Team Member
思路:这个,数学不太好,对模运算的一些特性不了解。模运算有如下公式:
(a+b)mod n = ((a mod n)+ (b mod n))mod n
(a-b) mod n = ((a mod n )- (b mod n)+n)mod n
ab mod n = (a mod n) (b mod n) mod n
本题,利用第一个公式即可。
代码:
#include <stdio.h>
#include <string.h>
int main()
{
int i;
int j;
int length;
int size;
int n;
int basis[100];
int representation[100];
char bigInt[401];
scanf("%d", &size);
while (size--)
{
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &basis[i]);
}
scanf("%s", bigInt);
length = strlen(bigInt);
for (i = 0; i < n; i++)
{
representation[i] = 0;
for (j = 0; j < length; j++)
{
representation[i] = (representation[i] * 10 + bigInt[j] - '0') % basis[i];
}
}
printf("(%d", representation[0]);
for (i = 1; i < n; i++)
{
printf(",%d", representation[i]);
}
printf(")\n");
}
return 0;
}