1020. Big Integer
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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)
这题主要要我们处理大叔取模的问题,其实还是很简单的。有取模公式:(a+b)%p 相当于 (a%p + b%p)%p
所以大数取模可以用递推的方式实现。从最高位开始取模,然后*10,加上下一位的数再取模,一直到这个数的个位截止。
本题完整实现代码如下:
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <queue>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <set>
using namespace std;
int mod(string bigNum,int n)
{
int l = bigNum.size(),tem = 0;
for(int i=0;i<l;i++)
tem = (tem*10 + bigNum[i] - '0') % n;
return tem;
}
void solve()
{
int n,num[101];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
string bigNum;
cin >> bigNum;
printf("(%d",mod(bigNum,num[0]));
for(int i=1;i<n;i++)
printf(",%d",mod(bigNum,num[i]));
printf(")\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
solve();
}
return 0;
}