高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem
P1932 A+B & A-B & A*B & A/B Problem
题目背景
这个题目很新颖吧!!!
题目描述
求A、B的和差积商余!
输入输出格式
输入格式:
两个数两行
A B
输出格式:
五个数
和 差 积 商 余
输入输出样例
输入样例#1:
1
1
输出样例#1:
2
0
1
1
0
说明
length(A),length(B)<=10^4
每个点3s。
题目链接
很明显,这道题是一道模板题,是很明显的高精度算法。当我翻阅《算法竞赛入门经典(第二版)》时,帅气的汝佳哥告诉我在代码仓库中有减法、乘法、除法的代码,但是当我来到代码仓库时,却被眼前的景象惊呆了
并没有什么减乘除!!
那这简直是一把鼻涕一把眼泪啊!还要自己思考、自己想算法。没办法,于是打了一份模板(调试了好久啊喂(╯‵□′)╯︵┻━┻)。但可怕的是,发生了如下情况:

七十分代码:[真正的模板]
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#define ll long long
#define INF 2147483647
#define ll_INF 9223372036854775807
using namespace std;
struct BigInteger {
static const int BASE = 10000;
static const int WIDTH = 4;
int size, s[11000];
inline void killzero() {
while( s[size - 1] == 0 && size > 1 ) size --;
}
inline void clear() {
size = 0; s[0] = 0;
}
inline void reverse() {
for( int i = 0 ; i < size >> 1 ; ++ i ) swap( s[i], s[size - i - 1] );
}
BigInteger( long long num = 0 ) { *this = num; }
BigInteger operator = ( long long num ) {
clear();
do {
s[size ++] = num % BASE;
num /= BASE;
} while( num > 0 );
return *this;
}
BigInteger operator = ( const string &str ) {
clear();
int x, len = ( str.length() - 1 ) / WIDTH + 1;
for( int i = 0 ; i < len ; ++ i ) {
int end = str.length() - i * WIDTH;
int start = max( 0, end - WIDTH );
sscanf( str.substr( start, end - start ).c_str(), "%d", &x );
s[size ++] = x;
}
return *this;
}
BigInteger operator + ( const BigInteger &b ) const {
BigInteger c;
c.clear();
for( int i = 0, g = 0 ; ; ++ i ) {
if( g == 0 && i >= size && i >= b.size ) break;
int x = g;
if( i < size ) x += s[i];
if( i < b.size ) x += b.s[i];
c.s[c.size ++] = x % BASE;
g = x / BASE;
}
c.killzero();
return c;
}
BigInteger operator - ( const BigInteger &b ) const {
BigInteger c;
c.clear();
for( int i = 0, g = 0 ; ; ++ i ) {
if( g == 0 && i >= size && i >= b.size ) break;
int x = -g;
if( i < size ) x += s[i];
if( i < b.size ) x -= b.s[i];
g = 0;
while( x < 0 ) { x += BASE ; ++ g; }
c.s[c.size ++] = x;
}
while( c.s[c.size - 1] == 0 && c.size > 1 ) c.size --;
return c;
}
BigInteger operator * ( const BigInteger &b ) const {
BigInteger c;
c.clear();
for( int i = 0 ; i < b.size ; ++ i ) {
BigInteger t;
t.clear();
if( b.s[i] == 0 ) continue;
for( int j = 0, g = 0 ; j < size || g != 0; ++ j ) {
int x = g;
if( j < size ) x += b.s[i] * s[j];
t.s[t.size ++] = x % BASE;
g = x / BASE;
}
BigInteger tmp;
tmp.clear();
for( int j = 0, g = 0 ; ; ++ j ) {
if( g == 0 && j >= t.size + i && j >= c.size ) break;
int x = g;
if( j < c.size ) x += c.s[j];
if( j >= i && j < t.size + i ) x += t.s[j - i];
tmp.s[tmp.size ++] = x % BASE;
g = x / BASE;
}
c = tmp;
}
c.killzero();
return c;
}
BigInteger operator / ( const BigInteger &b ) const {
BigInteger c, t;
c.clear(); t.clear();
for( int i = size - 1 ; i >= 0 ; -- i ) {
t = t * BASE + s[i];
int x = 0;
while( b <= t ) { t -= b; x ++ ; }
c.s[c.size ++] = x;
}
c.reverse();
c.killzero();
return c;
}
BigInteger operator % ( const BigInteger &b ) const {
BigInteger c;
c.clear();
for( int i = size - 1 ; i >= 0 ; -- i ) {
c = c * BASE + s[i];
while( b <= c ) c -= b;
}
c.killzero();
return c;
}
BigInteger operator += ( const BigInteger &b ) {
*this = *this + b;
return *this;
}
BigInteger operator -= ( const BigInteger &b ) {
*this = *this - b;
return *this;
}
BigInteger operator *= ( const BigInteger &b ) {
*this = *this * b;
return *this;
}
BigInteger operator /= ( const BigInteger &b ) {
*this = *this / b;
return *this;
}
BigInteger operator %= ( const BigInteger &b ) {
*this = *this % b;
return *this;
}
BigInteger operator ++ () {
*this = *this + 1;
return *this;
}
BigInteger operator ++ (int) {
BigInteger old = *this;
++ (*this);
return old;
}
BigInteger operator -- () {
*this = *this - 1;
return *this;
}
BigInteger operator -- (int) {
BigInteger old = *this;
++ (*this);
return old;
}
bool operator < ( const BigInteger &b ) const {
if( size != b.size ) return size < b.size;
for( int i = size - 1 ; i >= 0 ; -- i )
if( s[i] != b.s[i] ) return s[i] < b.s[i];
return false;
}
bool operator > ( const BigInteger &b ) const { return b < *this; }
bool operator <= ( const BigInteger &b ) const { return !( b < *this ); }
bool operator >= ( const BigInteger &b ) const { return !( *this < b ); }
bool operator != ( const BigInteger &b ) const { return b < *this || *this < b ;}
bool operator == ( const BigInteger &b ) const { return !( b < *this ) && !( *this < b ); }
void read() {
clear();
char s1[11000];
scanf( "%s", s1 );
int x, len_of_s1 = strlen( s1 ), rem = len_of_s1 % WIDTH , point = 1 - WIDTH;
for( int i = len_of_s1 - 1 ; i >= rem ; -- i ) {
x = x * 10 + s1[i + point] - '0'; point += 2;
if( ( len_of_s1 - i ) % WIDTH == 0 ) { s[size ++] = x; x = 0; point = 1 - WIDTH; }
}
if( rem != 0 ){
int x = 0;
for( int i = 0 ; i < rem ; ++ i ) x = x * 10 + s1[i] -'0';
s[size ++] = x;
}
}
void writeln() {
printf( "%d", s[size - 1] );
for(int i = size - 2 ; i >= 0 ; -- i ) printf( "%04d", s[i] );
putchar( '\n' );
}
};
BigInteger A, B;
int main(){
A.read(); B.read();
BigInteger add = A + B, cut = max( A, B ) - min( A, B ), mul = A * B, div = A / B, rem = A % B;
add.writeln(); cut.writeln(); mul.writeln(); div.writeln(); rem.writeln();
return 0;
}
为什么只有七十分?思考之后才发现,其实在做高精度除法的时候,就可以顺带着输出余数,这样才能真好踩着时间点过。
运行截图:

代码:(主程序)
BigInteger A, B;
int main(){
A.read(); B.read();
BigInteger add = A + B, cut = max( A, B ) - min( A, B ), mul = A * B;
add.writeln(); cut.writeln(); mul.writeln();
BigInteger c, t;
c.clear(); t.clear();
for( int i = A.size - 1 ; i >= 0 ; -- i ) {
t = t * BigInteger::BASE + A.s[i];
int x = 0;
while( B <= t ) { t = t - B; x ++ ; }
c.s[c.size ++] = x;
}
for( int i = 0 ; i <= ( c.size - 1 ) >> 1 ; ++ i ) swap( c.s[i], c.s[c.size - i - 1] );
while( c.s[c.size - 1] == 0 && c.size > 1 ) c.size --;
c.writeln(); t.writeln();
return 0;
}
高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem的更多相关文章
- C++高精度模板
原文地址:http://blog.csdn.net/wall_f/article/details/8373395 原文只附代码,没有解析,本文增加了一些对代码的解释. 请注意:本模板不涉及实数运算与负 ...
- [Template]高精度模板
重新写一下高精度模板(不要问我为什么) 自认为代码风格比较漂亮(雾 如果有更好的写法欢迎赐教 封装结构体big B是压位用的进制,W是每位长度 size表示长度,d[]就是保存的数字,倒着保存,从1开 ...
- [note]高精度模板
高精度模板 先定义一个struct struct gj{ int l,s[N]; bool fh; void Print(){ if(fh)putchar('-'); for(int i=l;i> ...
- 高精度模板 支持各种运算 c++
绪言 自从有了高精度模板,妈妈再也不用怕我不会打高精度了! 代码 代码长度与日俱增啊~~~ #include<iostream> #include<cstring> #incl ...
- 高精度模板 洛谷Luogu P1932 A+B & A-B & A*B & A/B Problem
P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...
- Java 大数、高精度模板
介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...
- JAVA高精度模板
刚开始还坚持用C++写高精来着,后来发现JAVA写高精方便太多了,所以也来学习一下JAVA高精度的模板. 参考:https://www.cnblogs.com/imzscilovecode/p/883 ...
- [SinGuLaRiTy] 复习模板-高精度模板
[SinGuLaRiTy-1042] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 结构体封装 //高精度运算 注意%I64d与%lld # ...
- XDOJ 1046 - 高精度模板综合测试 - [高精度模板]
题目链接:http://acm.xidian.edu.cn/problem.php?id=1046 题目描述 请输出两个数的和,差,积,商,取余.注意不要有前导零. 输入 多组数据,每组数据是两个整数 ...
随机推荐
- 在vhd中安装win7,并建立分差vhd
准备:硬盘分区激活第一个分区; imagex.exe; install.wim; winpe boot pc 1.cmd命令下,创建主vhd (1)diskpart (打开dis ...
- webstorm常用快捷键及插件
子曰:工欲善其事,必先利其器.那么问题来了,前端开发用什么比较好? 我反正用的是webstorm,之前也花了一些时间看看别人的使用方式.下面分类介绍一下. 常用快捷键: double shift : ...
- Linux下修改键盘映射
一篇关于修改键盘映射比较靠谱的文章,收藏一下! 原文地址:http://www.07net01.com/2016/04/1436249.html --------------------------- ...
- Unity 5 Stats窗口
Unity5的 Statistics上的统计信息和Unity4 有一些区别, Statistics窗口,全称叫做 Rendering Statistics Window,即渲染统计窗口(或渲染数据统计 ...
- Android中支持的距离单位
px(像素):每个px对应屏幕上的一个点 dip或dp(device independent pixels,设备独立像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dip=1px.但 ...
- C# Linq to SQL — Group by
需求是需要统计数据库中表某一列的总数量,同时以List的形式返回到UI层. Linq to SQL中的Group by用法如下: IList<Unit.HandleCountClass> ...
- 变形transform的副作用
前面的话 变形transform本来是一个用来处理移动.旋转.缩放和倾斜等基本操作的CSS3属性,但该属性除了完成其本职工作之后,还对普通元素造成了意想不到的影响,本文将详细介绍transform ...
- Hadoop权威指南:通过distcp并行复制
Hadoop权威指南:通过distcp并行复制 distcp是一个分布式复制程序,改程序可以从Hadoop文件系统间复制大量数据,也可以将大量的数据复制到Hadoop中 distcp的典型应用是在两个 ...
- C# OpenFileDialog 使用
OpenFileDialog ofd = new OpenFileDialog(); //设置标题 ofd.Title = "选择文件"; //是否保存上次打开文件的位置 ofd. ...
- 基于ssh反向代理实现的远程协助
本文描述了怎么通过ssh反向代理实现远程协助,并提供了相关代码. 可满足web开启远程协助功能后,维护人员能够通过ssh和http登录客户机器(包括在nat环境下) web开启该功能后,ssh才能登录 ...