QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#611622 | #9255. Python Program | duckindog# | AC ✓ | 3ms | 3604kb | C++23 | 3.0kb | 2024-10-04 21:47:24 | 2024-10-04 21:47:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
string s;
void trim()
{
while (s[0] == ' ')
s.erase(s.begin());
}
string parse(string token)
{
if (!s.starts_with(token))
assert(false);
s.erase(s.begin(), s.begin() + token.size());
return token;
};
string parseVar()
{
string token = string(s.begin(), s.begin() + 1);
s.erase(s.begin());
return token;
};
int parseNum()
{
if (!isdigit(s[0]) && s[0] != '-')
{
s.erase(s.begin());
return INT_MAX;
}
bool neg = s[0] == '-';
if (neg)
s.erase(s.begin());
auto token = 0;
while (isdigit(s[0]))
{
token = token * 10 + (s[0] - '0');
s.erase(s.begin());
}
if (neg)
token = -token;
return token;
};
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
string i, j;
int a, b, c, d, e, f;
int answer = 0;
for (int iteration = 0; iteration < 5; ++iteration)
{
getline(cin, s);
if (iteration == 1)
{
trim();
parse("for ");
i = parseVar();
parse(" in range(");
a = parseNum();
parseVar();
b = parseNum();
auto foo = parseVar();
c = foo == "," ? parseNum() : 1;
// cout << i << " " << a << " " << b << " " << c << '\n';
}
if (iteration == 2)
{
trim();
parse("for ");
j = parseVar();
parse(" in range(");
d = parseNum();
parseVar();
e = parseNum();
auto foo = parseVar();
f = foo == "," ? parseNum() : 1;
// cout << j << " " << d << " " << e << " " << f << '\n';
if (c > 0)
{
for (int ii = a; ii < b; ii += c)
{
int dd = d == INT_MAX ? ii : d;
int ee = e == INT_MAX ? ii : e;
int ff = f == INT_MAX ? ii : f;
if (ff > 0 && dd < ee)
{
ee -= 1;
int num = (ee - dd) / ff + 1;
int lanum = dd + (num - 1) * ff;
answer += (dd + lanum) * num / 2;
}
if (ff < 0 && dd > ee)
{
ee += 1;
int num = (ee - dd) / ff + 1;
int finum = dd + (num - 1) * ff;
// cerr << finum << " " << num << "\n";
answer += (finum + dd) * num / 2;
}
}
}
else
{
for (int ii = a; ii > b; ii += c)
{
int dd = d == INT_MAX ? ii : d;
int ee = e == INT_MAX ? ii : e;
int ff = f == INT_MAX ? ii : f;
if (ff > 0 && dd < ee)
{
ee -= 1;
int num = (ee - dd) / ff + 1;
int lanum = dd + (num - 1) * ff;
answer += (dd + lanum) * num / 2;
}
if (ff < 0 && dd > ee)
{
ee += 1;
int num = (ee - dd) / ff + 1;
int finum = dd + (num - 1) * ff;
// cerr << finum << " " << num << "\n";
answer += (finum + dd) * num / 2;
}
}
}
}
}
cout << answer << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3536kb
input:
ans=0 for a in range(1,3): for b in range(5,1,-2): ans+=b print(ans)
output:
16
result:
ok single line: '16'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
ans=0 for q in range(100,50,-1): for i in range(q,77,20): ans+=i print(ans)
output:
2092
result:
ok single line: '2092'
Test #3:
score: 0
Accepted
time: 3ms
memory: 3536kb
input:
ans=0 for i in range(1,1000000): for j in range(i,1,-1): ans+=j print(ans)
output:
166666666665500001
result:
ok single line: '166666666665500001'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
ans=0 for i in range(31,321983,2): for j in range(313,382193): ans+=j print(ans)
output:
11756963404587200
result:
ok single line: '11756963404587200'
Test #5:
score: 0
Accepted
time: 3ms
memory: 3604kb
input:
ans=0 for i in range(1,1000000): for j in range(i,114514,-1): ans+=j print(ans)
output:
160610445975856765
result:
ok single line: '160610445975856765'
Extra Test:
score: 0
Extra Test Passed