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)
Problem Source
ZSUACM Team Member
本题思路较为简单,题目给出一个序列,然后再给一个很大的数字,然后用序列中的每一个数字对大数取模,按照题目要求输出结果即可。
这道题的难点在于这个很大的数字有400位,超过了可以定义的整形数字的范围,需要用字符串来定义这个数字,所以不能用通常的直接取模方法。突破口在于,序列中的数字都是在可表示范围内的,所以不需要使用高精度取模,使用模拟竖式运算的方式运算即可,原理如下:
从大数字的最高数位开始,依次进行取模运算,可以看到最常规的竖式运算就是这样一个过程,对于大数字x,我们可以采取相同的做法,从最高位开始,取模,然后乘以10再加上后一位,重复这一过程直到最后一位,得到的最终结果即为大数字取模的结果。
代码如下:
#include<iostream>
using namespace std;
int b[1000006];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>b[i];
}
string x;
cin>>x;
int len=x.size();
cout<<"(";
for(int i=0;i<n;i++)
{
int r=0;
for(int j=0;j<x.size();j++)
{
r=r*10+(x[j]-'0');
r=r%b[i];
}
cout<<r;
if(i<n-1) cout<<",";
}
cout<<")"<<endl;
}
return 0;
}