QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#540573#8932. Bingoucup-team3282#WA 256ms3744kbC++145.8kb2024-08-31 17:21:502024-08-31 17:21:50

Judging History

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

  • [2024-08-31 17:21:50]
  • 评测
  • 测评结果:WA
  • 用时:256ms
  • 内存:3744kb
  • [2024-08-31 17:21:50]
  • 提交

answer

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


struct bignum {
    int sign;
    vector<int> v;

    bignum() : sign(1) {}
    bignum(const string &s) { *this = s; }
    bignum(int v) {
        char buf[21];
        sprintf(buf, "%d", v);
        *this = buf;
    }
    void zip(int unzip) {
        if (unzip == 0) {
            for (int i = 0; i < (int)v.size(); i++)
                v[i] = get_pos(i * 4) + get_pos(i * 4 + 1) * 10 + get_pos(i * 4 + 2) * 100 + get_pos(i * 4 + 3) * 1000;
        } else
            for (int i = (v.resize(v.size() * 4), (int)v.size() - 1), a; i >= 0; i--)
                a = (i % 4 >= 2) ? v[i / 4] / 100 : v[i / 4] % 100, v[i] = (i & 1) ? a / 10 : a % 10;
        setsign(1, 1);
    }
    int get_pos(unsigned pos) const { return pos >= v.size() ? 0 : v[pos]; }
    bignum &setsign(int newsign, int rev) {
        for (int i = (int)v.size() - 1; i > 0 && v[i] == 0; i--)
            v.erase(v.begin() + i);
        sign = (v.size() == 0 || (v.size() == 1 && v[0] == 0)) ? 1 : (rev ? newsign * sign : newsign);
        return *this;
    }
    string to_str() const {
        bignum b = *this;
        string s;
        for (int i = (b.zip(1), 0); i < (int)b.v.size(); ++i)
            s += char(*(b.v.rbegin() + i) + '0');
        return (sign < 0 ? "-" : "") + (s.empty() ? string("0") : s);
    }
    bool absless(const bignum &b) const {
        if (v.size() != b.v.size()) return v.size() < b.v.size();
        for (int i = (int)v.size() - 1; i >= 0; i--)
            if (v[i] != b.v[i]) return v[i] < b.v[i];
        return false;
    }
    bignum operator-() const {
        bignum c = *this;
        c.sign = (v.size() > 1 || v[0]) ? -c.sign : 1;
        return c;
    }
    bignum &operator=(const string &s) {
        if (s[0] == '-')
            *this = s.substr(1);
        else {
            for (int i = (v.clear(), 0); i < (int)s.size(); ++i)
                v.push_back(*(s.rbegin() + i) - '0');
            zip(0);
        }
        return setsign(s[0] == '-' ? -1 : 1, sign = 1);
    }
    bool operator<(const bignum &b) const {
        return sign != b.sign ? sign < b.sign : (sign == 1 ? absless(b) : b.absless(*this));
    }
    bool operator==(const bignum &b) const { return v == b.v && sign == b.sign; }
    bignum &operator+=(const bignum &b) {
        if (sign != b.sign) return *this = (*this) - -b;
        v.resize(max(v.size(), b.v.size()) + 1);
        for (int i = 0, carry = 0; i < (int)b.v.size() || carry; i++) {
            carry += v[i] + b.get_pos(i);
            v[i] = carry % 10000, carry /= 10000;
        }
        return setsign(sign, 0);
    }
    bignum operator+(const bignum &b) const {
        bignum c = *this;
        return c += b;
    }
    void add_mul(const bignum &b, int mul) {
        v.resize(max(v.size(), b.v.size()) + 2);
        for (int i = 0, carry = 0; i < (int)b.v.size() || carry; i++) {
            carry += v[i] + b.get_pos(i) * mul;
            v[i] = carry % 10000, carry /= 10000;
        }
    }
    bignum operator-(const bignum &b) const {
        if (b.v.empty() || b.v.size() == 1 && b.v[0] == 0) return *this;
        if (sign != b.sign) return (*this) + -b;
        if (absless(b)) return -(b - *this);
        bignum c;
        for (int i = 0, borrow = 0; i < (int)v.size(); i++) {
            borrow += v[i] - b.get_pos(i);
            c.v.push_back(borrow);
            c.v.back() -= 10000 * (borrow >>= 31);
        }
        return c.setsign(sign, 0);
    }
    bignum operator*(const bignum &b) const {
        if (b < *this) return b * *this;
        bignum c, d = b;
        for (int i = 0; i < (int)v.size(); i++, d.v.insert(d.v.begin(), 0))
            c.add_mul(d, v[i]);
        return c.setsign(sign * b.sign, 0);
    }
    bignum operator/(const bignum &b) const {
        bignum c, d;
        bignum e=b;
        e.sign=1;

        d.v.resize(v.size());
        double db = 1.0 / (b.v.back() + (b.get_pos((unsigned)b.v.size() - 2) / 1e4) +
                           (b.get_pos((unsigned)b.v.size() - 3) + 1) / 1e8);
        for (int i = (int)v.size() - 1; i >= 0; i--) {
            c.v.insert(c.v.begin(), v[i]);
            int m = (int)((c.get_pos((int)e.v.size()) * 10000 + c.get_pos((int)e.v.size() - 1)) * db);
            c = c - e * m, c.setsign(c.sign, 0), d.v[i] += m;
            while (!(c < e))
                c = c - e, d.v[i] += 1;
        }
        return d.setsign(sign * b.sign, 0);
    }
    bignum operator%(const bignum &b) const { return *this - *this / b * b; }
    bool operator>(const bignum &b) const { return b < *this; }
    bool operator<=(const bignum &b) const { return !(b < *this); }
    bool operator>=(const bignum &b) const { return !(*this < b); }
    bool operator!=(const bignum &b) const { return !(*this == b); }
};

int main(){
	ios::sync_with_stdio(0);
	int T;
	cin>>T;
	while(T--){
		string tn;
		int m;
		cin>>tn>>m;
		
		bignum n(tn);
		if(n<m){
			cout<<m<<endl;
			continue;
		}
		string tm=to_string(m);
		int lm=tm.length(),ln=tn.length();
		
		bool flag=0;
		for(int i=0;i+lm-1<ln-1;i++){
			if(tn.substr(i,lm)==tm){
				flag=1;
				break;
			}
		}
		if(flag){
			bignum ans=n+1;
			cout<<ans.to_str()<<endl;
			continue;
		}
		
		bignum ans=(n+m)/m*m;
		
		string tx=tn.substr(0,ln-lm)+tm;
		bignum x(tx);
		if(tm<=tn.substr(ln-lm))
			x+=bignum("1"+string("0",lm));
		if(x<ans)
			ans=x;
//		cout<<x.to_str()<<endl;
		
		if(ln>lm&&tn[ln-lm-1]==tm[0]){
			if(tn.substr(ln-lm-1,lm)<tm){
				tx=tn.substr(0,ln-lm-1)+tm+'0';
				x=tx;
				if(x<ans)
					ans=x;
			}
			else if(tn.substr(ln-lm-1,lm)==tm&&tn[ln-1]!='9'){
				tx=tn.substr(0,ln-lm-1)+tm+char(tn[ln-1]+1);
				x=tx;
				if(x<ans)
					ans=x;
			}
		}
		cout<<ans.to_str()<<endl;
	}
	
	
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3732kb

input:

6
7 3
12 3
9 10
249 51
1369 37
2 1

output:

9
13
10
251
1370
3

result:

ok 6 lines

Test #2:

score: -100
Wrong Answer
time: 256ms
memory: 3744kb

input:

100000
3196282243 28
7614814237 33
2814581084 97
1075124401 58
7822266214 100
1767317768 31
7189709841 75
9061337538 69
6552679231 38
9946082148 18
5497675062 54
7787300351 65
4310767261 68
4811341953 100
3265496130 31
8294404054 62
2845521744 90
1114254672 26
6442013672 13
3744046866 40
3289624367 ...

output:

3196282244
7614814251
2814581097
1075124424
7822266300
1767317769
7189709850
9061337569
6552679238
9946082160
5497675063
7787300365
4310767268
4811341690
3265496131
8294404062
2845521790
1114254674
6442013673
3744046867
3289624375
6477935360
1292587551
5504674689
2898829180
7882736025
2846033387
923...

result:

wrong answer 14th lines differ - expected: '4811342000', found: '4811341690'