QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#189178#5460. Sum of NumbersZhou_JKTL 1ms3516kbC++2310.1kb2023-09-26 23:27:532023-09-26 23:27:54

Judging History

你现在查看的是最新测评结果

  • [2023-09-26 23:27:54]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3516kb
  • [2023-09-26 23:27:53]
  • 提交

answer

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<sstream>
using namespace std;
class BigInteger
{
    public :
        //constructor
        BigInteger(int = 0);
        BigInteger(long long);
        BigInteger(const string &);
        BigInteger(const char *str)
        {
            *this = string(str);
        }

        //assignment operators
        BigInteger &operator=(int num)
        {
            return *this = BigInteger(num);
        }
        BigInteger &operator=(long long num)
        {
            return *this = BigInteger(num);
        }
        BigInteger &operator=(const string &str)
        {
            return *this = BigInteger(str);
        }
        BigInteger &operator=(const char *str)
        {
            return *this = BigInteger(str);
        }

        //relational operators
        bool operator<(const BigInteger &obj) const
        {
            return cmp(obj) < 0;
        }
        bool operator>(const BigInteger &obj) const
        {
            return cmp(obj) > 0;
        }
        bool operator<=(const BigInteger &obj) const
        {
            return cmp(obj) <= 0;
        }
        bool operator>=(const BigInteger &obj) const
        {
            return cmp(obj) >= 0;
        }
        bool operator==(const BigInteger &obj) const
        {
            return cmp(obj) == 0;
        }
        bool operator!=(const BigInteger &obj) const
        {
            return cmp(obj) != 0;
        }

        //arithmetic operators
        BigInteger operator+() const
        {
            return *this;
        }
        BigInteger operator-() const
        {
            return BigInteger(-sign_, val_);
        }
        BigInteger operator+(const BigInteger &) const;
        BigInteger operator-(const BigInteger &) const;
        BigInteger operator*(const BigInteger &) const;
        BigInteger operator/(const BigInteger &) const;
        BigInteger operator%(const BigInteger &) const;

        //compound assignment operators
        BigInteger &operator+=(const BigInteger &obj)
        {
            return *this = *this + obj;
        }
        BigInteger &operator-=(const BigInteger &obj)
        {
            return *this = *this - obj;
        }
        BigInteger &operator*=(const BigInteger &obj)
        {
            return *this = *this * obj;
        }
        BigInteger &operator/=(const BigInteger &obj)
        {
            return *this = *this / obj;
        }
        BigInteger &operator%=(const BigInteger &obj)
        {
            return *this = *this % obj;
        }

        //increment and decrement operators
        BigInteger &operator++()
        {
            return *this += 1;
        }
        BigInteger &operator--()
        {
            return *this -= 1;
        }
        BigInteger operator++(int);
        BigInteger operator--(int);

        //input and output
        friend istream &operator>>(istream &, BigInteger &);
        friend ostream &operator<<(ostream &, const BigInteger &);

    protected :
        enum div_type { division, remainder };
        enum cmp_type { with_sign, without_sign };
        static const int base_ = (int)1e8;
        static const int width_ = 8;
        BigInteger(int s, const vector<int> &v) : sign_(s), val_(v) {}
        int cmp(const BigInteger &, cmp_type = with_sign) const;
        BigInteger &delZero();
        BigInteger &add(const BigInteger &);
        BigInteger &sub(const BigInteger &);
        BigInteger &mul(const BigInteger &, const BigInteger &);
        BigInteger &div(BigInteger &, BigInteger, div_type = division);

    private :
        int sign_;
        vector<int> val_;
};

BigInteger::BigInteger(int num) : sign_(0)
{
    if (num < 0) sign_ = -1, num = -num;
    else if (num > 0) sign_ = 1;
    do
    {
        val_.push_back(num % base_);
        num /= base_;
    }
    while (num);
}

BigInteger::BigInteger(long long num) : sign_(0)
{
    if (num < 0) sign_ = -1, num = -num;
    else if (num > 0) sign_ = 1;
    do
    {
        val_.push_back(num % base_);
        num /= base_;
    }
    while (num);
}

BigInteger::BigInteger(const string &str)
{
    sign_ = str[0] == '-' ? -1 : 1;
    int be = str[0] == '-' ? 1 : 0, en = str.size();
    while ((en -= width_) >= be)
    {
        stringstream ss(str.substr(en, width_));
        int temp;
        ss >> temp;
        val_.push_back(temp);
    }
    if ((en += width_) > be)
    {
        stringstream ss(str.substr(be, en - be));
        int temp;
        ss >> temp;
        val_.push_back(temp);
    }
    delZero();
}

BigInteger BigInteger::operator+(const BigInteger &obj) const
{
    if (sign_ * obj.sign_ == 1)
    {
        BigInteger temp;
        return cmp(obj, without_sign) >= 0 ? (temp = *this).add(obj) : (temp = obj).add(*this);
    }
    else if (sign_ * obj.sign_ == -1) return *this - -obj;
    else return sign_ == 0 ? obj : *this;
}

BigInteger BigInteger::operator-(const BigInteger &obj) const
{
    if (sign_ * obj.sign_ == 1)
    {
        BigInteger temp;
        return cmp(obj, without_sign) >= 0 ? (temp = *this).sub(obj) : (temp = -obj).sub(*this);
    }
    else if (sign_ * obj.sign_ == -1) return *this + -obj;
    else return sign_ == 0 ? -obj : *this;
}

inline BigInteger BigInteger::operator*(const BigInteger &obj) const
{
    BigInteger temp;
    return (temp.sign_ = sign_ * obj.sign_) == 0 ? temp : temp.mul(*this, obj);
}

inline BigInteger BigInteger::operator/(const BigInteger &obj) const
{
    BigInteger temp, mod = *this;
    return cmp(obj, without_sign) < 0 || (temp.sign_ = sign_ * obj.sign_) == 0 ? temp : temp.div(mod, obj);
}

inline BigInteger BigInteger::operator%(const BigInteger &obj) const
{
    BigInteger temp, mod = *this;
    return cmp(obj, without_sign) < 0 || (temp.sign_ = sign_) == 0 ? mod : temp.div(mod, obj, remainder);
}

inline BigInteger BigInteger::operator++(int)
{
    BigInteger temp = *this;
    ++*this;
    return temp;
}

inline BigInteger BigInteger::operator--(int)
{
    BigInteger temp = *this;
    --*this;
    return temp;
}

inline istream &operator>>(istream &in, BigInteger &obj)
{
    string str;
    if (in >> str) obj = str;
    return in;
}

ostream &operator<<(ostream &out, const BigInteger &obj)
{
    if (obj.sign_ == -1) out << '-';
    out << obj.val_.back();
    for (int i = obj.val_.size() - 2; i >= 0; i--)
        out << setw(BigInteger::width_) << setfill('0') << obj.val_[i];
    return out;
}

int BigInteger::cmp(const BigInteger &obj, cmp_type typ) const
{
    if (typ == with_sign && sign_ != obj.sign_) return sign_ - obj.sign_;
    int sign = typ == with_sign ? sign_ : 1;
    if (val_.size() != obj.val_.size()) return sign * (val_.size() - obj.val_.size());
    for (int i = val_.size() - 1; i >= 0; i--)
        if (val_[i] != obj.val_[i]) return sign * (val_[i] - obj.val_[i]);
    return 0;
}

inline BigInteger &BigInteger::delZero()
{
    while((int)val_.size() > 1 && val_.back() == 0) val_.pop_back();
    if (val_.empty() || val_.back() == 0) sign_ = 0;
    return *this;
}

BigInteger &BigInteger::add(const BigInteger &obj)
{
    int os = obj.val_.size();
    val_.push_back(0);
    for (int i = 0; i < os; i++)
    {
        long long tmp = (long long) val_[i] + obj.val_[i];
        if(tmp >= base_) tmp -= base_, ++val_[i + 1];
        val_[i] = tmp;
    }
    return delZero();
}

BigInteger &BigInteger::sub(const BigInteger &obj)
{
    int pos = obj.val_.size();
    for (int i = 0; i < pos; i++)
    {
        long long tmp = (long long) val_[i] - obj.val_[i];
        if(tmp < 0) tmp += base_, --val_[i + 1];
        val_[i] = tmp;
    }
    while (val_[pos] < 0) val_[pos] += base_, --val_[++pos];
    return delZero();
}

BigInteger &BigInteger::mul(const BigInteger &a, const BigInteger &b)
{
    int as = a.val_.size(), bs = b.val_.size();
    val_.resize(as + bs);
    for (int i = 0; i < as; i++) for (int j = 0; j < bs; j++)
        {
            int x = i + j;
            long long tmp = val_[x] + (long long) a.val_[i] * b.val_[j];
            val_[x + 1] += tmp / base_;
            tmp %= base_;
            val_[x] = tmp;
        }
    return delZero();
}

BigInteger &BigInteger::div(BigInteger &a, BigInteger b, div_type typ)
{
    int move = a.val_.size() - b.val_.size();
    val_.resize(move + 1);
    b.val_.insert(b.val_.begin(), move, 0);
    for (int i = move; i >= 0; i--)
    {
        int left = 0, right = base_;
        while (left + 1 < right)
        {
            int mid = (left + right) >> 1;
            if (a.cmp(b * BigInteger(mid), without_sign) >= 0) left = mid;
            else right = mid;
        }
        val_[i] = left;
        a.sub(b * BigInteger(left));
        b.val_.erase(b.val_.begin());
    }
    return typ == division ? delZero() : a;
}
int n,k;
string s;
int len,ret;
BigInteger ans;
void dfs(int step,int sv,BigInteger sum)
{
    if(ret-sv<-(k-step+1)||ret-sv>(k-step+1)) return;
//    cerr<<"now"<<sv<<" "<<sum<<"\n";
    if(step==k+1)
    {
        if(ans==-1||sum<ans) ans=sum;
        return;
    }
    for(int v=-1;v<=1;v++)
    {
        if(ret-sv-v<-(k-step)||ret-sv-v>(k-step)) continue;
//        cerr<<"divide"<<step<<" "<<v<<" "<<s.substr(len*step+sv,len+v)<<"\n";
        dfs(step+1,sv+v,sum+s.substr(len*step+sv,len+v));
    }
    return;
}
void solve()
{
    cin>>n>>k;
    cin>>s;
    ans=-1;
    len=(n+k)/(k+1),ret=n-len*(k+1);
    dfs(0,0,0);
    len=n/(k+1),ret=n-len*(k+1);
    dfs(0,0,0);
//    if(ans=="6262")
//    {
//        cerr<<"data:\n";
//        cerr<<n<<" "<<k<<"\n";
//        cerr<<s<<"\n";
//        exit(1);
//        return;
//    }
    cout<<ans<<"\n";
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int T;
    cin>>T;
    while(T--)
        solve();
    return 0;
}

/*
1
16 4
7737414833519746

388+645+83+487+119+925+699
*/

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3516kb

input:

2
8 1
45455151
2 1
42

output:

9696
6

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10
1301 6
56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...

output:

2861837555106640794797067737879913860686764066159587941287350938727749577629356630565034353414526438507603808735990935008225192080065174423508575377930722196909797866802717925250679901255
1330897896655974774035586406544907434842835048336411271110427836483063457950873824562288934364096546537492367401...

result: