QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#551943#9255. Python Programucup-team3734#AC ✓7ms3832kbC++233.0kb2024-09-07 19:15:342024-09-07 19:15:35

Judging History

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

  • [2024-09-07 19:15:35]
  • 评测
  • 测评结果:AC
  • 用时:7ms
  • 内存:3832kb
  • [2024-09-07 19:15:34]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long u64;
typedef double lf;

vector<string> split(string s, char delim) {
    vector<string> res;
    string cur;
    for (char c : s) {
        if (c == delim) {
            res.push_back(cur);
            cur = "";
        } else {
            cur += c;
        }
    }
    res.push_back(cur);
    return res;
}

string strip(string s, string chars) {
    string res;
    for (char c : s) {
        if (chars.find(c) == string::npos) {
            res += c;
        }
    }
    return s;
}

tuple<string, string, string, string> find_range(string s) {
    string var;
    auto parts = split(s, ' ');
    for (size_t i = 0; i < parts.size(); i++) {
        if (parts[i] == "for") {
            var = parts[i + 1];
        }
    }
    for (size_t i = 0; i < parts.size(); i++) {
        if (parts[i].substr(0, 6) == "range(") {
            auto range = split(parts[i].substr(6, parts[i].size() - 7), ',');
            if (range.size() == 2) {
                return {var, strip(range[0], " )"), strip(range[1], " )"), "1"};
            }
            return {var, strip(range[0], " )"), strip(range[1], " )"), strip(range[2], " )")};
        }
    }
    assert(false);
}

int lenrange(int x, int y, int z) {
    if (z > 0) {
        if (x >= y) {
            return 0;
        }
        return (y - x + z - 1) / z;
    } else {
        if (x <= y) {
            return 0;
        }
        return (x - y - z - 1) / -z;
    }
}

int last_range(int x, int y, int z) {
    return x + z * (lenrange(x, y, z) - 1);
}

void solve() {
    assert(lenrange(0, 10, 1) == 10);
    assert(lenrange(0, 10, 2) == 5);
    assert(lenrange(0, 11, 2) == 6);
    assert(lenrange(0, 11, -2) == 0);
    assert(lenrange(10, 0, -1) == 10);

    string l1, l2;
    getline(cin, l1);
    getline(cin, l1);
    getline(cin, l2);

    auto [v, xs, ys, zs] = find_range(l1);
    auto x = stoi(xs);
    auto y = stoi(ys);
    auto z = stoi(zs);
    assert(z != 0);

    auto [_, as, bs, cs] = find_range(l2);
    int ia = 0, ib = 0, ic = stoi(cs);
    if (as != v) {
        ia = stoi(as);
    }
    if (bs != v) {
        ib = stoi(bs);
    }
    
    i64 res = 0;
    auto calcx = [&](int cur) {
        auto start = as == v ? cur : ia;
        auto end = bs == v ? cur : ib;
        int l = lenrange(start, end, ic);
        if (l == 0) {
            return;
        }
        auto last = last_range(start, end, ic);
        res += i64(l) * i64(last + start) / 2;
    };

    if (z > 0) {
        for (int cur = x; cur < y; cur += z) {
            calcx(cur);
        }
    } else {
        for (int cur = x; cur > y; cur += z) {
            calcx(cur);
        }
    }
    cout << res << endl;
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    int t = 1;
    // cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
}

详细

Test #1:

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

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

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

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

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

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