这题不开long long能过?
查看原帖
这题不开long long能过?
118239
Chinese_zjc_楼主2021/1/21 20:56

本人没开long long,十分轻易地通过了此题...

//This Code was made by Chinese_zjc_.
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>

class bigint
{
private:
    std::vector<int> num;

public:
    friend bigint operator+(const bigint &A, const bigint &B);
    friend bigint operator-(const bigint &A, const bigint &B);
    friend bigint operator*(const bigint &A, const int &B);
    friend bigint operator*(const bigint &A, const bigint &B);
    friend bigint operator/(const bigint &A, const int &B);
    friend int operator%(const bigint &A, const int &B);
    template <class T>
    void read(T val);
    void read(char *const &str);
    void read(const char *const &str);
    void read(const std::string &str);
    void write();
};

bigint operator+(const bigint &A, const bigint &B)
{
    bigint res;
    res.num.resize(std::max(A.num.size(), B.num.size()) + 1);
    for (unsigned i = 0; i != A.num.size(); ++i)
        res.num[i] += A.num[i];
    for (unsigned i = 0; i != B.num.size(); ++i)
        res.num[i] += B.num[i];
    for (unsigned i = 0; i != res.num.size(); ++i)
        if (res.num[i] >= 10)
        {
            res.num[i] -= 10;
            ++res.num[i + 1];
        }
    while (!(res.num.empty() || res.num.back()))
        res.num.pop_back();
    return res;
}

bigint operator-(const bigint &A, const bigint &B)
// A should greater than B.
{
    bigint res;
    res.num.resize(A.num.size());
    for (unsigned i = 0; i != A.num.size(); ++i)
        res.num[i] += A.num[i];
    for (unsigned i = 0; i != B.num.size(); ++i)
        res.num[i] -= B.num[i];
    for (unsigned i = 0; i != res.num.size(); ++i)
        if (res.num[i] < 0)
        {
            res.num[i] += 10;
            --res.num[i + 1];
        }
    while (!(res.num.empty() || res.num.back()))
        res.num.pop_back();
    return res;
}

bigint operator*(const bigint &A, const int &B)
// B should not greater than 214748364.
{
    bigint res;
    res.num = A.num;
    res.num.resize(A.num.size() + 10);
    for (unsigned i = 0; i != A.num.size(); ++i)
        res.num[i] *= B;
    for (unsigned i = 0; i != A.num.size(); ++i)
        if (res.num[i] >= 10)
        {
            res.num[i + 1] += res.num[i] / 10;
            res.num[i] %= 10;
        }
    while (!(res.num.empty() || res.num.back()))
        res.num.pop_back();
    return res;
}

bigint operator*(const bigint &A, const bigint &B)
{
    bigint res;
    res.num.resize(A.num.size() + B.num.size() + 2);
    for (unsigned i = 0; i != A.num.size(); ++i)
        for (unsigned j = 0; j != B.num.size(); ++j)
            res.num[i + j] += A.num[i] * B.num[j];
    for (unsigned i = 0; i != res.num.size(); ++i)
        if (res.num[i] >= 10)
        {
            res.num[i + 1] += res.num[i] / 10;
            res.num[i] %= 10;
        }
    while (!(res.num.empty() || res.num.back()))
        res.num.pop_back();
    return res;
}

bigint operator/(const bigint &A, const int &B)
{
    bigint res;
    int left = 0;
    res.num.resize(A.num.size());
    for (unsigned i = A.num.size(); i--; left *= 10)
    {
        left += A.num[i];
        res.num[i] = left / B;
        left %= B;
    }
    while (!(res.num.empty() || res.num.back()))
        res.num.pop_back();
    return res;
}

int operator%(const bigint &A, const int &B)
{
    bigint res;
    int left = 0;
    res.num.resize(A.num.size());
    for (unsigned i = A.num.size(); i--; left *= 10)
    {
        left += A.num[i];
        res.num[i] = left / B;
        left %= B;
    }
    while (!(res.num.empty() || res.num.back()))
        res.num.pop_back();
    return left;
}

template <class T>
void bigint::read(T val)
{
    num.clear();
    if (val)
        do
            num.push_back(val % 10);
        while (val /= 10);
}
void bigint::read(char *const &str)
{
    std::string s = str;
    read(s);
}
void bigint::read(const char *const &str)
{
    std::string s = str;
    read(s);
}
void bigint::read(const std::string &str)
{
    std::string s = str;
    std::reverse(s.begin(), s.end());
    num.resize(s.size());
    for (unsigned i = 0; i != s.size(); ++i)
        num[i] = s[i] & 15;
}

void bigint::write()
{
    if (num.empty())
        putchar('0');
    for (unsigned i = num.size(); i--;)
        putchar(num[i] | '0');
}

int main()
{
    bigint a;
    int b;
    char s[5000];
    scanf("%s%d", s, &b);
    a.read(s);
    a = a / b;
    a.write();
    return 0;
}

求加强数据或解释原因

2021/1/21 20:56
加载中...