zoukankan      html  css  js  c++  java
  • sicily 1020. 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)
    #include <iostream>
    #include <string>
    #include <string.h>
    using namespace std;
                 
    int* findMod(string str, int* arr, int len) {
        int size = str.size();
        int* arrt = new int[len];
        memset(arrt, 0, sizeof(int) * len);
                 
        for (int i = 0; i != size; ++i) {
           for (int j = 0; j != len; ++j) {
             arrt[j] = (arrt[j] * 10 + (str[i] - '0')) % arr[j]; 
           }   
        }        
        return arrt;
    }            
                 
    int main(int argc, char* argv[])
    {            int T, n, *arr;
        string x;
        cin >> T;
        while (T--) {
           cin >> n;
           arr = new int[n];
           for (int i = 0; i != n; ++i)
              cin >> arr[i];
           cin >> x;
           int *result = findMod(x, arr, n); 
           cout << "(";
           for (int i = 0; i != n - 1; i++)
              cout << result[i] << ",";
           cout << result[n - 1] << ")" << endl;
        }      
                 
        return 0;
    } 

     因为给的空间还是很大的,所以我在mod的时候用空间换时间,其实这样实现是不好的,因为申请的空间根本没释放。下面这样的话比较好一点,时间上也只是差了0.07多

  • 相关阅读:
    Hive介绍和Hive环境搭建
    数学【p1658】 购物
    背包【p1858】 多人背包(次优解 or 第k优解)
    Manacher【p4555】 [国家集训队]最长双回文串
    Manacher【p1659】 [国家集训队]拉拉队排练
    数学【p2117】 小z的矩阵
    数学【p2613】 【模板】有理数取余(费马小定理)
    贪心+数学【p3156】 [CQOI2011]分金币 ([HAOI2008]糖果传递)
    线段树【 bzoj3132 】【p4145 】上帝造题的七分钟2 / 花神游历各国
    ST表【p1311】 选择客栈
  • 原文地址:https://www.cnblogs.com/xiezhw3/p/3989284.html
Copyright © 2011-2022 走看看