QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#540518 | #8932. Bingo | ucup-team4702# | WA | 339ms | 3908kb | C++17 | 8.7kb | 2024-08-31 17:13:30 | 2024-08-31 17:13:33 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <cstring>
using namespace std;
const int MAXN = 3e6 + 5, N = 3e6 + 5;
bool _stmer;
using ll=long long int;
using namespace std;
struct Bigint {
string a;
int sign;
Bigint(){}
void operator = (string b) {
a= (b[0]=='-' ? b.substr(1) : b);
reverse(a.begin(), a.end());
(*this).Remove0(b[0]=='-' ? -1 : 1);
}
Bigint(string x) {(*this)=x;}
Bigint(ll x) {(*this)=to_string(x);}
void operator = (ll x){*this=to_string(x);}
char operator[](int i){return a[i];}
int size() {return a.size();}
Bigint inverseSign() {sign*=-1; return (*this);}
Bigint Remove0(int newSign) {
sign = newSign;
for(int i=a.size()-1; i>0 && a[i]=='0'; i--) a.pop_back();
if(a.size()==1 && a[0]=='0') sign=1;
return (*this);
}
bool operator == (Bigint x) {return sign==x.sign && a==x.a;}
bool operator == (string x) {return *this==Bigint(x);}
bool operator == (ll x) {return *this==Bigint(x);}
bool operator != (Bigint x) {return !(*this==x);}
bool operator != (string x) {return !(*this==x);}
bool operator != (ll x) {return !(*this==x);}
bool operator < (Bigint b) {
if (sign!=b.sign) return sign<b.sign;
if(a.size()!=b.size()) return a.size()*sign<b.size()*sign;
for(int i=a.size()-1; i>=0; i--)
if(a[i] != b[i]) return a[i]<b[i];
return false;
}
bool operator < (string x) {return *this<Bigint(x);}
bool operator < (ll x) {return *this<Bigint(x);}
bool operator <= (Bigint b) {return *this==b || *this<b;}
bool operator <= (string b) {return *this==b || *this<b;}
bool operator <= (ll b) {return *this==b || *this<b;}
bool operator > (Bigint b) {return !(*this==b || *this<b);}
bool operator > (string x) {return !(*this==x || *this<x);}
bool operator > (ll b) {return !(*this==b || *this<b);}
bool operator >= (Bigint b) {return *this==b || *this>b;}
bool operator >= (string b) {return *this==b || *this>b;}
bool operator >= (ll b) {return *this==b || *this>b;}
Bigint operator + (Bigint b) {
if(sign != b.sign) return (*this)-b.inverseSign();
Bigint sum;
for(int i=0, carry=0; i<a.size() || i<b.size() || carry; i++){
if (i<a.size()) carry+=a[i]-'0';
if (i<b.size()) carry+=b[i]-'0';
sum.a += (carry % 10 + 48);
carry /= 10;
}
return sum.Remove0(sign);
}
Bigint operator + (string x) {return *this+Bigint(x);}
Bigint operator + (ll x) {return *this+Bigint(x);}
Bigint operator ++ (int) {*this+=1; return *this-1;}
Bigint operator ++ () {*this+=1; return *this;}
void operator += (Bigint x) {*this = *this+x;}
void operator += (string x) {*this = *this+x;}
void operator += (ll x) {*this = *this+x;}
Bigint operator - ( Bigint b ) {
if(sign != b.sign) return (*this)+b.inverseSign();
if(*this < b) return (b - *this).inverseSign();
Bigint sub;
for(int i=0,borrow=0; i<a.size(); i++) {
borrow = a[i]-borrow-(i<b.size() ? b.a[i] : '0');
sub.a += borrow>=0 ? borrow+'0' : borrow + 58;
borrow = borrow>=0 ? 0:1;
}
return sub.Remove0(sign);
}
Bigint operator - (string x) {return *this-Bigint(x);}
Bigint operator - (ll x) {return *this-Bigint(x);}
Bigint operator -- (int) {*this-=1; return *this+1;}
Bigint operator -- () {*this-=1; return *this;}
void operator -= (Bigint x) {*this = *this-x;}
void operator -= (string x) {*this = *this-x;}
void operator -= (ll x) {*this = *this-x;}
Bigint operator * (Bigint b) {
Bigint mult("0");
for(int i=0, k=a[i]; i<a.size(); i++, k=a[i]) {
while(k-- -'0') mult=mult+b;
b.a.insert(b.a.begin(),'0');
}
return mult.Remove0(sign * b.sign);
}
Bigint operator * (string x) {return *this*Bigint(x);}
Bigint operator * (ll x) {return *this*Bigint(x);}
void operator *= (Bigint x) {*this = *this*x;}
void operator *= (string x) {*this = *this*x;}
void operator *= (ll x) {*this = *this*x;}
Bigint operator / (Bigint b) {
if(b.size()==1 && b[0]=='0') b.a[0]/=(b[0]-'0');
Bigint c("0"), d;
for(int j=0; j<a.size(); j++) d.a += "0";
int dSign = sign*b.sign; b.sign=1;
for(int i=a.size()-1; i>=0; i--) {
c.a.insert(c.a.begin(),'0');
c=c+a.substr(i,1);
while(!(c<b)) c=c-b, d.a[i]++;
}
return d.Remove0(dSign);
}
Bigint operator / (string x) {return *this/Bigint(x);}
Bigint operator / (ll x) {return *this/Bigint(x);}
void operator /= (Bigint x) {*this = *this/x;}
void operator /= (string x) {*this = *this/x;}
void operator /= (ll x) {*this = *this/x;}
Bigint operator % (Bigint b) {
if( b.size()==1 && b[0]=='0') b.a[0]/=(b[0]-'0') ;
Bigint c("0");
int cSign = sign*b.sign; b.sign=1;
for( int i=a.size()-1; i>=0; i-- ) {
c.a.insert( c.a.begin(),'0');
c = c+a.substr(i,1);
while(!(c<b)) c=c-b;
}
return c.Remove0(cSign);
}
Bigint operator % (string x) {return *this%Bigint(x);}
Bigint operator % (ll x) {return *this%Bigint(x);}
void operator %= (Bigint x) {*this = *this%x;}
void operator %= (string x) {*this = *this%x;}
void operator %= (ll x) {*this = *this%x;}
void print() {
if(sign==-1) putchar('-');
for(int i=a.size()-1; i>=0; i--) putchar(a[i]);
}
friend istream& operator >>(istream &in,Bigint &x){
string s; in>>s; x=s; return in;
}
friend ostream& operator <<(ostream &out,Bigint &x){
if(x.sign==-1) putchar('-');
for(int i=x.size()-1; i>=0; i--)
putchar(x[i]);
return out;
}
friend Bigint pow(Bigint base, Bigint pw){
Bigint ans=1;
while(pw!=0){
if(pw%2 !=0) ans*=base;
base*=base, pw/=2;
}
return ans;
}
friend Bigint pow(Bigint a, Bigint b,Bigint mod) {
if (b==0) return Bigint(1);
Bigint tmp=pow(a,b/2,mod);
if ((b%2)==0) return (tmp*tmp)%mod;
else return (((tmp*tmp)%mod)*a)%mod;
}
friend Bigint sqrt(Bigint x) {
Bigint ans=x,tmp=(x+1)/2;
while (tmp<ans) ans=tmp, tmp=(tmp+x/tmp)/2;
return ans;
}
friend Bigint gcd(Bigint a,Bigint b){
return a%b==0 ? b : gcd(b, a%b);
}
friend Bigint lcm(Bigint a,Bigint b){
return a/gcd(a,b);
}
};
#define bign Bigint
void solve() {
bign n, m;
cin >> n >> m;
bign ans = n + m - (n % m);
bign res1 = n + 1;
bool flg1 = 0;
for (int i = 0; i + m.size() < res1.size(); i++) {
bool flg = 0;
for (int j = m.size() - 1; ~j; j--)
if (res1[i + j] != m[j]) { flg = 1; break; }
if (flg) continue;
flg1 = 1; break;
}
if (flg1) { return (void)(cout << res1 << endl); }
bign res2; int pos2 = -1;
for (int i = 0; i + m.size() < n.size(); i++) {
bool flg = 0;
for (int j = m.size() - 1; ~j; j--) {
if (res1[i + j] == m[j]) continue;
if (res1[i + j] > m[j]) { flg = 1; break; }
if (res1[i + j] < m[j]) { flg = 0; break; }
}
if (flg) continue;
pos2 = i;
}
if (~pos2) {
bign tp;
for (int i = pos2 + m.size(); i < n.size(); i++)
tp.a.push_back(n[i]);
tp = tp;
for (int i = 0; i < pos2; i++)
res2.a.push_back('0');
int pos3 = pos2 + m.size();
for (int i = pos2; i < pos3; i++)
res2.a.push_back(m[i - pos2]);
for (int i = pos3; i < pos3 + tp.size(); i++)
res2.a.push_back(tp[i - pos3]);
}
bign res3;
for (int i = 0; i < m.size(); i++)
res3.a.push_back(m[i]);
bign tp;
for (int i = m.size(); i < n.size(); i++)
tp.a.push_back(n[i]);
tp = tp + 1;
for (int i = m.size(); i < m.size() + tp.size(); i++)
res3.a.push_back(tp[i - m.size()]);
if (res2.size() && res2 < res3)
res3 = res2;
if (res3 < ans) cout << res3 << endl;
else cout << ans << endl;
}
bool _edmer;
int main() {
cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
/* freopen("in.txt", "r", stdin); */
int T; cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3908kb
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: 339ms
memory: 3864kb
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 2814581100 1075124424 7822266300 1767317769 7189709850 9061337577 6552679264 9946082160 5497675092 7787300365 4310767316 4811342000 3265496131 8294404082 2845521810 1114254674 6442013673 3744046867 3289624425 6477935360 1292587554 5504674742 2898829200 7882736025 2846033436 923...
result:
wrong answer 3rd lines differ - expected: '2814581097', found: '2814581100'