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的更多相关文章

  1. 【数论】卢卡斯定理模板 洛谷P3807

    [数论]卢卡斯定理模板 洛谷P3807 >>>>题目 [题目] https://www.luogu.org/problemnew/show/P3807 [输入格式] 第一行一个 ...

  2. KMP字符串匹配 模板 洛谷 P3375

    KMP字符串匹配 模板 洛谷 P3375 题意 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.(如果 ...

  3. 【模板】LIS模板 洛谷P1091 [NOIP2004提高组]合唱队形 [2017年4月计划 动态规划11]

    以题写模板. 写了两个:n^2版本与nlogn版本 P1091 合唱队形 题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队 ...

  4. P1654 OSU!-洛谷luogu

    传送门 题目背景 原 <产品排序> 参见P2577 题目描述 osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败 ...

  5. 树链剖分模板(洛谷P3384)

    洛谷P3384 #include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " < ...

  6. 【原创】洛谷 LUOGU P3366 【模板】最小生成树

    P3366 [模板]最小生成树 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N ...

  7. 【原创】洛谷 LUOGU P3371 【模板】单源最短路径

    P3371 [模板]单源最短路径 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出 ...

  8. 【原创】洛谷 LUOGU P3373 【模板】线段树2

    P3373 [模板]线段树 2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第 ...

  9. 【原创】洛谷 LUOGU P3372 【模板】线段树1

    P3372 [模板]线段树 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别 ...

随机推荐

  1. hbase报Dead Region Servers

    问题描述: 16010端口启动成功,16020未启动. hbase-root-regionserver-hbase2.log日志: 2019-08-14 16:45:10,552 WARN [Thre ...

  2. C++输出字符指针指向的地址

    int main() { char *s2 = "jwdajkj"; ]; )); printf("%p,%p\n", s3, s1); cout <&l ...

  3. window.location.href后携带参数

    JS文件中: window.location.href后可携带参数,但是不安全,虽然在技术上是可以实现的 1.传参:window.location.href = "RecordCare.as ...

  4. PHPer面试指南-laravel 篇

    简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件. 注册类文件自动加载器 : Laravel通过 composer ...

  5. CentOS 安装开发工具包

    这里使用组安装包,一次性安装所有开发者工具. 1.查看有那些组安装包可用. [root@bogon ~]# yum grouplist | more 2.搜索一下有哪些和 Development 有关 ...

  6. [暑假集训Day1T1]黑暗城堡

    因为D[i]表示i号节点到1号节点的最短路径,所以可以先以1为源点跑一边SPFA,预处理出每个点到1号节点的最短路.之后开始考虑所谓的“最短路径生成树”,在这棵生成树中有以下性质:当fa[i]==no ...

  7. 10、numpy——位运算

    NumPy 位运算 NumPy "bitwise_" 开头的函数是位运算函数. NumPy 位运算包括以下几个函数: 函数 描述 bitwise_and 对数组元素执行位与操作 b ...

  8. Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

    题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. Windows系统时间会偶尔自动回拨吗?

     为什么80%的码农都做不了架构师?->>>    Spring boot 项目 通过日志记录插入sql操作用时 long start2 = System.currentTimeMi ...

  10. Visual Studio 插件ReSharper:代码生成工具

    下载地址:http://www.jetbrains.com/resharper/download/download-thanks.html?code=RSU&platform=windows ...