码迷,mamicode.com
首页 > 其他好文 > 详细

Sicily 1020. Big Integer

时间:2016-06-29 01:12:16      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/cenyunxian/p/5625405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!