UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#206802#3719. 加密通话cql10015ms1256kbC++115.3kb2024-07-25 18:15:132024-07-25 20:21:29

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;

class BigInt {
	string num;
	bool positive = 1;
public:
	// 构造函数
	BigInt() {}
	BigInt(const string& s) {
		positive = s[0] != '-';
		num = s.substr(!positive);
	}
	BigInt(const long long x) : BigInt(to_string(x)) {}
	BigInt(const string& s, bool f) : positive(f) { num = s; }
	BigInt(const BigInt& big_int) : BigInt(big_int.num) {}

	// 加、减、乘、除、取余(核心代码)

	// 加
	BigInt operator+(const BigInt& other) const {
		string b = other.num;
		if (!positive && other.positive) return other.operator-(BigInt(num));
		else if (!other.positive) return this->operator-(BigInt(other));
		string res(max(num.size(), b.size()) + 1, 0);
		for (int i = 0; i < res.size(); i++)
			res[res.size() - 1 - i] = (i < num.size() ? num[num.size() - 1 - i] : '0') + (i < b.size() ? b[b.size() - 1 - i] : '0') - '0' - '0';
		for (int i = res.size() - 1; i > 0; i--) {
			res[i - 1] += res[i] / 10;
			res[i] = res[i] % 10 + '0';
		}
		res[0] += '0';
		return BigInt(res.substr(res[0] == '0'));
	}

	// 减
	BigInt operator-(const BigInt& other) const {
		string a = num, b = other.num;
		if (!positive && other.positive) return BigInt(BigInt(a).operator+(other).num, 0);
		else if (!positive && !other.positive) return BigInt(b).operator-(BigInt(a));
		else if (positive && !other.positive) return this->operator+(BigInt(b));
		bool bl = (a.size() > b.size() || a.size() == b.size() && a >= b);
		string sign = (bl ? "" : "-");
		if (sign == "-") swap(a, b);
		string res(a.size(), 0);
		for (int i = 0; i < res.size(); i++)
			res[res.size() - 1 - i] = a[a.size() - 1 - i] - (i < b.size() ? b[b.size() - 1 - i] : '0');
		for (int i = res.size() - 1; i > 0; i--) {
			res[i - 1] -= res[i] < 0;
			res[i] = (res[i] + 10) % 10 + '0';
		}
		res[0] += '0';
		int i = 0;
		for (; res[i] == '0' && i < res.size() - 1; i++);
		return BigInt(sign + res.substr(i));
	}

	// 乘
	BigInt operator*(const BigInt& other) const {
		string b = other.num;
		if (num == "0" || b == "0") return BigInt("0");
		bool bl = positive ^ other.positive;
		string res(num.size() + b.size(), 0);
		for (int i = 0; i < num.size(); i++)
			for (int j = 0; j < b.size(); j++)
				res[i + j + 1] += (num[i] - '0') * (b[j] - '0');
		for (int i = res.size() - 1; i > 0; i--) {
			res[i - 1] += res[i] / 10;
			res[i] = res[i] % 10 + '0';
		}
		res[0] += '0';
		return (bl ? "-" : "") + res.substr(res[0] == '0');
	}

	// 单精度除
	BigInt operator/(long long a) const {
		if (a == 0) throw runtime_error("The divisor cannot be zero");
		long long b = abs(a);
		long long t = 0;
		string res = "";
		for (int i = 0; i < num.size(); i++) {
			t = t * 10 + num[i] - '0';
			res += t / b + '0';
			t %= b;
		}
		int i = 0;
		for (; i < res.size() - 1 && res[i] == '0'; i++);
		return BigInt((positive != (a < 0) ? "-" : "") + res.substr(i));
	}

	// 单精取余
	BigInt operator%(long long a) const {
		a = abs(a);
		long long r = 0;
		for (int i = 0; i < num.size(); i++)
			r = (r * 10 + num[i] - '0') % a;
		return BigInt(r * (positive ? 1 : -1));
	}

	// 大小比较
	bool operator==(const BigInt& other) const {
		return num == other.num;
	}
	bool operator<(const BigInt& other) const {
		return num.size() < other.num.size() || num.size() == other.num.size() && num < other.num;
	}
	bool operator>(const BigInt& other) const {
		return num.size() > other.num.size() || num.size() == other.num.size() && num > other.num;
	}
	bool operator<=(const BigInt& other) const {
		return num.size() < other.num.size() || num.size() == other.num.size() && num <= other.num;
	}
	bool operator>=(const BigInt& other) const {
		return num.size() > other.num.size() || num.size() == other.num.size() && num >= other.num;
	}

	// 加等于、减等于、乘等于、除等于、取余等于
	BigInt& operator+=(const BigInt& other) {
		*this = this->operator+(other);
		return *this;
	}
	BigInt& operator-=(const BigInt& other) {
		*this = this->operator-(other);
		return *this;
	}
	BigInt& operator*=(const BigInt& other) {
		*this = this->operator*(other);
		return *this;
	}
	BigInt& operator/=(long long other) {
		*this = this->operator/(other);
		return *this;
	}
	BigInt& operator%=(long long other) {
		*this = this->operator%(other);
		return *this;
	}

	// 加加、减减
	BigInt operator++(signed) {
		BigInt res = *this;
		this->operator+=(BigInt("1"));
		return res;
	}
	BigInt& operator++() {
		this->operator+=(BigInt("1"));
		return *this;
	}
	BigInt operator--(signed) {
		BigInt res = *this;
		this->operator-=(BigInt("1"));
		return res;
	}
	BigInt& operator--() {
		this->operator-=(BigInt("1"));
		return *this;
	}

	// 提供属性函数
	int size() const { return num.size(); }
	string getVal() const { return (positive ? "" : "-") + num; }

	// cin cout
	friend istream& operator>>(istream& is, BigInt& other) {
		string s; is >> s;
		other = BigInt(s);
		return is;
	}
	friend ostream& operator<<(ostream& os, const BigInt& other) {
		os << other.getVal();
		return os;
	}
};

signed main() {
	string s; cin >> s;
	BigInt res(0);
	for (int i = 0; i < s.size(); i++)
		res = res * BigInt(26) + BigInt(s[i] - 'a' + 1);
	cout << res;
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 1ms
memory: 1252kb

input:

aaa

output:

703

result:

ok single line: '703'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1252kb

input:

otto

output:

277695

result:

ok single line: '277695'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1256kb

input:

wonder

output:

280375204

result:

ok single line: '280375204'

Test #4:

score: 10
Accepted
time: 0ms
memory: 1256kb

input:

abitwzdkxul

output:

154073598812678

result:

ok single line: '154073598812678'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1256kb

input:

opgwlppmlumy

output:

57356751904151439

result:

ok single line: '57356751904151439'

Test #6:

score: 10
Accepted
time: 0ms
memory: 1252kb

input:

jwwvqldtsjskf

output:

1042077430022487696

result:

ok single line: '1042077430022487696'

Test #7:

score: 10
Accepted
time: 4ms
memory: 1256kb

input:

jfrioyvbyqsoxygmajdiqorgijlhkhbwwjawtwckokqyvxrdsqymffhfbuwajpuoqbcnddsyevhkapeusipgjvszubapviqtvmrz...

output:

4300911453790411800945391903152765942782793138336118343042260142874506488263785673172087778384488963...

result:

ok single line: '430091145379041180094539190315...9276413044766128134123598377397'

Test #8:

score: 10
Accepted
time: 4ms
memory: 1256kb

input:

udbsljdblcgcdylvoettkrrxsonxxwzpwcxtjnqbvbirbsrgliszqfqkaiwydmmxknyaryrfjogtianswvlqzsmyscvsiipecngs...

output:

1487898281503557831748070024669444336196763093279730130921507188777488934832629510294231582511917662...

result:

ok single line: '148789828150355783174807002466...3974947859267251785532872755112'

Test #9:

score: 10
Accepted
time: 2ms
memory: 1256kb

input:

gursgiqzlcfqnjjxjowqzubnydiygjxestvlehffjkybkmnrsbzzkprrnyvmppxwwyhmjmsfukcpjmmltovusjaqzfmhdsqjkcsx...

output:

6050651831854106655593124171348136351661687936535471990086285246928592125170210941171129207820422193...

result:

ok single line: '605065183185410665559312417134...0496840954331952742930298239413'

Test #10:

score: 10
Accepted
time: 4ms
memory: 1256kb

input:

vgkblomiykrhfzqnuwhrnmrjbqpflvpqlpivadvtwilhsbpttuolnfztlmcoctewhjrsjmhlgnlbginckuabmaogphxqkiweuubw...

output:

7504794918572298229797229224430944880316394524818964978603096666297543234087454896646797196502491357...

result:

ok single line: '750479491857229822979722922443...1605770094120254709299186651392'