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. cocos2dx实例开发之flappybird(入门版)

    cocos2dx社区里有个系列博客完整地复制原版flappybird的全部特性.只是那个代码写得比較复杂,新手学习起来有点捉摸不透,这里我写了个简单的版本号.演演示样例如以下: watermark/2 ...

  2. andorid

    js内存泄露 三分面加七分水 —— 十分糊涂 膝盖上打瞌睡 —— 自己靠自己 不当家,不知柴米贵:不生子,不知父母恩. 水落现石头,日久见人心. sqllite http://wenku.baidu. ...

  3. 关于OpenGL+GLSL深度贴图采样

    作者:Nin+.Lee 邮箱:lilei9110@gmail.com * 本文属原创,转载请注明出处. 在GLSL中,存在着sampler2D和sampler2DShadow两种2D贴图采样器.在对一 ...

  4. oracle的触发器

    oracle的触发器分为语句级和行级两种类型,在视图上所创建的触发器叫做什么类型的触发器? DML触发器有三类: 1, insert触发器: 2, update触发器: 3, delete触发器: 触 ...

  5. Ubuntu下安装可视化SVN客户端Rabbitvcs

    如果你用过Windows下的tortoisesvn,肯定会感叹,同样是开源程序,为什么这些开源的东西不在开源的系统上先跑呢? 不用着急,那边有个乌龟,这篇有只兔子,只是看了太多的龟兔赛跑的故事,不知到 ...

  6. Golang学习 - sync 包

    ------------------------------------------------------------ 临时对象池 Pool 用于存储临时对象,它将使用完毕的对象存入对象池中,在需要 ...

  7. iOS “请在微信客户端打开链接” UIWebview加载H5页面携带session、cookie、User-Agent信息 设置cookie、清除cookie、设置User-Agent

    公司新开的一个项目..内容基本上是加载H5页面显示..当时觉得挺简单的..后来发现自己掉坑里了..一些心理历程就不说了..说这个项目主要用到的知识点吧..也是自己踩得坑. 首先说说..这个项目上的内容 ...

  8. Java面向对象设计题2

    有感于很多新人都不知道怎么学习软件开发,个人感觉还是因为练习做的太少,软件开发知识想看懂太难了,必须是边读资料边动手练习.莫说是新人,Java老人研究新技术的时候也是边读资料边练习.因此整理和编排了一 ...

  9. [PHP] htaccess 探秘

    .htaccess访问控制(Allow/Deny) 1. 验证是否支持.htaccess 在目录下新建一个.htaccess 文件,随笔输入一串字符(毫无意义),看看什么反应,如果是500错误,说明目 ...

  10. [Java] String.Split 方法的6个重载函数

    String.Split 方法有6个重载函数: 程序代码 1) public string[] Split(params char[] separator) 2) public string[] Sp ...