大数求模 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|% ...
随机推荐
- NBUT 1225 NEW RDSP MODE I
找出循环周期即可了 #include<bits/stdc++.h> using namespace std; int N,M,X; int time(int x,int y,int z) ...
- 大一C语言结课设计之《学生信息管理系统》
第一次写这么长的程序,代码仅供參考,有问题请留言. /* ** 学生信息管理系统 ** IDE:Dev-Cpp 4.9.9.2 ** 2014-6-15 */ #include <stdio.h ...
- Mip-Mapping很重要
MipMap这个东东,记得我除了最早在DX9龙书上了解了其基本概念后,以后便再没接触过,因为从创建到使用都是硬件一手包办,所以这个知识点很容易被遗忘和忽视.这几天空闲时恰好发现了一点MipMap引起的 ...
- javascript冷知识
本人很少写博客,所以文笔很不好,如果解释的不够清楚的,欢迎点评 1.+号(一元加操作符): 如果放在数值前的话,对数值不会产生任何影响,不过放在其他的数据类型前面的话,就等于调用number()将他转 ...
- iOS- SQLite3的基本使用
iOS- 简单说说iOS移动客户端SQLite3的基本使用 1.为什么要使用SQLite3? •大量数据需要存储 •管理数据,存储数据 SQLite是一种关系型数据库(也是目前移动客户端的主 ...
- KVO监听数组的变化
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong)NSMutab ...
- jQery无缝滚动效果
思路: 赋值所有li,添加到ul末尾,重新计算ul宽度 每次移动一个固定的值,当超出一半时,将ul拉回原位 以下代码 <!DOCTYPE html> <html> <he ...
- Java Script基础(七) HTML DOM模型
一.HTML DOM. HTML DOM的特性和方法是专门针对HTML的,HTML中的每个节点都是一个对象,通过访问属性和方法的方式,让一些DOM操作更加简便,在HTML DOM中有专门用来处理白哦个 ...
- 一个简单的Python爬虫
写了一个抓taobao图片的爬虫,全是用if,for,while写的,比较简陋,入门作品. 从网页http://mm.taobao.com/json/request_top_list.htm?type ...
- python 基础——常用日志装饰器
from functools import wraps class logit(): def __init__(self, logfile='out.log'): self.log = logfile ...