大数求模 sicily 1020


- Problem Description
- Solved Number
- 2830
- Submit Number
- 8952
- Statistics
- Source code
- Discuss
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
// 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的更多相关文章
- LightOJ1214 Large Division —— 大数求模
题目链接:https://vjudge.net/problem/LightOJ-1214 1214 - Large Division PDF (English) Statistics Forum ...
- [18/12/3]蓝桥杯 练习系统 入门级别 Fibonacci数列求模问题 题解思路
前言略. 看到这个题目本来应该很高兴的,因为什么,因为太TM的基础了啊! 可是当你用常规方法尝试提交OJ时你会发现..hhh...运行超时..(开心地摇起了呆毛 //Fibonacci数列递归一般问题 ...
- 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU4704Sum 费马小定理+大数取模
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4704 题目大意: 看似复杂,其实就是求整数n的划分数,4=1+1+2和4=1+2+1是不同的.因而可 ...
- HPU 1471:又是斐波那契数列??(大数取模)
1471: 又是斐波那契数列?? 时间限制: 1 Sec 内存限制: 128 MB 提交: 278 解决: 27 统计 题目描述 大家都知道斐波那契数列吧?斐波那契数列的定义是这样的: f0 = 0; ...
- NYOJ-676小明的求助,快速幂求模,快速幂核心代码;
小明的求助 时间限制:2000 ms | 内存限制:65535 KB 难度:2 描述 小明对数学很有兴趣,今天老师出了道作业题,让他求整数N的后M位,他瞬间感觉老师在作弄他,因为这是so easy ...
- 【C语言学习趣事】_33_关于C语言和C++语言中的取余数(求模)的计算_有符号和无符号数的相互转换问题
最近再次复习C++语言,用的教材是<C++ Primer>这本教材, 看到第二章的时候,里面有个问题困扰了我. 于是想上网查查怎么回事, 结果看了很久都没有得到一个满意的答案. 书上有这么 ...
- hdu2302(枚举,大数取模)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2303 题意:给出两个数k, l(4<= k <= 1e100, 2<=l<=1 ...
- 求余VS求模--C语言中表述
之前看帖子,发现许多时候基本上大家都把求模和求余混为一谈了.但实际上二者的概念是有区别的 1. 求余 在C语言中,求余对应的操作符是%,且a%b求余的最后结果总是与a符号相同,最后的数值为|a|% ...
随机推荐
- CSRF 攻击的应对之道--转
http://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/ 简介: CSRF(Cross Site Request Forgery, 跨站域 ...
- linux 关闭显示器命令
首先要解释下DPMS的意思,dpms可以认为是一个显示能源管理系统,一般用于计算机功耗的管理.在linux中有几个选项:To control Energy Star (DPMS) features: ...
- Flume-ng-1.4.0 spooling source的方式增加了对目录的递归检测的支持
因为flume的spooldir不支持子目录文件的递归检测,并且业务需要,所以修改了源码,重新编译 代码修改参考自:http://blog.csdn.net/yangbutao/article/det ...
- T-SQL 之 多表联合更新
1. sqlite 多表更新方法 UPDATE tA SET col1=tB.col1 FROM tableA tA INNER JOIN tableB tB ON tA.col2=tB.col2 这 ...
- iOS 关于流媒体 的初级认识与使用
1.流媒体指在Internet/Intranet中使用流式传输技术的连续时基媒体,如:音频.视频或多媒体文件.流式媒体在播放前并不下载整个文件,只将开始部分内容存入内存,流式媒体的数据流随时传送随时播 ...
- Debian 8 在虚拟环境中安装kivy
mkvirtualenv kivy 或者按官方教程所说: virtualenv --no-site-packages kivyinstall (kivy)~/pythonvenv ᐅ pip list ...
- Android开发 SDK NDK下载
2014.7版本 ADT Bundle http://dl.google.com/android/adt/adt-bundle-windows-x86-20140702.ziphttp://dl.go ...
- Django升级1.6之后出现“Bad Request (400)”错误的解决方案
Django从1.4升级到1.6之后发现之前的网站都无法访问了,会出现“Bad Request (400)”的错误,搜了半天终于找到了解决办法. 解决方法很简单: 在settings.py里面添加: ...
- [转]oracle 11g 忘记 默认用户密码
本文转自:http://blog.csdn.net/huangbiao86/article/details/6595052 首先启动sqlplus 输入用户名:sqlplus / as sysdba ...
- Java POI操作Excle工具类
用到了jxl.jar和poi.jar 一些基本的操作Excel的操作方法: import java.io.File; import java.io.FileInputStream; import ja ...