1020. Big Integer(高精度运算)

原创 2015年11月21日 00:22:26
  1. Big Integer
    Total: 11039 Accepted: 3632 Rating:
    2.7/5.0(43 votes)

Time Limit: 1sec Memory Limit:32MB
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)

c语言

#include <stdio.h>
#include <string.h>
int mod(char dividend[], int divisor) {
    int p = 0;
    int temp = dividend[p++] - '0';
    while (1) {
        while (temp < divisor) {
            if (p == (strlen(dividend)) {
                return temp;
            } else {
                temp = temp * 10 + dividend[p++] - '0';
            }
        }
        temp %= divisor;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        for (int i = 0; i < n; i++) {
        int m;
        int base[100];
        char bigint[400];
        scanf("%d", &m);
        for (int i = 0; i < m; i++) {
            scanf("%d", &base[i]);
        }
        scanf("%s", bigint);
        printf("(");
        for (int i = 0; i < m; i++) {
            printf("%d", mod(bigint, base[i]));
            if (i != m - 1) {
                printf(",");
            }

        }
            printf(")\n");
    }
    }
    return 0;
}

但是不幸超时了hahhh

cpp语言 可以正常通过。

#include<iostream>
#include<string>
using namespace std;
int mod(string dividend,int divisor)
{
    int p = 0;//对被除数的位数的指针
    int temp = dividend[p++] - '0';
    while(1)
    {
        while(temp < divisor)
        {
            if(p == dividend.size())    return temp;//如果指针超过被除数最低位还不能找到比除数小的数跳出,此时temp为答案
            temp = temp * 10 + dividend[p++] - '0';
        }
        temp %= divisor;
    }
}
int main()
{
    int t,n,b[102];
    string num;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i = 0;i < n;++i) cin >> b[i];
        cin >> num;
        cout << "(";
        for(int i = 0;i < n;++i)
        {
            cout << mod(num,b[i]);
            if(i != n - 1)  cout <<",";
        }
        cout << ")" << endl;
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

分水岭算法 matlab的三种实现方法

本文转自: http://blog.csdn.net/caiye917015406/article/details/7726628 clear,clc%三种方法进行分水岭分割 %读入图像 fi...

如何在服务器上展示你的你的网页--no!!!用github也可以

前言 经常会有人问我如何才能将自己做的静态页面放到网上供他人欣赏,是不是需要自己有一个服务器,是不是还要搞个域名才能访问?对于以上问题我都会回答:用github来展示你的前端页面吧。 工欲善其事,必先...

1020. Big Integer 回归了 高精度模运算 ans = (ans*10 + (b[j]-'0')) % a[i]

1020. Big Integer Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description ...

1020. Big Integer

一直没写过技术博客,此博客到大三才开实在是晚。 此题为“算法分析与设计”的作业第一题,是sicily题库里的编号为1020的题目。 早上老师课上刚说分治法,看题目要求是大数求模,估计用int之类的...
  • Er_mu
  • Er_mu
  • 2014年09月23日 17:58
  • 381

算法学习【2】 - 1020. Big Integer

题目:http://soj.me/1020 1020. Big Integer Description Long long ago, there was a su...
  • BetaBin
  • BetaBin
  • 2012年03月15日 13:39
  • 1133

Sicily 1020 Big Integer

1020. Big Integer Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Long long a...

[sicily] 1020. Big Integer

1020. Big IntegerConstraintsTime Limit: 1 secs, Memory Limit: 32 MBDescriptionLong long ago, there w...

sicily 1020 Big Integer

1020. Big Integer Sample Input 2 3 2 3 5 10 4 2 3 5 7 13 Sample Output (0,1,0) (1,1,3,6) ...

Sicily1020——Big Integer

1020. Big Integer Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Long long a...

UVa 10220 I Love Big Numbers ! (简单高精度)

10220 - I Love Big Numbers ! Time limit: 3.000 seconds  http://uva.onlinejudge.org/index.php?optio...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:1020. Big Integer(高精度运算)
举报原因:
原因补充:

(最多只允许输入30个字)