这阵子有点无聊,帮人做程序吧【绝对免费】
2026-02-07 16:37:22
发布于:湖南
先来个大整数数组吧!
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <cstdint>
#include <climits>
#include <cctype>
#include <cmath>
#include <utility>
#include <sstream>
#include <limits>
#define BITS_PER_CHUNK 32
#define TOTAL_CHUNKS 32
#define SIGN_BIT_INDEX 1023
#define SIGN_CHUNK (SIGN_BIT_INDEX / BITS_PER_CHUNK)
#define SIGN_BIT_OFFSET (SIGN_BIT_INDEX % BITS_PER_CHUNK)
class int_1024t {
private:
std::vector<uint32_t> chunks_;
void init_chunks() {
chunks_.resize(TOTAL_CHUNKS, 0);
}
bool get_bit(size_t bit_pos) const {
if (bit_pos >= 1024) return false;
size_t chunk_idx = bit_pos / BITS_PER_CHUNK;
size_t bit_offset = bit_pos % BITS_PER_CHUNK;
return (chunks_[chunk_idx] >> bit_offset) & 1U;
}
void set_bit(size_t bit_pos, bool value) {
if (bit_pos >= 1024) return;
size_t chunk_idx = bit_pos / BITS_PER_CHUNK;
size_t bit_offset = bit_pos % BITS_PER_CHUNK;
if (value) {
chunks_[chunk_idx] |= (1U << bit_offset);
} else {
chunks_[chunk_idx] &= ~(1U << bit_offset);
}
}
bool is_negative() const {
return get_bit(SIGN_BIT_INDEX);
}
void negate() {
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
chunks_[i] = ~chunks_[i];
}
uint64_t carry = 1;
for (size_t i = 0; i < TOTAL_CHUNKS && carry; ++i) {
carry += chunks_[i];
chunks_[i] = static_cast<uint32_t>(carry);
carry >>= BITS_PER_CHUNK;
}
}
void add_signed(const int_1024t& other) {
uint64_t carry = 0;
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
carry += chunks_[i];
carry += other.chunks_[i];
chunks_[i] = static_cast<uint32_t>(carry);
carry >>= BITS_PER_CHUNK;
}
}
void sub_unsigned(const int_1024t& other) {
uint64_t borrow = 0;
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
uint64_t a = chunks_[i];
uint64_t b = other.chunks_[i] + borrow;
borrow = (a < b) ? 1ULL : 0ULL;
if (borrow) a += (1ULL << BITS_PER_CHUNK);
chunks_[i] = static_cast<uint32_t>(a - b);
}
}
bool unsigned_greater_or_equal(const int_1024t& other) const {
for (int i = TOTAL_CHUNKS - 1; i >= 0; --i) {
if (chunks_[i] != other.chunks_[i]) {
return chunks_[i] > other.chunks_[i];
}
}
return true;
}
bool unsigned_greater(const int_1024t& other) const {
for (int i = TOTAL_CHUNKS - 1; i >= 0; --i) {
if (chunks_[i] != other.chunks_[i]) {
return chunks_[i] > other.chunks_[i];
}
}
return false;
}
bool unsigned_less(const int_1024t& other) const {
return !unsigned_greater_or_equal(other);
}
bool unsigned_less_or_equal(const int_1024t& other) const {
return !unsigned_greater(other);
}
void mul_small(uint32_t val) {
uint64_t carry = 0;
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
carry += static_cast<uint64_t>(chunks_[i]) * val;
chunks_[i] = static_cast<uint32_t>(carry);
carry >>= BITS_PER_CHUNK;
}
}
void add_small(uint32_t val) {
uint64_t carry = val;
for (size_t i = 0; i < TOTAL_CHUNKS && carry; ++i) {
carry += chunks_[i];
chunks_[i] = static_cast<uint32_t>(carry);
carry >>= BITS_PER_CHUNK;
}
}
void unsigned_div_mod(const int_1024t& divisor, int_1024t& quotient, int_1024t& remainder) const {
if (divisor.is_zero()) {
throw std::domain_error("division by zero");
}
quotient = int_1024t();
remainder = int_1024t();
for (int bit = 1023; bit >= 0; --bit) {
remainder <<= 1;
if (get_bit(bit)) {
remainder.set_bit(0, true);
}
if (remainder.unsigned_greater_or_equal(divisor)) {
remainder.sub_unsigned(divisor);
quotient.set_bit(bit, true);
}
}
}
public:
int_1024t() {
init_chunks();
}
int_1024t(int value) {
init_chunks();
set_from_signed(static_cast<long long>(value));
}
int_1024t(long value) {
init_chunks();
set_from_signed(static_cast<long long>(value));
}
int_1024t(long long value) {
init_chunks();
set_from_signed(value);
}
int_1024t(unsigned int value) {
init_chunks();
set_from_unsigned(static_cast<uint64_t>(value));
}
int_1024t(unsigned long value) {
init_chunks();
set_from_unsigned(static_cast<uint64_t>(value));
}
int_1024t(unsigned long long value) {
init_chunks();
set_from_unsigned(static_cast<uint64_t>(value));
}
int_1024t(float value) {
init_chunks();
const long long int_val = static_cast<long long>(std::trunc(value));
*this = int_1024t(int_val);
}
int_1024t(double value) {
init_chunks();
const long long int_val = static_cast<long long>(std::trunc(value));
*this = int_1024t(int_val);
}
explicit int_1024t(const std::string& dec_str) {
init_chunks();
if (dec_str.empty()) throw std::invalid_argument("empty string");
bool negative = false;
size_t start = 0;
if (dec_str[0] == '-') {
negative = true;
start = 1;
} else if (dec_str[0] == '+') {
start = 1;
}
if (start >= dec_str.size()) {
throw std::invalid_argument("no digits after sign");
}
for (size_t i = start; i < dec_str.size(); ++i) {
if (!isdigit(static_cast<unsigned char>(dec_str[i]))) {
throw std::invalid_argument("invalid character: " + std::string(1, dec_str[i]));
}
mul_small(10);
add_small(static_cast<uint32_t>(dec_str[i] - '0'));
}
if (negative && !is_zero()) negate();
}
private:
void set_from_signed(long long value) {
if (value == 0) return;
const uint64_t abs_val = static_cast<uint64_t>(llabs(value));
chunks_[0] = static_cast<uint32_t>(abs_val);
chunks_[1] = static_cast<uint32_t>(abs_val >> BITS_PER_CHUNK);
if (value < 0) negate();
}
void set_from_unsigned(uint64_t value) {
chunks_[0] = static_cast<uint32_t>(value);
chunks_[1] = static_cast<uint32_t>(value >> BITS_PER_CHUNK);
}
public:
operator int64_t() const {
if (is_negative()) {
int_1024t temp = *this;
temp.negate();
const uint64_t val = temp.chunks_[0] | (static_cast<uint64_t>(temp.chunks_[1]) << BITS_PER_CHUNK);
return -static_cast<int64_t>(val);
}
const uint64_t res = chunks_[0] | (static_cast<uint64_t>(chunks_[1]) << BITS_PER_CHUNK);
return static_cast<int64_t>(res);
}
operator std::string() const {
if (is_zero()) return "0";
int_1024t temp = *this;
std::string res;
if (is_negative()) {
temp.negate();
res += "-";
}
while (!temp.is_zero()) {
uint32_t rem;
temp = temp.div_mod(10, rem);
res += static_cast<char>('0' + rem);
}
if (res[0] == '-') {
std::reverse(res.begin() + 1, res.end());
} else {
std::reverse(res.begin(), res.end());
}
return res;
}
bool is_zero() const {
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
if (chunks_[i] != 0) return false;
}
return true;
}
int_1024t div_mod(uint32_t divisor, uint32_t& remainder) const {
if (divisor == 0) throw std::domain_error("division by zero");
int_1024t quotient;
uint64_t rem = 0;
for (int i = TOTAL_CHUNKS - 1; i >= 0; --i) {
rem = (rem << BITS_PER_CHUNK) | chunks_[i];
quotient.chunks_[i] = static_cast<uint32_t>(rem / divisor);
rem = rem % divisor;
}
remainder = static_cast<uint32_t>(rem);
return quotient;
}
int_1024t div_mod(const int_1024t& divisor, int_1024t& remainder) const {
if (divisor.is_zero()) {
throw std::domain_error("division by zero");
}
bool neg_quotient = is_negative() != divisor.is_negative();
bool neg_remainder = is_negative();
int_1024t a = *this;
int_1024t b = divisor;
if (a.is_negative()) a.negate();
if (b.is_negative()) b.negate();
int_1024t quotient;
a.unsigned_div_mod(b, quotient, remainder);
if (neg_quotient && !quotient.is_zero()) quotient.negate();
if (neg_remainder && !remainder.is_zero()) remainder.negate();
return quotient;
}
int_1024t& operator+=(const int_1024t& other) {
add_signed(other);
return *this;
}
int_1024t& operator-=(const int_1024t& other) {
int_1024t neg = other;
neg.negate();
return *this += neg;
}
int_1024t& operator*=(const int_1024t& other) {
bool res_neg = is_negative() != other.is_negative();
int_1024t a = *this;
int_1024t b = other;
if (a.is_negative()) a.negate();
if (b.is_negative()) b.negate();
int_1024t res;
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
uint64_t carry = 0;
for (size_t j = 0; j < TOTAL_CHUNKS - i; ++j) {
carry += static_cast<uint64_t>(a.chunks_[i]) * b.chunks_[j];
carry += res.chunks_[i + j];
res.chunks_[i + j] = static_cast<uint32_t>(carry);
carry >>= BITS_PER_CHUNK;
}
}
*this = res;
if (res_neg && !is_zero()) negate();
return *this;
}
int_1024t& operator/=(const int_1024t& other) {
int_1024t remainder;
*this = div_mod(other, remainder);
return *this;
}
int_1024t& operator%=(const int_1024t& other) {
int_1024t remainder;
div_mod(other, remainder);
*this = remainder;
return *this;
}
int_1024t& operator/=(uint32_t d) {
uint32_t rem;
*this = div_mod(d, rem);
return *this;
}
int_1024t& operator%=(uint32_t d) {
uint32_t rem;
div_mod(d, rem);
*this = int_1024t(rem);
return *this;
}
int_1024t& operator<<=(int shift) {
if (shift <= 0) return *this;
if (shift >= 1024) {
*this = int_1024t();
return *this;
}
int chunk_shift = shift / BITS_PER_CHUNK;
int bit_shift = shift % BITS_PER_CHUNK;
for (int i = TOTAL_CHUNKS - 1; i >= 0; --i) {
if (i >= chunk_shift) {
chunks_[i] = chunks_[i - chunk_shift];
} else {
chunks_[i] = 0;
}
}
if (bit_shift > 0) {
uint64_t carry = 0;
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
uint64_t val = static_cast<uint64_t>(chunks_[i]) << bit_shift;
val |= carry;
carry = val >> BITS_PER_CHUNK;
chunks_[i] = static_cast<uint32_t>(val);
}
}
return *this;
}
int_1024t& operator>>=(int shift) {
if (shift <= 0) return *this;
if (shift >= 1024) {
bool neg = is_negative();
*this = int_1024t();
if (neg) set_bit(SIGN_BIT_INDEX, true);
return *this;
}
int chunk_shift = shift / BITS_PER_CHUNK;
int bit_shift = shift % BITS_PER_CHUNK;
if (bit_shift > 0) {
uint32_t carry = 0;
for (int i = TOTAL_CHUNKS - 1; i >= 0; --i) {
uint64_t val = static_cast<uint64_t>(chunks_[i]) >> bit_shift;
val |= static_cast<uint64_t>(carry) << (BITS_PER_CHUNK - bit_shift);
carry = static_cast<uint32_t>(chunks_[i] << (BITS_PER_CHUNK - bit_shift));
chunks_[i] = static_cast<uint32_t>(val);
}
}
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
if (i + chunk_shift < TOTAL_CHUNKS) {
chunks_[i] = chunks_[i + chunk_shift];
} else {
chunks_[i] = is_negative() ? 0xFFFFFFFF : 0;
}
}
return *this;
}
int_1024t& operator++() {
*this += 1;
return *this;
}
int_1024t operator++(int) {
const int_1024t temp = *this;
*this += 1;
return temp;
}
int_1024t& operator--() {
*this -= 1;
return *this;
}
int_1024t operator--(int) {
const int_1024t temp = *this;
*this -= 1;
return temp;
}
int_1024t operator~() const {
int_1024t result = *this;
for (size_t i = 0; i < TOTAL_CHUNKS; ++i) {
result.chunks_[i] = ~result.chunks_[i];
}
return result;
}
bool operator==(const int_1024t& other) const {
return chunks_ == other.chunks_;
}
bool operator!=(const int_1024t& other) const {
return !(*this == other);
}
bool operator<(const int_1024t& other) const {
bool tn = is_negative(), on = other.is_negative();
if (tn != on) return tn;
return tn ? unsigned_greater_or_equal(other) : !unsigned_greater_or_equal(other);
}
bool operator>(const int_1024t& other) const {
return other < *this;
}
bool operator<=(const int_1024t& other) const {
return !(*this > other);
}
bool operator>=(const int_1024t& other) const {
return !(*this < other);
}
friend std::ostream& operator<<(std::ostream& os, const int_1024t& num);
friend std::istream& operator>>(std::istream& is, int_1024t& num);
friend int_1024t operator<<(const int_1024t& num, int shift);
friend int_1024t operator>>(const int_1024t& num, int shift);
};
int_1024t operator+(const int_1024t& a, const int_1024t& b) {
int_1024t res = a;
res += b;
return res;
}
int_1024t operator-(const int_1024t& a, const int_1024t& b) {
int_1024t res = a;
res -= b;
return res;
}
int_1024t operator*(const int_1024t& a, const int_1024t& b) {
int_1024t res = a;
res *= b;
return res;
}
int_1024t operator/(const int_1024t& a, const int_1024t& b) {
int_1024t res = a;
res /= b;
return res;
}
int_1024t operator%(const int_1024t& a, const int_1024t& b) {
int_1024t res = a;
res %= b;
return res;
}
int_1024t operator<<(const int_1024t& num, int shift) {
int_1024t res = num;
res <<= shift;
return res;
}
int_1024t operator>>(const int_1024t& num, int shift) {
int_1024t res = num;
res >>= shift;
return res;
}
#define DEFINE_OP_FOR_TYPE(T) \
int_1024t operator+(const int_1024t& a, T b) { return a + int_1024t(b); } \
int_1024t operator+(T a, const int_1024t& b) { return int_1024t(a) + b; } \
int_1024t operator-(const int_1024t& a, T b) { return a - int_1024t(b); } \
int_1024t operator-(T a, const int_1024t& b) { return int_1024t(a) - b; } \
int_1024t operator*(const int_1024t& a, T b) { return a * int_1024t(b); } \
int_1024t operator*(T a, const int_1024t& b) { return int_1024t(a) * b; } \
int_1024t operator/(const int_1024t& a, T b) { return a / int_1024t(b); } \
int_1024t operator%(const int_1024t& a, T b) { return a % int_1024t(b); }
DEFINE_OP_FOR_TYPE(int)
DEFINE_OP_FOR_TYPE(long)
DEFINE_OP_FOR_TYPE(long long)
DEFINE_OP_FOR_TYPE(unsigned int)
DEFINE_OP_FOR_TYPE(unsigned long)
DEFINE_OP_FOR_TYPE(unsigned long long)
int_1024t operator+(const int_1024t& a, double b) {
return a + int_1024t(b);
}
int_1024t operator+(double a, const int_1024t& b) {
return int_1024t(a) + b;
}
int_1024t operator-(const int_1024t& a, double b) {
return a - int_1024t(b);
}
int_1024t operator-(double a, const int_1024t& b) {
return int_1024t(a) - b;
}
int_1024t operator*(const int_1024t& a, double b) {
return a * int_1024t(b);
}
int_1024t operator*(double a, const int_1024t& b) {
return int_1024t(a) * b;
}
std::ostream& operator<<(std::ostream& os, const int_1024t& num) {
os << static_cast<std::string>(num);
return os;
}
std::istream& operator>>(std::istream& is, int_1024t& num) {
std::string input_str;
if (!(is >> input_str)) {
return is;
}
try {
num = int_1024t(input_str);
} catch (const std::invalid_argument& e) {
is.setstate(std::ios::failbit);
}
return is;
}
需要我做程序的,评论区说(名字+描述)(一次一个)明天中午抽^ v ^
绝对公正
这里空空如也





















有帮助,赞一个