Submit

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
 

Sicily Online Judge System(Rev 20120716-961) 
中文 | English | Archives | Help | About 
Copyright © 2005-2012 Informatic Lab in SYSU. All rights reserved.

// 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;
}
}

  

大数求模 sicily 1020的更多相关文章

  1. LightOJ1214 Large Division —— 大数求模

    题目链接:https://vjudge.net/problem/LightOJ-1214 1214 - Large Division    PDF (English) Statistics Forum ...

  2. [18/12/3]蓝桥杯 练习系统 入门级别 Fibonacci数列求模问题 题解思路

    前言略. 看到这个题目本来应该很高兴的,因为什么,因为太TM的基础了啊! 可是当你用常规方法尝试提交OJ时你会发现..hhh...运行超时..(开心地摇起了呆毛 //Fibonacci数列递归一般问题 ...

  3. 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. HDU4704Sum 费马小定理+大数取模

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4704 题目大意: 看似复杂,其实就是求整数n的划分数,4=1+1+2和4=1+2+1是不同的.因而可 ...

  5. HPU 1471:又是斐波那契数列??(大数取模)

    1471: 又是斐波那契数列?? 时间限制: 1 Sec 内存限制: 128 MB 提交: 278 解决: 27 统计 题目描述 大家都知道斐波那契数列吧?斐波那契数列的定义是这样的: f0 = 0; ...

  6. NYOJ-676小明的求助,快速幂求模,快速幂核心代码;

    小明的求助 时间限制:2000 ms  |  内存限制:65535 KB 难度:2 描述 小明对数学很有兴趣,今天老师出了道作业题,让他求整数N的后M位,他瞬间感觉老师在作弄他,因为这是so easy ...

  7. 【C语言学习趣事】_33_关于C语言和C++语言中的取余数(求模)的计算_有符号和无符号数的相互转换问题

    最近再次复习C++语言,用的教材是<C++ Primer>这本教材, 看到第二章的时候,里面有个问题困扰了我. 于是想上网查查怎么回事, 结果看了很久都没有得到一个满意的答案. 书上有这么 ...

  8. hdu2302(枚举,大数取模)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2303 题意:给出两个数k, l(4<= k <= 1e100, 2<=l<=1 ...

  9. 求余VS求模--C语言中表述

    之前看帖子,发现许多时候基本上大家都把求模和求余混为一谈了.但实际上二者的概念是有区别的   1. 求余 在C语言中,求余对应的操作符是%,且a%b求余的最后结果总是与a符号相同,最后的数值为|a|% ...

随机推荐

  1. C++检测一个文件是否存在

    ifstream::is_open - C++ Reference http://www.cplusplus.com/reference/fstream/ifstream/is_open/ // if ...

  2. php模拟多线程

    一:应该知道的: php本身是不支持多线, 但是php的好搭档,apache和linux是支持的,故lamp才是最佳组合,还在使用win服务器的现在知道为什么要用linux吧.既然是模拟的, 就不是真 ...

  3. Tomcat7启动报Error listenerStart错误--转载

    原文地址:http://www.cnblogs.com/nayitian/p/3439336.html 问题 Tomcat7在启动时报错,详细信息如下: 十一月 23, 2013 7:21:58 下午 ...

  4. tachyon 集群安装

    tachyon的集群安装和单机安装差别不大 http://www.cnblogs.com/admln/p/tachyon-local-install.html 不同的地方 1.修改slaves时填入所 ...

  5. 3.6html学习笔记之样式选择

    1.元素选择器 *{padding:0;margin:0;} p,span{} 2.类选择器 *.class{} p.class{} <p class="important class ...

  6. 【阿里云产品公测】在Laravel4框架中使用阿里云OCS缓存

    作者:阿里云用户 supechina Laravel 是我最近用得非常多而且越用就越喜欢的一款PHP框架,由于没有向下兼容的历史包袱,完全面向对象的风格,借助 Facades 优雅的 IoC Cont ...

  7. java的线程中的Runnable

                      在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thr ...

  8. JS 鼠标事件大全

    一般事件 事件 浏览器支持 描述 onClick HTML: 2 | 3 | 3.2 | 4 Browser: IE3 | N2 | O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDb ...

  9. 【MongoDB】使用mongo连接服务器。。。

    使用mongo连接服务器 命令行: ./mongo 主机号:端口号/数据库名 e.g. ./mongo 127.0.0.1:12345/mongodb1 关闭服务器 use admin db.shut ...

  10. 关于ADO.NET 实体数据数据模型无法为Mysql 选择6.0 解决方案

    错误:您的项目引用了最新实体框架:但是,找不到数据链接所需的与版本兼容的实体框架数据库....... 图片: