1. 程式人生 > >資訊學奧賽一本通(C++版)第二部分 基礎演算法 第一章 高精度計算

資訊學奧賽一本通(C++版)第二部分 基礎演算法 第一章 高精度計算

第一章 高精度計算

模板在最後。

T1307 : 高精度乘法

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1308 : 高精除

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1309 : 迴文數

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1168 : 大整數加法

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1169 : 大整數減法

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1170 : 計算2的N次方

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1171 : 大整數的因子

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1172 : 求10000以內n的階乘

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1173 : 階乘和

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1174 : 大整數乘法

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

T1175 : 除以13

時間限制: 1000 ms 記憶體限制: 65536 KB

【題目描述】

【輸入】

【輸出】

【輸入樣例】

【輸出樣例】

【答案&程式碼】

(未完待續)

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
using std::string;
using std::cin;
using std::cout;
using std::istream;
using std::ostream;
using std::endl;
using std::max;
#define MAX_LENGTH 2005
struct BIG_NUMBER{
	int len,s[MAX_LENGTH];
	bool sign;
	
	BIG_NUMBER(void);
	BIG_NUMBER(const char*);
	BIG_NUMBER(int);
	~BIG_NUMBER(void);
	void clean(void);
	string toStr(void)const;
	friend istream& operator>>(istream&,BIG_NUMBER&);
	friend ostream& operator<<(ostream&,BIG_NUMBER&);
	BIG_NUMBER operator=(const char*);
	BIG_NUMBER operator=(int);
	BIG_NUMBER operator=(const string);
	bool operator>(const BIG_NUMBER&)const;
	bool operator>=(const BIG_NUMBER&)const;
	bool operator<(const BIG_NUMBER&)const;
	bool operator<=(const BIG_NUMBER&)const;
	bool operator==(const BIG_NUMBER&)const;
	bool operator!=(const BIG_NUMBER&)const;
	BIG_NUMBER operator+(const BIG_NUMBER&)const;
	BIG_NUMBER operator++(void);
	BIG_NUMBER operator++(int);
	BIG_NUMBER operator+=(const BIG_NUMBER&);
	BIG_NUMBER operator-(const BIG_NUMBER &) const;
	BIG_NUMBER operator--(void);
	BIG_NUMBER operator--(int);
	BIG_NUMBER operator-=(const BIG_NUMBER&);
	BIG_NUMBER operator*(const BIG_NUMBER &)const;
	BIG_NUMBER operator*(const int num)const;
	BIG_NUMBER operator*=(const BIG_NUMBER&);
	BIG_NUMBER operator/(const BIG_NUMBER&)const;
	BIG_NUMBER operator/=(const BIG_NUMBER&);
	BIG_NUMBER operator%(const BIG_NUMBER&)const;
	BIG_NUMBER factorial(void)const;
	BIG_NUMBER sqrt(void)const;
	BIG_NUMBER pow(const BIG_NUMBER&)const;
};
BIG_NUMBER::BIG_NUMBER(void){
	memset(s, 0, sizeof(s));
	len = 1;
	sign = 1;
}
BIG_NUMBER::BIG_NUMBER(const char*num){
	*this = num;
}
BIG_NUMBER::BIG_NUMBER(int num){
	*this = num;
}
string BIG_NUMBER::toStr(void)const{
	string res;
	res = "";
	for (int i = 0; i < len; i++)
		res = (char)(s[i] + '0') + res;
	if (res == "")
		res = "0";
	if (!sign&&res != "0")
		res = "-" + res;
	return res;
}
istream &operator>>(istream &in,BIG_NUMBER&num){
	string str;
	in>>str;
	num=str;
	return in;
}
ostream &operator<<(ostream &out,BIG_NUMBER&num){
	out<<num.toStr();
	return out;
}
BIG_NUMBER BIG_NUMBER::operator=(const char*num){
	memset(s, 0, sizeof(s));
	char a[MAX_LENGTH] = "";
	if (num[0] != '-')
		strcpy(a, num);
	else
		for (int i = 1; i < strlen(num); i++)
			a[i - 1] = num[i];
	sign = !(num[0] == '-');
	len = strlen(a);
	for (int i = 0; i < strlen(a); i++)
		s[i] = a[len - i - 1] - 48;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator=(int num){
	char temp[MAX_LENGTH];
	sprintf(temp, "%d", num);
	*this = temp;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator=(const string num){
	const char *tmp;
	tmp = num.c_str();
	*this = tmp;
	return *this;
}
bool BIG_NUMBER::operator<(const BIG_NUMBER&num)const{
	if (sign^num.sign)
		return num.sign;
	if (len != num.len)
		return len < num.len;
	for (int i = len - 1; i >= 0; i--)
		if (s[i] != num.s[i])
			return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));
	return !sign;
}
bool BIG_NUMBER::operator>(const BIG_NUMBER&num)const{
	return num < *this;
}
bool BIG_NUMBER::operator<=(const BIG_NUMBER&num)const{
	return !(*this>num);
}
bool BIG_NUMBER::operator>=(const BIG_NUMBER&num)const{
	return !(*this<num);
}
bool BIG_NUMBER::operator!=(const BIG_NUMBER&num)const{
	return *this > num || *this < num;
}
bool BIG_NUMBER::operator==(const BIG_NUMBER&num)const{
	return !(num != *this);
}
BIG_NUMBER BIG_NUMBER::operator+(const BIG_NUMBER &num)const{
	if (sign^num.sign) {
		BIG_NUMBER tmp = sign ? num : *this;
		tmp.sign = 1;
		return sign ? *this - tmp : num - tmp;
	}
	BIG_NUMBER result;
	result.len = 0;
	int temp = 0;
	for (int i = 0; temp || i < (max(len, num.len)); i++) {
		int t = s[i] + num.s[i] + temp;
		result.s[result.len++] = t % 10;
		temp = t / 10;
	}
	result.sign = sign;
	return result;
}
BIG_NUMBER BIG_NUMBER::operator++(void){
	*this = *this + 1;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator++(int){
	BIG_NUMBER old = *this;
	++(*this);
	return old;
}
BIG_NUMBER BIG_NUMBER::operator+=(const BIG_NUMBER &num){
	*this = *this + num;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator-(const BIG_NUMBER &num) const{
	BIG_NUMBER b=num,a=*this;
	if (!num.sign && !sign) {
		b.sign=1;
		a.sign=1;
		return b-a;
	}
	if (!b.sign) {
		b.sign=1;
		return a+b;
	}
	if (!a.sign) {
		a.sign=1;
		b=BIG_NUMBER(0)-(a+b);
		return b;
	}
	if (a<b) {
		BIG_NUMBER c=(b-a);
		c.sign=false;
		return c;
	}
	BIG_NUMBER result;
	result.len = 0;
	for (int i = 0, g = 0; i < a.len; i++) {
		int x = a.s[i] - g;
		if (i < b.len) x -= b.s[i];
		if (x >= 0) g = 0;
		else {
			g = 1;
			x += 10;
		}
		result.s[result.len++] = x;
	}
	result.clean();
	return result;
}
BIG_NUMBER BIG_NUMBER::operator*(const BIG_NUMBER &num)const{
	BIG_NUMBER result;
	result.len = len + num.len;
 
	for (int i = 0; i < len; i++)
		for (int j = 0; j < num.len; j++)
			result.s[i + j] += s[i] * num.s[j];
 
	for (int i = 0; i < result.len; i++) {
		result.s[i + 1] += result.s[i] / 10;
		result.s[i] %= 10;
	}
	result.clean();
	result.sign = !(sign^num.sign);
	return result;
}
BIG_NUMBER BIG_NUMBER::operator*(const int num)const{
	BIG_NUMBER x = num;
	BIG_NUMBER z = *this;
	return x*z;
}
BIG_NUMBER BIG_NUMBER::operator*=(const BIG_NUMBER&num){
	*this = *this * num;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator/(const BIG_NUMBER&num)const{
	BIG_NUMBER ans;
	ans.len = len - num.len + 1;
	if (ans.len < 0) {
		ans.len = 1;
		return ans;
	}
 
	BIG_NUMBER divisor = *this, divid = num;
	divisor.sign = divid.sign = 1;
	int k = ans.len - 1;
	int j = len - 1;
	while (k >= 0) {
		while (divisor.s[j] == 0) j--;
		if (k > j) k = j;
		char z[MAX_LENGTH];
		memset(z, 0, sizeof(z));
		for (int i = j; i >= k; i--)
			z[j - i] = divisor.s[i] + '0';
		BIG_NUMBER dividend = z;
		if (dividend < divid) {
			k--;
			continue;
		}
		int key = 0;
		while (divid*key <= dividend) key++;
		key--;
		ans.s[k] = key;
		BIG_NUMBER temp = divid*key;
		for (int i = 0; i < k; i++)
			temp = temp * 10;
		divisor = divisor - temp;
		k--;
	}
	ans.clean();
	ans.sign = !(sign^num.sign);
	return ans;
}
BIG_NUMBER BIG_NUMBER::operator/=(const BIG_NUMBER&num){
	*this = *this / num;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator%(const BIG_NUMBER& num)const{
	BIG_NUMBER a = *this, b = num;
	a.sign = b.sign = 1;
	BIG_NUMBER result, temp = a / b*b;
	result = a - temp;
	result.sign = sign;
	return result;
}
BIG_NUMBER BIG_NUMBER::pow(const BIG_NUMBER& num)const{
	BIG_NUMBER result = 1;
	for (BIG_NUMBER i = 0; i < num; i++)
		result = result*(*this);
	return result;
}
BIG_NUMBER BIG_NUMBER::factorial(void)const{
	BIG_NUMBER result =