我们直接进入正题。(怎么可能)
这是一道价值一千多行代码的题目!(手动滑稽)
首先,愚蠢的我竟然想一行一行改成C++代码 (暴力解决一切) ,确实有点抽象,那我们看看该如何模拟。
这么多行的循环嵌套,看上去好像没什么思路,一层套一层,但是我们想想一个数据结构 栈 ,每个嵌套子循环就相当于入栈,如果从子循环回到了父循环,那么我们就出栈。
具体题解都在代码注释中。
理论可行,闷头就冲!(唔~)
#include<bits/stdc++.h>
#define pi acos(-1)
#define fi first
#define se second
#define ls node<<1
#define rs node<<1|1
#define mp(a, b) make_pair(a, b)
#define uf(a, b, i) for (register int i = (a); i <= (b); ++i)
#define df(a, b, i) for (register int i = (a); i >= (b); --i)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline int read() {
int x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9') {
if(ch == '-') f = -1;ch = getchar();}while(ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';ch = getchar();}return x * f;
}
template<class T>
inline void print(T x) {
if(x > 9) print(x/10);putchar(x%10 + '0');
}
template<class T>
T Max(T a, T b) {
return a > b ? a : b;
}
template<class T>
T Min(T a, T b) {
return a < b ? a : b;
}
template<class T>
void Swap(T &a, T &b) {
T temp;temp = a;a = b;b = temp;
}
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int mul, ans, line;//mul计算每层的累乘数, ans记录答案, line记录第几行
string s;//读入每行的字符串
stack<int> st;//记录每次循环要乘的数 void scan() {
//一个像我一样好吃懒做的函数 }
void work() {
mul = 1;while (getline(cin, s), s.size()) {
++line;if (line == 1) continue;//因为文件中第一行是初始化,所以要跳过 int pos = 0, len = s.size();while (pos < len && s[pos] == ' ') pos++;//计算隶属于第几级循环 while (st.size() > pos / 4) {
//如果回到父亲级循环,那就出栈 mul /= st.top();st.pop();}if (s[pos] == 'R') {
//如果是R那么表示这一行是循环命令行 pos += 7;//跳到数字字符 mul *= (int)(s[pos] - '0'); //更新循环次数 st.push((int)(s[pos]-'0'));//把循环次数压入栈中 } else {
//不是R那么说明这行是 操作赋值 pos += 8;//跳到数字字符 ans += mul * (int)(s[pos]-'0');//计算这个数字产生的贡献 }}print(ans);putchar('\n');
}
int main() {
scan();work();return 0;
}
下面是题目文件中的代码,有点小长 =…=
A = 0
REPEAT 2:A = A + 4REPEAT 5:REPEAT 6:A = A + 5A = A + 7REPEAT 6:A = A + 7REPEAT 4:A = A + 2A = A + 7A = A + 2REPEAT 7:REPEAT 4:A = A + 8A = A + 7A = A + 4A = A + 5A = A + 8REPEAT 8:A = A + 5REPEAT 1:A = A + 2REPEAT 7:A = A + 5A = A + 5REPEAT 2:REPEAT 3:A = A + 1A = A + 1REPEAT 5:A = A + 1REPEAT 9:REPEAT 6:A = A + 5A = A + 1REPEAT 6:A = A + 2A = A + 8A = A + 3REPEAT 2:A = A + 5REPEAT 3:A = A + 9REPEAT 1:A = A + 4REPEAT 2:A = A + 9REPEAT 1:A = A + 6A = A + 6A = A + 4REPEAT 3:A = A + 7A = A + 1REPEAT 2:A = A + 3REPEAT 5:A = A + 2A = A + 5A = A + 2A = A + 4A = A + 3
REPEAT 4:A = A + 4A = A + 3A = A + 7REPEAT 5:REPEAT 4:A = A + 5A = A + 7REPEAT 5:A = A + 3REPEAT 3:A = A + 3A = A + 1A = A + 8A = A + 2REPEAT 9:A = A + 5REPEAT 1:A = A + 5A = A + 2A = A + 8
A = A + 6
REPEAT 3:REPEAT 4:A = A + 9REPEAT 5:A = A + 2A = A + 1REPEAT 9:A = A + 9A = A + 2REPEAT 1:A = A + 6A = A + 8REPEAT 2:A = A + 9A = A + 4A = A + 7REPEAT 2:REPEAT 7:A = A + 3A = A + 5REPEAT 3:A = A + 5A = A + 3A = A + 6A = A + 4REPEAT 9:A = A + 2A = A + 8A = A + 2A = A + 3REPEAT 2:REPEAT 8:A = A + 5A = A + 1A = A + 6A = A + 1A = A + 2REPEAT 6:REPEAT 1:A = A + 3REPEAT 1:A = A + 2REPEAT 4:A = A + 7A = A + 1A = A + 8REPEAT 6:A = A + 5REPEAT 6:A = A + 3REPEAT 2:A = A + 2A = A + 9A = A + 7REPEAT 9:A = A + 8REPEAT 9:A = A + 8A = A + 9A = A + 3A = A + 2REPEAT 6:A = A + 3REPEAT 9:A = A + 1A = A + 9A = A + 5REPEAT 2:A = A + 4A = A + 9A = A + 8REPEAT 5:A = A + 6A = A + 9A = A + 1REPEAT 1:A = A + 4A = A + 2REPEAT 9:REPEAT 3:A = A + 4REPEAT 7:A = A + 8A = A + 3REPEAT 5:A = A + 9REPEAT 8:A = A + 9A = A + 8REPEAT 4:A = A + 7A = A + 7A = A + 3
A = A + 5
REPEAT 6:A = A + 7
REPEAT 7:A = A + 2A = A + 2
A = A + 1
REPEAT 8:REPEAT 1:REPEAT 4:A = A + 6A = A + 6A = A + 2REPEAT 5:A = A + 4A = A + 8A = A + 4REPEAT 1:A = A + 5REPEAT 7:A = A + 8REPEAT 6:A = A + 4A = A + 4A = A + 8REPEAT 4:A = A + 2REPEAT 2:A = A + 4REPEAT 2:A = A + 3REPEAT 1:A = A + 2A = A + 8REPEAT 2:A = A + 7REPEAT 8:A = A + 6A = A + 1A = A + 7REPEAT 8:A = A + 2REPEAT 8:REPEAT 6:A = A + 1A = A + 6REPEAT 2:A = A + 4A = A + 1A = A + 7A = A + 4
REPEAT 4:REPEAT 9:A = A + 2REPEAT 1:A = A + 2A = A + 5
REPEAT 8:REPEAT 6:A = A + 3REPEAT 4:A = A + 1A = A + 6A = A + 1REPEAT 7:A = A + 7REPEAT 7:A = A + 3A = A + 9A = A + 1A = A + 9REPEAT 3:A = A + 5A = A + 5A = A + 6A = A + 2REPEAT 1:A = A + 4REPEAT 2:A = A + 7REPEAT 1:A = A + 7REPEAT 4:A = A + 7A = A + 2REPEAT 5:A = A + 9A = A + 1A = A + 9A = A + 5A = A + 9REPEAT 5:A = A + 5REPEAT 1:A = A + 6REPEAT 2:A = A + 3A = A + 2A = A + 6A = A + 8A = A + 8A = A + 7A = A + 5A = A + 5
REPEAT 2:A = A + 1A = A + 7
A = A + 3
REPEAT 2:A = A + 7
A = A + 1
A = A + 4
REPEAT 1:REPEAT 7:REPEAT 2:A = A + 3A = A + 5A = A + 2A = A + 6A = A + 1
A = A + 2
A = A + 4
A = A + 9
REPEAT 1:A = A + 8
REPEAT 8:REPEAT 4:REPEAT 8:A = A + 4REPEAT 3:A = A + 1A = A + 8REPEAT 7:A = A + 8REPEAT 7:A = A + 7A = A + 7REPEAT 7:A = A + 6REPEAT 5:A = A + 9A = A + 3REPEAT 4:A = A + 5A = A + 5A = A + 4REPEAT 9:REPEAT 3:A = A + 4A = A + 3A = A + 6REPEAT 1:A = A + 3A = A + 3A = A + 6REPEAT 6:A = A + 7A = A + 7A = A + 5A = A + 5A = A + 1A = A + 2A = A + 6A = A + 6REPEAT 9:A = A + 6REPEAT 1:REPEAT 2:A = A + 4A = A + 7REPEAT 3:A = A + 6REPEAT 5:A = A + 3A = A + 6REPEAT 9:A = A + 3A = A + 6REPEAT 5:A = A + 8A = A + 8REPEAT 3:A = A + 7A = A + 9A = A + 8A = A + 3A = A + 3A = A + 9
REPEAT 6:A = A + 9
A = A + 1
REPEAT 4:REPEAT 1:A = A + 7REPEAT 9:A = A + 2A = A + 9A = A + 1
A = A + 2
A = A + 8
A = A + 7
A = A + 9
A = A + 6
REPEAT 4:REPEAT 2:A = A + 3REPEAT 3:A = A + 4A = A + 4
REPEAT 6:A = A + 6
A = A + 1
A = A + 5
A = A + 8
REPEAT 2:A = A + 6REPEAT 1:REPEAT 2:A = A + 2REPEAT 3:A = A + 1REPEAT 1:A = A + 8A = A + 7A = A + 4A = A + 2A = A + 8A = A + 4REPEAT 5:REPEAT 6:A = A + 8REPEAT 9:A = A + 5A = A + 5REPEAT 5:A = A + 5REPEAT 3:REPEAT 5:A = A + 4REPEAT 4:A = A + 6A = A + 3REPEAT 7:A = A + 3A = A + 3A = A + 1A = A + 7A = A + 7A = A + 6A = A + 5A = A + 5A = A + 6REPEAT 1:A = A + 9A = A + 3REPEAT 1:REPEAT 1:A = A + 1REPEAT 8:A = A + 5REPEAT 8:A = A + 6REPEAT 4:A = A + 9A = A + 4REPEAT 2:A = A + 3A = A + 7REPEAT 5:A = A + 7A = A + 5A = A + 8A = A + 7A = A + 8A = A + 5REPEAT 2:A = A + 5A = A + 7A = A + 8
A = A + 5
A = A + 9
REPEAT 2:REPEAT 6:A = A + 9A = A + 1A = A + 8A = A + 7A = A + 1A = A + 5REPEAT 3:A = A + 3A = A + 9A = A + 7REPEAT 3:A = A + 9A = A + 1REPEAT 6:A = A + 1REPEAT 9:REPEAT 7:A = A + 3REPEAT 5:A = A + 5A = A + 8A = A + 8A = A + 1A = A + 2REPEAT 4:A = A + 6REPEAT 3:A = A + 3A = A + 7REPEAT 8:REPEAT 1:A = A + 7A = A + 8A = A + 3A = A + 1
A = A + 2
A = A + 4
A = A + 7
REPEAT 1:REPEAT 1:REPEAT 1:A = A + 4A = A + 6REPEAT 1:A = A + 3A = A + 9A = A + 6REPEAT 9:A = A + 1A = A + 6REPEAT 5:A = A + 3A = A + 9A = A + 5A = A + 5A = A + 7A = A + 2REPEAT 2:A = A + 7A = A + 7REPEAT 7:REPEAT 4:A = A + 6A = A + 8REPEAT 6:A = A + 6REPEAT 2:A = A + 1A = A + 7A = A + 6A = A + 7REPEAT 4:REPEAT 7:A = A + 1REPEAT 2:A = A + 2A = A + 5A = A + 8A = A + 2
A = A + 1
A = A + 4
REPEAT 8:A = A + 5
A = A + 6
REPEAT 7:REPEAT 6:REPEAT 9:A = A + 7A = A + 8REPEAT 4:A = A + 6A = A + 4A = A + 3A = A + 6REPEAT 9:A = A + 3REPEAT 9:A = A + 2A = A + 7A = A + 5A = A + 2
REPEAT 7:REPEAT 8:REPEAT 6:A = A + 4A = A + 9A = A + 5A = A + 3A = A + 9REPEAT 4:REPEAT 1:A = A + 6A = A + 8REPEAT 1:A = A + 6A = A + 4A = A + 6REPEAT 3:A = A + 7REPEAT 3:A = A + 4A = A + 4A = A + 2A = A + 3A = A + 7REPEAT 5:A = A + 6A = A + 5REPEAT 1:REPEAT 8:A = A + 5REPEAT 3:A = A + 6REPEAT 9:A = A + 4A = A + 3REPEAT 6:REPEAT 2:A = A + 1A = A + 5A = A + 2
A = A + 2
A = A + 7
REPEAT 4:A = A + 7
A = A + 9
A = A + 2
REPEAT 8:A = A + 9REPEAT 9:REPEAT 2:A = A + 3A = A + 2A = A + 1A = A + 5REPEAT 9:A = A + 1A = A + 3A = A + 9REPEAT 7:A = A + 2REPEAT 5:A = A + 9A = A + 3REPEAT 2:A = A + 4REPEAT 8:A = A + 9REPEAT 5:A = A + 5A = A + 4A = A + 2A = A + 4REPEAT 6:A = A + 2REPEAT 5:A = A + 7A = A + 7A = A + 8A = A + 3REPEAT 8:A = A + 2A = A + 5REPEAT 1:A = A + 8A = A + 5A = A + 1A = A + 1A = A + 5REPEAT 2:A = A + 6REPEAT 6:A = A + 9A = A + 2A = A + 5REPEAT 4:A = A + 7A = A + 1REPEAT 6:A = A + 8A = A + 4REPEAT 3:REPEAT 2:A = A + 1A = A + 5REPEAT 2:A = A + 7REPEAT 9:A = A + 6A = A + 8A = A + 9A = A + 5REPEAT 9:REPEAT 3:A = A + 7A = A + 7A = A + 9A = A + 7REPEAT 5:A = A + 7A = A + 2A = A + 1A = A + 8A = A + 3A = A + 5
A = A + 1
REPEAT 8:A = A + 4
A = A + 2
A = A + 2
A = A + 8
REPEAT 4:REPEAT 4:A = A + 8REPEAT 7:A = A + 5A = A + 2REPEAT 2:A = A + 6REPEAT 4:A = A + 8A = A + 6A = A + 1A = A + 3
A = A + 2
A = A + 7
A = A + 4
REPEAT 8:A = A + 2A = A + 4
REPEAT 5:REPEAT 3:REPEAT 6:A = A + 8A = A + 1A = A + 6A = A + 5A = A + 9
REPEAT 8:A = A + 7
REPEAT 6:A = A + 4
A = A + 5
REPEAT 3:A = A + 1REPEAT 1:REPEAT 5:A = A + 6A = A + 2REPEAT 9:REPEAT 5:A = A + 9A = A + 3REPEAT 9:A = A + 9A = A + 8REPEAT 8:REPEAT 5:A = A + 9A = A + 4REPEAT 9:A = A + 3A = A + 4A = A + 5
REPEAT 9:REPEAT 7:A = A + 5REPEAT 3:A = A + 7REPEAT 9:REPEAT 6:A = A + 4A = A + 6REPEAT 5:REPEAT 6:A = A + 5A = A + 3A = A + 3A = A + 3A = A + 5REPEAT 7:A = A + 5REPEAT 2:A = A + 5A = A + 6REPEAT 2:A = A + 2A = A + 5A = A + 3
A = A + 5
A = A + 5
REPEAT 4:A = A + 2A = A + 1REPEAT 9:A = A + 9A = A + 5A = A + 6A = A + 2A = A + 2A = A + 5REPEAT 9:A = A + 5A = A + 4REPEAT 4:REPEAT 4:A = A + 1A = A + 2REPEAT 6:A = A + 9A = A + 3REPEAT 2:A = A + 5A = A + 1A = A + 1A = A + 3A = A + 8REPEAT 7:A = A + 4REPEAT 6:A = A + 9REPEAT 5:A = A + 9A = A + 8A = A + 3A = A + 9A = A + 4A = A + 6
REPEAT 7:A = A + 9
REPEAT 9:A = A + 4A = A + 9A = A + 1A = A + 3REPEAT 5:REPEAT 1:A = A + 4A = A + 4REPEAT 8:A = A + 9A = A + 6A = A + 2REPEAT 3:A = A + 4A = A + 4REPEAT 3:A = A + 5A = A + 2A = A + 8A = A + 3A = A + 6A = A + 4A = A + 9A = A + 1A = A + 9A = A + 5A = A + 3REPEAT 3:A = A + 2A = A + 5A = A + 8A = A + 2A = A + 5REPEAT 8:REPEAT 2:A = A + 6A = A + 7A = A + 6A = A + 9A = A + 2
REPEAT 2:A = A + 3REPEAT 8:A = A + 7A = A + 2A = A + 1A = A + 4A = A + 1A = A + 5A = A + 2A = A + 1REPEAT 1:A = A + 1REPEAT 6:A = A + 4A = A + 3A = A + 3REPEAT 5:A = A + 3REPEAT 6:REPEAT 1:A = A + 5A = A + 7A = A + 7A = A + 7REPEAT 5:A = A + 9A = A + 7REPEAT 5:A = A + 9A = A + 1A = A + 9A = A + 8REPEAT 1:A = A + 2REPEAT 5:A = A + 8REPEAT 3:A = A + 2A = A + 9A = A + 6A = A + 3REPEAT 5:REPEAT 6:A = A + 5A = A + 5REPEAT 4:A = A + 5A = A + 4REPEAT 8:A = A + 9A = A + 1REPEAT 8:A = A + 8A = A + 1A = A + 4REPEAT 6:A = A + 6REPEAT 2:A = A + 3A = A + 9A = A + 6A = A + 9REPEAT 1:A = A + 4REPEAT 3:A = A + 3A = A + 4A = A + 2A = A + 8REPEAT 2:A = A + 4A = A + 1REPEAT 9:A = A + 2A = A + 9A = A + 7
REPEAT 7:REPEAT 7:REPEAT 5:A = A + 7REPEAT 5:A = A + 1A = A + 1REPEAT 5:A = A + 6REPEAT 1:A = A + 4REPEAT 9:A = A + 4A = A + 1REPEAT 6:A = A + 8A = A + 5REPEAT 1:A = A + 4REPEAT 5:A = A + 8A = A + 7A = A + 2REPEAT 3:A = A + 3REPEAT 8:REPEAT 8:A = A + 4A = A + 7REPEAT 5:A = A + 1REPEAT 8:A = A + 7A = A + 8A = A + 4A = A + 7A = A + 6A = A + 9A = A + 5
REPEAT 3:A = A + 5REPEAT 9:A = A + 1A = A + 7
REPEAT 1:A = A + 8
A = A + 4
REPEAT 8:REPEAT 7:A = A + 2REPEAT 4:A = A + 6A = A + 6REPEAT 1:A = A + 7A = A + 1
REPEAT 9:REPEAT 5:A = A + 6A = A + 5REPEAT 7:A = A + 3A = A + 6A = A + 8REPEAT 2:A = A + 7A = A + 1A = A + 9REPEAT 3:REPEAT 3:A = A + 5