SOJ 1020

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


 这题要求输出一个大数对多个较小的数的取余结果,较小的数可以直接用int变量储存,大数只能用int数组来存储,最高位存储在int数组的0号位置,依此类推。然后从最高位开始对基底取余,模拟除法的过程最后就可以得到余数。

// Problem#: 1020
// Submission#: 5045619
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
using namespace std;

int longInt[400],answer[100],basis[100];//分别用来存储大数,最后的答案和取余的基底 
string longInteger;//先将大数作为字符串存储 
int n,T;//n为基底的个数,T为例子的个数 

void MOD()
{
    int temp;
    for(int i=0;i<longInteger.size();++i)//将字符串型的大数存入int数组,最高位存入longInt[0],次高位存入longInt[1],依此类推 
        longInt[i]=longInteger[i]-'0';
    for(int i=0;i<n;++i)//用循环模拟列竖式做除法的过程 
    {
        temp=longInt[0];
        for(int j=0;j<longInteger.size()-1;++j)//注意循环次数,小心数组越界 
        {
            temp=temp%basis[i];
            temp=temp*10+longInt[j+1];
        }
        temp=temp%basis[i];
        answer[i]=temp;
    }
}

int main()
{
    cin>>T;
    for(int i=0;i<T;++i)
    {
        cin>>n;
        for(int j=0;j<n;++j)
        {
            cin>>basis[j];
        }
        cin>>longInteger;
        MOD();
        cout<<"(";
        for(int k=0;k<n-1;++k)
        {
            cout<<answer[k]<<',';
            
        }
        cout<<answer[n-1]<<")"<<endl;
    }
    return 0;
}                                 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值