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. 【剑指offer】Q18:树的子结构

    类似于字符串的匹配,我们总是找到第一个匹配的字符,在继续比較以后的字符是否所有同样,假设匹配串的第一个字符与模式串的第一个不同样,我们就去查看匹配串的下一个字符是否与模式串的第一个同样,相应到这里,就 ...

  2. linux 体系结构知识 博客

    http://blog.csdn.net/haiross/article/category/1488205/3

  3. 终端I/O之终端窗口的大小

    大多数UNIX系统都提供了一种功能,可以对当前终端窗口的大小进行跟踪,在窗口大小发生变化时,使内核通知前台进程组.内核为每个终端和伪终端保存一个winsize结构: Struct winsize { ...

  4. Laravel 5.0 之命令及处理程序

    本文译自 Matt Stauffer 的 系列文章 . 本文中涉及的新功能都是关于 Commands 的,这些特性在 Laravel 旧版本中已经有了,但是在 Laravel 5.0 中变得更加好用了 ...

  5. PowerDesigner 之 PDM建模

    很多码农使用Powerdesigner用来创建数据库,下面来具体记录一下使用该工具创建的过程. 1.首先运行PowerDesigner程序,进入主界面,如下图: 2.菜单栏依次"File→N ...

  6. 1.5.1 Analyzers,Tokenizers,Filters概述

    字段分析器(Analyzers)即用于文档索引也用于查询.一个分析器检查字段的文本,并生成一个token流.分析器可能是一个单独的类,也可能是一系列的tokenizer和filter的组合. 分词器把 ...

  7. Android Studio开发RecyclerView遇到的各种问题以及解决

    是大神总结的,我直接提供链接了,以便以后学习 大神链接: 问题一和解决:http://www.cnblogs.com/yunfang/p/5830953.html 问题二和解决:http://www. ...

  8. nodejs的mysql模块学习(八)关闭连接池

    关闭连接池 可以用pool.end()关闭连接池 pool.end(function (err) { // 所有的连接都已经被关闭 }); 当关闭之后pool将不可以getconnection()

  9. Java、Android中Math详解

    java.math.Math类常用的常量和方法: Math.PI 记录的圆周率 Math.E记录e的常量 Math.abs 求绝对值 Math.sin 正弦函数 Math.asin 反正弦函数 Mat ...

  10. jQuery选择器之层次选择器Demo

    测试代码: 02-层次选择器.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...