zoukankan      html  css  js  c++  java
  • 大数求模 sicily 1020

    I get ideas
     
     
    Unauthorizedxieyizun
    You havn't any signature yet.
    Logout
     

    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)

    Problem Source

    ZSUACM Team Member

    Submit
     

    // Problem#: 1020 // Submission#: 2930409 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stack> using namespace std; int remainder(int n, int p, stack<int>*); int main() { int t; int bs[100]; int factors[100]; int num_b; int b; string x; //字符串保存大数 cin >> t; while (t-- > 0) { cin >> num_b; for (int i = 0; i < num_b; i++) { cin >> b; bs[i] = b; } cin >> x; for (int i = 0; i < num_b; i++) { int result = 0; //解决超时主要在这步,也就是只用调用一次求余操作函数把全部10^n (n = x.lengt() -1, x.lengt() -2, ...., 1)的 % p全部算出来 //用一个栈来保存,而不用每次都重复计算10^n%p stack<int> st; int first_rd = remainder(x.length()-1, bs[i], &st); //去掉 n = x.length() -2 ,..., 1时重复迭代保存的10^n % p的值,因为这些值均在 10 ^ (x.length()-1) % p步的递归中获得了 while (st.size() > x.length() - 1) { st.pop(); } st.push(first_rd); for (int j = 0; j < x.length(); j++) { //此步用到了 求解大数求余的公式 //(ab mod c) = ((a mod c) * (b mod c)) mod c //(a+b) mod c = (a mod c + b mod c) mod c result += (int)(((x[j]-48) % bs[i]) * st.top()) % bs[i]; st.pop(); } factors[i] = result % bs[i]; } cout << "("; for (int i = 0; i < num_b; i++) { if (i != num_b-1) cout << factors[i] << ","; else cout << factors[i] << ")"; } cout << endl; } return 0; } //递归求余数10^n % p,即 10^n % p = (remainder(n-1,p,st)*(10%p))%p,用的求模公式为(ab mod c) = ((a mod c) * (b mod c))%p int remainder(int n, int p, stack<int> *st) { if (n == 0) return 1; else { //此步记得只递归一次 st->push(remainder(n-1, p, st)); return (st->top()* (10 % p)) % p; } }

      

  • 相关阅读:
    蛙蛙推荐:微软MSDN导航菜单演示(Javascript+CSS2)
    看来偶的WEB软件开发基本功还得再扎实一些
    蛙蛙请教:c#面向对象编程的有些抉择问题?
    蛙蛙请教:偶在CSDN发的好多问题都没有解决呢,我集中了一下,大家给看看
    Hive UDF开发
    ubuntu下设置开机自启动项
    ubuntu 扩展存储空间
    ubuntu 快速回到桌面
    编译Hive/Hadoop总结
    Linux MC——终端环境的文件管理器
  • 原文地址:https://www.cnblogs.com/xieyizun-sysu-programmer/p/3990090.html
Copyright © 2011-2022 走看看