本代码由本人自主编写。
“更符合本题数据”指的是公开的 30~50 的测试点。具体原理就是生成不带括号的算式然后随机套括号。生成保证了不会除以 0。
数据的参数范围在题目最后一档分的范围内,长度和数字数量可自行调整。
经过测试,本代码在对拍了超过 5000 组数据依然正确,并且自身不会出现运行错误。
作用:把输入存到目录下的 pic.in,输出答案存到 pic.ans 中。
#include <bits/stdc++.h>
#define int long long
using namespace std;
// 最长算式长度,最多数字数量
const int MAX_LEN = 35, MAX_NUMBER_CNT = 15;
string standard[20][110] = {
{
"...........########...........",
".........############.........",
".......################.......",
"......##################......",
".....####################.....",
"....######################....",
"...########################...",
"...##########....##########...",
"..#########........#########..",
"..########..........########..",
".#########..........#########.",
".########............########.",
".########............########.",
".########............########.",
"########..............########",
"########..............########",
"########..............########",
"########..............########",
"########..............########",
"########..............########",
"########..............########",
"########..............########",
"########..............########",
"########..............########",
"########..............########",
".########............########.",
".########............########.",
".########............########.",
".#########..........#########.",
"..########..........########..",
"..#########........#########..",
"...##########....##########...",
"...########################...",
"....######################....",
".....####################.....",
"......##################......",
".......################.......",
".........############.........",
"...........########...........",
},
{
".............####..........",
"...........#######.........",
"........##########.........",
".....#############.........",
"..################.........",
".#################.........",
".#################.........",
".#################.........",
".#################.........",
"..######..########.........",
"..###.....########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
"..........########.........",
".##########################",
"###########################",
"###########################",
"###########################",
"###########################",
".##########################",
},
{
".........#########..........",
"......##############........",
"...###################......",
"..#####################.....",
".#######################....",
".#######################....",
".########################...",
".#########......#########...",
".#######.........#########..",
".#######..........########..",
".#######..........########..",
".#######..........########..",
".#######..........########..",
"..######..........########..",
".................#########..",
".................########...",
"................#########...",
"...............##########...",
"..............##########....",
".............###########....",
"............###########.....",
"...........###########......",
"..........###########.......",
".........###########........",
"........###########.........",
".......###########..........",
"......###########.....#####.",
".....###########.....#######",
"....###########......#######",
"...###########.......#######",
"..###########........#######",
".###########.........#######",
"############################",
"############################",
"############################",
"############################",
"############################",
".###########################",
},
{
".........##########.........",
".....################.......",
"...####################.....",
"..######################....",
"..#######################...",
"..########################..",
"..########################..",
"..########.......##########.",
"..#######.........#########.",
"..#######..........########.",
"..#######..........########.",
"...######..........########.",
"...................########.",
"...................########.",
"..................########..",
".................#########..",
"..........###############...",
".........###############....",
".........##############.....",
".........###############....",
".........################...",
"..........################..",
".................##########.",
"...................########.",
"...................#########",
"....................########",
"....................########",
"....................########",
"....................########",
"...................#########",
"..###.............##########",
".########........##########.",
".##########################.",
".#########################..",
"##########################..",
".########################...",
".######################.....",
"....#################.......",
".......###########..........",
},
{
".................#####........",
"................#######.......",
"...............########.......",
"..............#########.......",
".............##########.......",
"............###########.......",
"............###########.......",
"...........############.......",
"..........#############.......",
".........##############.......",
"........###############.......",
".......################.......",
".......########.#######.......",
"......########..#######.......",
".....########...#######.......",
"....########....#######.......",
"...#########....#######.......",
"...########.....#######.......",
"..########......#######.......",
".########.......#######.......",
"##############################",
"##############################",
"##############################",
"##############################",
"##############################",
"##############################",
"...............########.......",
"...............########.......",
"...............########.......",
"...............########.......",
"...............########.......",
"...............########.......",
".........####################.",
"........#####################.",
"........#####################.",
"........#####################.",
"........#####################.",
".........####################.",
},
{
"...######################...",
"...#######################..",
"...#######################..",
"...#######################..",
"...#######################..",
"...######################...",
"...#######..................",
"...#######..................",
"...#######..................",
"...#######..................",
"...#######..................",
"...#######..................",
"..########..########........",
"..####################......",
"..######################....",
"..#######################...",
"..########################..",
"..########################..",
"..#########################.",
"..########.......##########.",
".....##...........#########.",
"...................#########",
"....................########",
"....................########",
"....................########",
"....................########",
"....................########",
"....................########",
"...#...............#########",
"..####............#########.",
".########.......###########.",
".##########################.",
".#########################..",
"#########################...",
".#######################....",
"..#####################.....",
"....#################.......",
"........##########..........",
},
{
"....................######...",
"...............###########...",
"............###############..",
"..........#################..",
"........###################..",
".......####################..",
"......####################...",
".....################........",
"....############.............",
"...###########...............",
"...#########.................",
"..#########..................",
"..########...................",
".########....................",
".########....................",
".#######....########.........",
".#######..#############......",
"#########################....",
"##########################...",
"###########################..",
"###########################..",
"############......##########.",
"##########.........#########.",
"#########...........#########",
"########.............########",
"########.............########",
"########.............########",
".#######.............########",
".#######.............########",
".########...........#########",
".#########.........#########.",
"..##########.....###########.",
"...#########################.",
"...########################..",
"....######################...",
".....####################....",
"......##################.....",
"........##############.......",
"...........########..........",
},
{
"###########################.",
"############################",
"############################",
"############################",
"############################",
"###########################.",
"#######...........#########.",
"#######...........#########.",
"#######..........#########..",
"#######..........#########..",
"#######..........########...",
"#######.........#########...",
"#######.........########....",
"#######........#########....",
".#####.........#########....",
"...............########.....",
"..............#########.....",
"..............########......",
".............#########......",
".............########.......",
"............#########.......",
"............#########.......",
"............########........",
"...........#########........",
"...........########.........",
"..........#########.........",
"..........########..........",
"..........########..........",
".........########...........",
".........########...........",
"........#########...........",
"........########............",
"........########............",
".......########.............",
".......########.............",
".......#######..............",
".......#######..............",
".........#####..............",
},
{
"..........#########.........",
"........#############.......",
"......#################.....",
".....###################....",
"....#####################...",
"...#######################..",
"...#######################..",
"...#########.....#########..",
"..#########.......#########.",
"..########.........########.",
"..########.........########.",
"..########.........########.",
"..########.........########.",
"..########.........########.",
"...########.......########..",
"...#########.....#########..",
"....#####################...",
".....###################....",
"......#################.....",
"......#################.....",
"....#####################...",
"...#######################..",
"..#########......##########.",
".########..........########.",
".########..........#########",
"########............########",
"########............########",
"########............########",
"########............########",
"#########..........#########",
"#########..........#########",
".##########......##########.",
".##########################.",
"..########################..",
"..########################..",
"...######################...",
"....####################....",
"......################......",
".........##########.........",
},
{
".........#########..........",
".......#############........",
".....#################......",
"....###################.....",
"...#####################....",
"..#######################...",
".########################...",
".##########......#########..",
".#########........#########.",
"#########..........########.",
"########............#######.",
"########............#######.",
"########............########",
"########............########",
"########............########",
"#########..........#########",
".########.........##########",
".##########......###########",
"..##########################",
"..##########################",
"...#########################",
"....########################",
"......############..########",
"........########....#######.",
"....................#######.",
"...................########.",
"...................########.",
"..................########..",
".................#########..",
"...............##########...",
".............############...",
".........###############....",
"....###################.....",
"...###################......",
"...##################.......",
"...################.........",
"...##############...........",
"...############.............",
"....######..................",
},
{
"...............####..",
"..............######.",
"............########.",
"...........##########",
"..........###########",
".........############",
"........############.",
".......###########...",
"......###########....",
".....###########.....",
".....##########......",
"....##########.......",
"....#########........",
"...#########.........",
"...########..........",
"..#########..........",
"..########...........",
".#########...........",
".########............",
".########............",
".########............",
"#########............",
"########.............",
"########.............",
"########.............",
"########.............",
"########.............",
"########.............",
"########.............",
"########.............",
"########.............",
"########.............",
".########............",
".########............",
".########............",
".#########...........",
"..########...........",
"..#########..........",
"..#########..........",
"...#########.........",
"...##########........",
"....#########........",
".....#########.......",
".....##########......",
"......###########....",
".......###########...",
"........###########..",
".........############",
"..........###########",
"...........##########",
"............#########",
".............#######.",
"...............####..",
".................#...",
},
{
"..####...............",
".######..............",
"#########............",
"##########...........",
"###########..........",
"############.........",
".############........",
"...###########.......",
"....###########......",
".....##########......",
"......##########.....",
".......##########....",
"........#########....",
".........#########...",
"..........########...",
"..........#########..",
"...........########..",
"...........#########.",
"............########.",
"............########.",
"............########.",
"............#########",
".............########",
".............########",
".............########",
".............########",
".............########",
".............########",
".............########",
".............########",
".............########",
".............########",
"............########.",
"............########.",
"............########.",
"...........#########.",
"...........########..",
"..........#########..",
"..........#########..",
".........#########...",
"........##########...",
".......##########....",
".......#########.....",
".....###########.....",
"....###########......",
"...###########.......",
"..###########........",
"############.........",
"###########..........",
"##########...........",
"#########............",
".#######.............",
"..####...............",
"...#.................",
},
{
"...........#####...........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"###########################",
"###########################",
"###########################",
"###########################",
"###########################",
"###########################",
"###########################",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"..........#######..........",
"...........#####...........",
},
{
".##########################.",
"############################",
"############################",
"############################",
"############################",
"############################",
".###########################",
},
{
"...........####...........",
"..........######..........",
"..........#######.........",
".........########.........",
".........########.........",
"..........#######.........",
"..........######..........",
".#####....######.....####.",
".######...######...#######",
"#########..#####.#########",
"###############.##########",
"##########################",
"##########################",
".########################.",
"......##############......",
".........########.........",
"........###########.......",
".......#############......",
".....########.#######.....",
"....########..########....",
"....########..#########...",
"...########....########...",
"...########....########...",
"....#######.....#######...",
".....#####......######....",
"......###.........##......",
},
{
"......................####..",
"......................######",
".....................#######",
".....................#######",
"....................########",
"....................#######.",
"...................########.",
"...................#######..",
"..................########..",
"..................#######...",
".................########...",
".................#######....",
"................########....",
"................########....",
"................#######.....",
"...............########.....",
"...............#######......",
"..............########......",
"..............#######.......",
".............########.......",
".............#######........",
"............########........",
"............#######.........",
"...........########.........",
"...........#######..........",
"..........########..........",
"..........#######...........",
"..........#######...........",
".........########...........",
".........#######............",
"........########............",
"........#######.............",
".......########.............",
".......#######..............",
"......########..............",
"......#######...............",
".....########...............",
".....#######................",
"....########................",
"....#######.................",
"...########.................",
"...########.................",
"...#######..................",
"..########..................",
"..#######...................",
".########...................",
".#######....................",
"########....................",
"#######.....................",
".######.....................",
"..#####.....................",
}};
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randint(int l, int r)
{
return uniform_int_distribution<int>(l, r)(rng);
}
inline double randdouble(double l, double r)
{
return uniform_real_distribution<double>(l, r)(rng);
}
int sz[] = {39, 38, 38, 39, 38, 38, 39, 38, 39, 39, 54, 54, 28, 7, 26, 51};
map <char, int> mp;
string s[70];
const int H = 65;
int W = 0;
string eq;
namespace Generate
{
vector <int> pos;
inline void pb(char c)
{
eq.push_back(c);
}
inline void gen_equation()
{
eq.clear();
pos.clear();
int len = randint(1, MAX_NUMBER_CNT);
pb(randint(0, 9) + '0');
pos.emplace_back(0);
for (int i = 1; i < len; i++)
{
int op = randint(0, 3);
if (op == 0)
pb('+');
else if (op == 1)
pb('-');
else if (op == 2)
pb('*');
else
pb('/');
pb(randint(0, 9) + '0');
pos.emplace_back(eq.size() - 1);
}
len = len * 2 - 1;
int pt = randint(0, min((int)(MAX_LEN - len) >> 1, 6ll));
for (int i = 0; i < pt; i++)
{
int l = randint(0, pos.size() - 1), r = randint(0, pos.size() - 1);
if (l > r)
swap(l, r);
eq.insert(pos[l], "(");
for (int j = l; j < pos.size(); j++)
pos[j]++;
eq.insert(pos[r] + 1, ")");
for (int j = r + 1; j < pos.size(); j++)
pos[j]++;
}
}
string cur[150];
inline void gen_pic()
{
W = randint(15, 20);
for (int i = 0; i < H; i++)
{
s[i].resize(W, '.');
}
for (char c : eq)
{
double M = randdouble(0.9, 1), Mh = randdouble(0.9, 1), Mw = randdouble(0.9, 1);
int h = sz[mp[c]] * M * Mh, w = standard[mp[c]][0].size() * M * Mw;
for (int i = 0; i < h; i++)
cur[i] = standard[mp[c]][(int)(i / M / Mh)];
for (int j = 0; j < w; j++)
if ((int)(j / M / Mw) != j)
for (int i = 0; i < h; i++)
cur[i][j] = cur[i][(int)(j / M / Mw)];
for (int j = w; j < standard[mp[c]][0].size(); j++)
for (int i = 0; i < h; i++)
cur[i][j] = '.';
for (int i = 0; i < h; i++)
cur[i].resize(80, '.');
for (int i = h; i < h + 40; i++)
cur[i].clear(), cur[i].resize(80, '.');
w = 80, h += 40;
double theta_deg = randdouble(-15, 15);
double delta = 1 / tan(theta_deg * 2 * acos(-1) / 360);
double cc = delta;
int cnt = 0;
string sc = "";
if (delta >= 0)
{
for (int i = 0; i < h; i++)
{
if (i > cc)
cc += delta, sc += ".";
cur[i] = sc + cur[i];
cur[i] = cur[i].substr(0, w);
}
cc = delta;
for (int j = w - 1; j >= 0; j--)
{
if (w - 1 - j > cc)
cc += delta, cnt++;
if (cnt)
{
for (int i = h - 1; i >= 0; i--)
{
if (i - cnt < 0)
cur[i][j] = '.';
else
cur[i][j] = cur[i - cnt][j];
}
}
}
}
else
{
delta = -delta;
for (int i = h - 1; i >= 0; i--)
{
if (i > cc)
cc += delta, sc += ".";
cur[i] = sc + cur[i];
cur[i] = cur[i].substr(0, w);
}
cc = delta;
for (int j = 0; j < w; j++)
{
if (j > cc)
cc += delta, cnt++;
if (cnt)
{
for (int i = h - 1; i >= 0; i--)
{
if (i - cnt < 0)
cur[i][j] = '.';
else
cur[i][j] = cur[i - cnt][j];
}
}
}
}
cnt = 0;
for (int i = 0; i < h; i++)
{
if (cur[i].find('#') != string::npos)
break;
cnt++;
}
if (cnt)
{
h -= cnt;
for (int i = 0; i < h; i++)
cur[i] = cur[i + cnt];
}
while (cur[h - 1].find('#') == string::npos)
h--;
int nl = 0x3f3f3f3f3f3f3f3f, ns = 0;
for (int i = 0; i < h; i++)
{
nl = min(nl, (int)cur[i].find('#'));
}
for (int i = 0; i < h; i++)
{
for (int j = w - 1; j >= 0; j--)
if (cur[i][j] == '#')
{
ns = max(ns, j - nl + 1);
break;
}
}
for (int i = 0; i < h; i++)
cur[i] = cur[i].substr(nl, ns);
// for (int i = 0; i < h; i++)
// cout << cur[i] << endl;
// cout << endl;
W += cur[0].size() + randint(15, 20);
int ps = H / 2 - h / 2 + randint(-7, 7);
for (int i = 0; i < h; i++)
if (i + ps >= 0 && i + ps < H)
s[i + ps] += cur[i];
for (int i = 0; i < H; i++)
s[i].resize(W, '.');
}
}
}
namespace Calculate
{
string bk;
inline void build()
{
bk.clear();
stack<char> st;
for (char c : eq)
{
if (c >= '0' && c <= '9')
bk.push_back(c);
else if (c == '(')
st.push('(');
else if (c == ')')
{
while (!st.empty() && st.top() != '(')
bk.push_back(st.top()), st.pop();
st.pop();
}
else if (c == '*' || c == '/')
{
while (!st.empty() && (st.top() == '*' || st.top() == '/'))
bk.push_back(st.top()), st.pop();
st.push(c);
}
else if (c == '+' || c == '-')
{
while (!st.empty() && st.top() != '(')
bk.push_back(st.top()), st.pop();
st.push(c);
}
}
while (!st.empty())
bk.push_back(st.top()), st.pop();
}
stack<int> st;
inline pair<int, int> gettop()
{
int a = st.top(), b;
st.pop();
b = st.top();
st.pop();
return make_pair(b, a);
}
inline int calc()
{
for (char c : bk)
{
if (c >= '0' && c <= '9')
st.push(c - '0');
else
{
pair<int, int> p = gettop();
if (c == '+')
st.push(p.first + p.second);
if (c == '-')
st.push(p.first - p.second);
if (c == '*')
st.push(p.first * p.second);
if (c == '/')
{
if (p.second == 0)
return 0x3f3f3f3f3f3f3f3f;
st.push(p.first / p.second);
}
}
}
return st.top();
}
}
signed main()
{
do
Generate::gen_equation();
while (Calculate::build(), Calculate::calc() == 0x3f3f3f3f3f3f3f3f);
cout << eq << endl;
freopen("pic.ans", "w", stdout);
printf("%lld\n", Calculate::calc());
fclose(stdout);
freopen("pic.in", "w", stdout);
mp['0'] = 0, mp['1'] = 1, mp['2'] = 2, mp['3'] = 3, mp['4'] = 4, mp['5'] = 5, mp['6'] = 6, mp['7'] = 7, mp['8'] = 8, mp['9'] = 9;
mp['('] = 10, mp[')'] = 11, mp['+'] = 12, mp['-'] = 13, mp['*'] = 14, mp['/'] = 15;
Generate::gen_pic();
cout << 999 << endl << W << " " << H << endl;
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (randint(1, 20) == 1)
s[i][j] = (s[i][j] == '.' ? '#' : '.');
}
cout << s[i] << endl;
}
return 0;
}