QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#540267 | #8932. Bingo | ucup-team4702# | TL | 95ms | 150252kb | C++17 | 7.5kb | 2024-08-31 16:46:43 | 2024-08-31 16:46:43 |
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;
struct bign
{
int len, s[MAXN];
bign () //初始化
{
memset(s, 0, sizeof(s));
len = 1;
}
bign (int num) { *this = num; }
bign (const char *num) { *this = num; } //让this指针指向当前字符串
bign operator = (const int num)
{
char s[MAXN];
sprintf(s, "%d", num); //sprintf函数将整型映到字符串中
*this = s;
return *this; //再将字符串转到下面字符串转化的函数中
}
bign operator = (const char *num)
{
for(int i = 0; num[i] == '0'; num++) ; //去前导0
len = strlen(num);
for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0'; //反着存
return *this;
}
bign operator + (const bign &b) const //对应位相加,最为简单
{
bign c;
c.len = 0;
for(int i = 0, g = 0; g || i < max(len, b.len); i++)
{
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % 10; //关于加法进位
g = x / 10;
}
return c;
}
bign operator += (const bign &b) //如上文所说,此类运算符皆如此重载
{
*this = *this + b;
return *this;
}
void clean() //由于接下来的运算不能确定结果的长度,先大而估之然后再查
{
while(len > 1 && !s[len-1]) len--; //首位部分‘0’故删除该部分长度
}
bign operator * (const bign &b) //乘法重载在于列竖式,再将竖式中的数转为抽象,即可看出运算法则。
{
bign c;
c.len = len + b.len;
for(int i = 0; i < len; i++)
{
for(int j = 0; j < b.len; j++)
{
c.s[i+j] += s[i] * b.s[j];//不妨列个竖式看一看
}
}
for(int i = 0; i < c.len; i++) //关于进位,与加法意同
{
c.s[i+1] += c.s[i]/10;
c.s[i] %= 10;
}
c.clean(); //我们估的位数是a+b的长度和,但可能比它小(1*1 = 1)
return c;
}
bign operator *= (const bign &b)
{
*this = *this * b;
return *this;
}
bign operator - (const bign &b) //对应位相减,加法的进位改为借1
{ //不考虑负数
bign c;
c.len = 0;
for(int i = 0, g = 0; i < len; i++)
{
int x = s[i] - g;
if(i < b.len) x -= b.s[i]; //可能长度不等
if(x >= 0) g = 0; //是否向上移位借1
else
{
g = 1;
x += 10;
}
c.s[c.len++] = x;
}
c.clean();
return c;
}
bign operator -= (const bign &b)
{
*this = *this - b;
return *this;
}
bign operator / (const bign &b) //运用除是减的本质,不停地减,直到小于被减数
{
bign c, f = 0; //可能会在使用减法时出现高精度运算
for(int i = len-1; i >= 0; i--) //正常顺序,从最高位开始
{
f = f*10; //上面位的剩余到下一位*10
f.s[0] = s[i]; //加上当前位
while(f >= b)
{
f -= b;
c.s[i]++;
}
}
c.len = len; //估最长位
c.clean();
return c;
}
bign operator /= (const bign &b)
{
*this = *this / b;
return *this;
}
bign operator % (const bign &b) //取模就是除完剩下的
{
bign r = *this / b;
r = *this - r*b;
r.clean();
return r;
}
bign operator %= (const bign &b)
{
*this = *this % b;
return *this;
}
bool operator < (const bign &b) //字符串比较原理
{
if(len != b.len) return len < b.len;
for(int i = len-1; i != -1; i--)
{
if(s[i] != b.s[i]) return s[i] < b.s[i];
}
return false;
}
bool operator > (const bign &b) //同理
{
if(len != b.len) return len > b.len;
for(int i = len-1; i != -1; i--)
{
if(s[i] != b.s[i]) return s[i] > b.s[i];
}
return false;
}
bool operator == (const bign &b)
{
return !(*this > b) && !(*this < b);
}
bool operator != (const bign &b)
{
return !(*this == b);
}
bool operator <= (const bign &b)
{
return *this < b || *this == b;
}
bool operator >= (const bign &b)
{
return *this > b || *this == b;
}
string str() const //将结果转化为字符串(用于输出)
{
string res = "";
for(int i = 0; i < len; i++) res = char(s[i]+'0')+res;
return res;
}
};
istream& operator >> (istream &in, bign &x) //重载输入流
{
string s;
in >> s;
x = s.c_str(); //string转化为char[]
return in;
}
ostream& operator << (ostream &out, const bign &x) //重载输出流
{
out << x.str();
return out;
}
int strbuf[N];
void solve() {
bign n, m;
cin >> n >> m;
bign ans = n + m - (n % m);
/* cout << ans << endl; */
bign res1 = n + 1;
bool flg1 = 0;
for (int i = 0; i + m.len < res1.len; i++) {
bool flg = 0;
for (int j = m.len - 1; ~j; j--)
if (res1.s[i + j] != m.s[j]) { flg = 1; break; }
if (flg) continue;
flg1 = 1; break;
}
if (flg1) { return (void)(cout << res1 << endl); }
bign res2 = 0; int pos2 = -1;
for (int i = 0; i + m.len < n.len; i++) {
bool flg = 0;
for (int j = m.len - 1; ~j; j--) {
if (res1.s[i + j] == m.s[j]) continue;
if (res1.s[i + j] > m.s[j]) { flg = 1; break; }
if (res1.s[i + j] < m.s[j]) { flg = 0; break; }
}
if (flg) continue;
pos2 = i;
}
if (~pos2) {
bign tp = 0;
for (int i = pos2 + m.len; i < n.len; i++)
tp.s[tp.len++] = n.s[i];
tp = tp;
for (int i = 0; i < pos2; i++)
res2.s[res2.len++] = 0;
int pos3 = pos2 + m.len;
for (int i = pos2; i < pos3; i++)
res2.s[res2.len++] = m.s[i - pos2];
for (int i = pos3; i < pos3 + tp.len; i++)
res2.s[res2.len++] = tp.s[i - pos3];
}
bign res3 = 0;
for (int i = 0; i < m.len; i++)
res3.s[res3.len++] = m.s[i];
bign tp = 0;
for (int i = m.len; i < n.len; i++)
tp.s[tp.len++] = n.s[i];
tp = tp + 1;
for (int i = m.len; i < m.len + tp.len; i++)
res3.s[res3.len++] = tp.s[i - m.len];
if (res2.len && 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";
int T; cin >> T;
while (T--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 95ms
memory: 150252kb
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
Time Limit Exceeded
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...