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
 Copy sample input to clipboard
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

我的做法
#include <iostream>
#include <string>
#include <vector> 
using namespace std;


int main()
{
int group;      //定義共有幾組測試數據
cin>>group;              
for(int i=0;i<group;i++)  //用for循環計算各組測試數據
{
int num; //num是該組測試數據basis of computer的個數
vector<int> vb;  //定義向量vb來存儲basis of computer
        int temp;      //讀取basis用
cin>>num;      
for(int y=0;y<num;y++)
{
cin>>temp;
vb.push_back(temp);     //將讀取的basis加入向量vb中
}
string s;       //存放VeryLongInteger,就是大數,這樣不能用int或者long,會溢出
cin>>s;
for(int x=0;x<s.length();x++)
{
s[x]=s[x]-'0';         //將字符變成十進制數字
}
vector<int>::iterator it;  //迭代
int totalm=0;            //記錄結果
cout<<"(";
int tmp;
for(it=vb.begin();it!=vb.end();it++)   //逐一讀取每個basis
{
tmp=*it;
for(int z=0;z<s.length();z++)    //大數取模算法
{
totalm=(totalm*10+s[z])%tmp;
}
cout<<totalm;
if(it!=(vb.end()-1))     //安要求格式輸出
   cout<<",";
totalm=0;                //歸零,進行下一個循環
}
cout<<")";
cout<<endl;
}
return 0;
}











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章