Sicily 1020. Big Integer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sicily 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)



题目大意:

输入一系列被除数b[n]和一个大数s,求这个大数除以这一系列被除数所得的余数。

 

解题思路:

大数问题,用字符串来保存数,将数值的整体情况转化成每个位的情况。

在这道题中,就是将这个大数除以某个数得到的余数,转化成每个位除以某个数的余数情况。

举个例子,求76除以5的余数。

可以先算7除以5的余数,是2;

再加入一位6,将2左移(×10),再加10,得到26,除以5的余数,是1;

于是,结果是1;

 

为什么可以先算7除以5的余数?因为7可以分解成5*1+2,5*1的部分被左移了之后,仍然是5的倍数,所以不会产生余数,会产生余数的是2这个部分。

 

1)字符串s保存这个大数

cin>>s;

2)数组保存一系列的被除数

for(j = 0; j <n; j++)
     cin>>b[j];

3)数组保存每一次移位计算得到的余数情况,移位完成后,便得到了最终的余数;

for(char *p = s; *p != 0; p++)
{
    for(j = 0; j<n; j++)
        m[j] = (m[j]*10 + *p -0)%b[j];
}
    

 

全部代码:

#include <iostream>

using namespace std;

int main()
{
//  freopen("1020.txt","r",stdin);
    int t,n,b[100],i,j;
    char s[401];
    while(cin>>t)
    {
        for(i = 0; i<t; i++)
        {
            cin>>n;
            for(j = 0; j <n; j++)
                cin>>b[j];
            cin>>s;
            int m[100] = {0};
            for(char *p = s; *p != 0; p++)
            {
                for(j = 0; j<n; j++)
                    m[j] = (m[j]*10 + *p -0)%b[j];
            }
            cout<<"(";
            for(j = 0; j <n; j++)
            {
                cout<<m[j];
                if(j == n-1) break;
                else cout<<",";
            }
            cout<<")"<<endl;
        }
    }
    return 0;   
}                           

运行情况:

技术分享

还可以优化,迟点再说。

 

2016-06-29 00:09:46

 

以上是关于Sicily 1020. Big Integer的主要内容,如果未能解决你的问题,请参考以下文章

hdu 6067Big Integer

[生成函数][DFT][NTT] Hdu P6067 Big Integer

在 C++ 中转换 Big Integer <-> double

Sicily 1021 Couple

Sicily 1176 Two Ends

Sicily 1046 Plane Spotting