QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#552806#9255. Python Programucup-team1766#AC ✓41ms3640kbC++172.0kb2024-09-08 02:39:352024-09-08 02:39:35

Judging History

你现在查看的是最新测评结果

  • [2024-09-08 02:39:35]
  • 评测
  • 测评结果:AC
  • 用时:41ms
  • 内存:3640kb
  • [2024-09-08 02:39:35]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

vector<string> parse_loop_vars(string line) {
    bool first_r = true;
    int ind = -1;
    vector<string> vars;
    for (int i = 0; i < line.length(); i++) {
        if (first_r && line[i] == 'r') {
            first_r = false;
            vars.push_back(line.substr(i + 2, 1));
        }
        if (ind == -1 && line[i] == '(') {
            ind = i + 1;
            break; 
        }
    }
    while (true) {
        int next_ind = ind;
        while (line[next_ind] != ',' && line[next_ind] != ')') {
            next_ind++;
        }
        vars.push_back(line.substr(ind, next_ind - ind));
        if (line[next_ind] == ')') {
            break;
        }
        ind = next_ind + 1;
    }
    return vars;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string line;
    getline(cin, line);
    getline(cin, line);
    vector<string> vars1 = parse_loop_vars(line);
    getline(cin, line);
    vector<string> vars2 = parse_loop_vars(line);
    getline(cin, line);
    getline(cin, line);
    
    int i = stoi(vars1[1]);
    int b = stoi(vars1[2]);
    int c = (vars1.size() == 4 ? stoi(vars1[3]) : 1);
    long long res = 0;
    while ((c > 0 && i < b) || (c < 0 && i > b)) {
        int j = (isalpha(vars2[1][0]) ? i : stoi(vars2[1]));
        int e = (isalpha(vars2[2][0]) ? i : stoi(vars2[2]));
        int f = 1;
        if (vars2.size() == 4) {
            f = (isalpha(vars2[3][0]) ? i : stoi(vars2[3]));
        }
        
        if (f < 0) {
            if (j > e) {
                int n = (j - e - 1) / (-f) + 1;
                int last = j + (j - e - 1) / (-f) * f;
                res += (long long) (j + last) * n / 2;
            }
        } else {
            if (j < e) {
                int n = (e - j - 1) / abs(f) + 1;
                int last = j + (e - j - 1) / f * f;
                res += (long long) (j + last) * n / 2;
            }
        }
        i += c;
    }
    cout << res << "\n";
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3596kb

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: 3564kb

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: 32ms
memory: 3640kb

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: 7ms
memory: 3568kb

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: 41ms
memory: 3624kb

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