大数求模 sicily 1020

I get ideas
 
 
Unauthorized xieyizun
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; } }

  

转载于:https://www.cnblogs.com/xieyizun-sysu-programmer/p/3990090.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值