跳到正文

剪贴板

剪贴板 ndrakiy3

原文
剪贴板信息

剪贴板信息

收录
更新

P8813

AI-J00001:

C++
#include <iostream>

int main() {
    // 读取输入
    long long a, b;
    std::cin >> a >> b;

    // 计算 a 的 b 次方
    long long result = 1;
    for (int i = 0; i < b; ++i) {
        result *= a;

        // 判断是否超过 10^9
        if (result > 1000000000) {
            std::cout << -1 << std::endl;
            return 0;
        }
    }

    // 输出结果
    std::cout << result << std::endl;

    return 0;
}

AI-J00002:

C++
#include <iostream>

using namespace std;

const long long MOD = 1e9;

long long fast_power(long long a, long long b) {
    long long res = 1;
    while (b > 0) {
        if (b & 1) {
            res = res * a % MOD;
        }
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

int main() {
    long long a, b;
    cin >> a >> b;
    long long result = fast_power(a, b);
    if (result >= MOD) {
        cout << -1 << endl;
    } else {
        cout << result << endl;
    }
    return 0;
}

AI-J00003:

C++
#include <iostream>

using namespace std;

int main() {
    long long a, b;
    cin >> a >> b;

    if (a == 1) {
        cout << 1 << endl;
        return 0;
    }

    long long result = 1;
    for (int i = 1; i <= b; ++i) {
        result *= a;
        if (result >= 1e9) {
            cout << -1 << endl;
            return 0;
        }
    }

    cout << result << endl;
    return 0;
}

P8814

AI-J00001

C++
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

ll mod_pow(ll base, ll exp, ll mod) {
    ll result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
        exp /= 2;
    }
    return result;
}

bool miller_rabin(ll n, int k) {
    if (n == 2 || n == 3)
        return true;
    if (n % 2 == 0 || n == 1)
        return false;

    ll d = n - 1;
    int s = 0;

    while (d % 2 == 0) {
        d /= 2;
        s++;
    }

    for (int i = 0; i < k; i++) {
        ll a = rand() % (n - 3) + 2;
        ll x = mod_pow(a, d, n);

        if (x == 1 || x == n - 1)
            continue;

        for (int j = 0; j < s - 1; j++) {
            x = mod_pow(x, 2, n);
            if (x == n - 1)
                break;
        }

        if (x != n - 1)
            return false;
    }

    return true;
}

pair<ll, ll> find_pq(ll n, ll m) {
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            ll p = i;
            ll q = n / i;

            if (p < q && (p - 1) * (q - 1) % m == 0 && miller_rabin(p, 20) && miller_rabin(q, 20)) {
                return {p, q};
            }
        }
    }

    return {-1, -1};
}

int main() {
    srand(time(NULL));

    int k;
    cin >> k;

    for (int i = 0; i < k; i++) {
        ll n, d, e;
        cin >> n >> d >> e;

        ll m = n - e * d + 2;
        pair<ll, ll> result = find_pq(n, m);

        if (result.first == -1) {
            cout << "NO" << endl;
        } else {
            cout << min(result.first, result.second) << " " << max(result.first, result.second) << endl;
        }
    }

    return 0;
}

AI-J00002

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

bool check(ll n, ll e, ll d){
    ll m = n - e * d + 2;
    if(m <= 0) return false;
    ll x = sqrt(m);
    if(x * x == m) return false;
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int k = 10;
    for(int i = 0; i < k; i++){
        ll n, e, d;
        cin >> n >> e >> d;
        if(check(n, e, d)){
            ll x = sqrt(n);
            if(x * x == n) x--;
            cout << x << " " << n / x << "\n";
        } else {
            cout << "NO\n";
        }
    }
    return 0;
}

AI-J00003:

C++
#include <iostream>
#include <cmath>
#include <vector>
#include <set>

using namespace std;

int main() {
    int k;
    cin >> k;
    while (k--) {
        long long n, e, d, m;
        cin >> n >> d >> e;
        m = n - e * d + 2;
        if (m < 2) {
            cout << "NO" << endl;
            continue;
        }
        long long l = 2, r = m - 1, ans = 0;
        while (l <= r) {
            long long mid = l + (r - l) / 2;
            long long s = mid * (mid - 1) + 1;
            if (s == m) {
                ans = mid;
                break;
            } else if (s < m) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        if (ans == 0) {
            cout << "NO" << endl;
        } else {
            cout << ans << " " << n / ans << endl;
        }
    }
    return 0;
}

P8815

AI-J00001

C++
#include <iostream>
#include <stack>

using namespace std;

int main() {
    string expression;
    cin >> expression;

    stack<char> op_stack;
    stack<int> val_stack;
    int and_short_circuit = 0;
    int or_short_circuit = 0;

    for (char c : expression) {
        if (c == '0' || c == '1') {
            val_stack.push(c - '0');
        } else if (c == '&' || c == '|') {
            while (!op_stack.empty() && (op_stack.top() == '&' || op_stack.top() == '|')) {
                char top_op = op_stack.top();
                op_stack.pop();
                int b = val_stack.top();
                val_stack.pop();
                int a = val_stack.top();
                val_stack.pop();
                if (top_op == '&' && a == 0) {
                    and_short_circuit++;
                } else if (top_op == '|' && a == 1) {
                    or_short_circuit++;
                }
                val_stack.push((c == '&') ? (a & b) : (a | b));
            }
            op_stack.push(c);
        } else if (c == '(') {
            op_stack.push(c);
        } else if (c == ')') {
            while (op_stack.top() != '(') {
                char top_op = op_stack.top();
                op_stack.pop();
                int b = val_stack.top();
                val_stack.pop();
                int a = val_stack.top();
                val_stack.pop();
                if (top_op == '&' && a == 0) {
                    and_short_circuit++;
                } else if (top_op == '|' && a == 1) {
                    or_short_circuit++;
                }
                val_stack.push((top_op == '&') ? (a & b) : (a | b));
            }
            op_stack.pop(); // Pop '('
        }
    }

    while (!op_stack.empty()) {
        char top_op = op_stack.top();
        op_stack.pop();
        int b = val_stack.top();
        val_stack.pop();
        int a = val_stack.top();
        val_stack.pop();
        if (top_op == '&' && a == 0) {
            and_short_circuit++;
        } else if (top_op == '|' && a == 1) {
            or_short_circuit++;
        }
        val_stack.push((top_op == '&') ? (a & b) : (a | b));
    }

    cout << val_stack.top() << endl;
    cout << and_short_circuit << " " << or_short_circuit << endl;

    return 0;
}

AI-J00002

C++
#include <iostream>
#include <string>
using namespace std;

char getNextOperatorIndex(const string& s, int index) {
    while (index < s.length() && isdigit(s[index])) index++;
    return index;
}

char calculateNextOperatorIndex(const string& s, int index) {
    while (index < s.length() && (s[index] == '&' || s[index] == '|')) index++;
    return index;
}

int calculate(const string& s, int index) {
    if (index >= s.length()) return 0; // 已经处理完整个表达式,返回0

    char c = s[index];
    if (c == '0' || c == '1') return c - '0'; // 转换为逻辑值
    if (c == '(') { // 处理括号内的表达式
        int count = 0;
        while (s[index] != ')') {
            index++;
            count++;
        }
        string subExpr = s.substr(index + 1, count - 1); // 提取括号内的表达式
        int value = calculate(subExpr, 0); // 递归计算括号内的表达式
        return value; // 返回计算结果
    } else if (c == '&' || c == '|') { // 处理逻辑运算符
        int value = 0; // 初始值为0,表示假
        int shortCircuitCountAnd = 0, shortCircuitCountOr = 0; // 短路计数器
        int nextIndex = calculateNextOperatorIndex(s, index + 1); // 下一个运算符的索引位置
        if (s[index + 1] == '(') { // 需要添加括号的情况
            nextIndex = getNextOperatorIndex(s, nextIndex + 1); // 找到括号内的运算符位置
            value = calculate(s.substr(index + 2, nextIndex - index - 2), 0); // 递归计算括号内的表达式
            index = nextIndex; // 更新索引位置
        } else { // 不需要添加括号的情况
            if (s[nextIndex] == '&') shortCircuitCountAnd++; // & 的短路计数器加1
            if (s[nextIndex] == '|') shortCircuitCountOr++; // | 的短路计数器加1
            value = calculate(s.substr(nextIndex), nextIndex); // 递归计算当前运算符及其右侧的表达式
            index = nextIndex; // 更新索引位置
        }
        cout << shortCircuitCountAnd << " " << shortCircuitCountOr << endl; // 输出短路计数器的值
        return value; // 返回计算结果
    } else { // 其他情况返回0,表示假
        return 0;
    }
}

int main() {
    string s;
    cin >> s; // 读入逻辑表达式字符串s
    int value = calculate(s, 0); // 计算逻辑表达式的值并输出结果
    cout << value << endl; // 输出结果为0或1,表示假或真
    return 0;
}

AI-J00003:

C++
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <queue>

using namespace std;

int short_and = 0, short_or = 0;

int calc(string &s) {
    if(s[0] == '0')
        return 0;
    if(s[0] == '1')
        return 1;
    if(s[0] == '(') {
        int idx = s.find_first_of(')');
        int res = calc(s.substr(1, idx - 1));
        s = s.substr(idx + 1);
        return res;
    }
    int a = calc(s.substr(0, s.find('&')));
    short_and++;
    if(a == 0)
        return 0;
    s = s.substr(s.find('&') + 2);
    return calc(s) & 1;
}

int main() {
    string s;
    cin >> s;
    int ans = calc(s);
    cout << ans << endl;
    cout << short_and << ' ' << short_or << endl;
    return 0;
}

P8816

AI-J00001

C++
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;

    vector<pair<int, int>> points;

    // 输入给定的点
    for (int i = 0; i < n; ++i) {
        int x, y;
        cin >> x >> y;
        points.push_back({x, y});
    }

    // 对点按照横坐标和纵坐标分别排序
    sort(points.begin(), points.end());

    // 构建状态转移矩阵
    vector<vector<int>> dp(2 * n + 1, vector<int>(k + 1, -1));
    dp[n][0] = 0;

    // 更新状态转移矩阵
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= k; ++j) {
            if (dp[i][j] == -1) continue;

            int x = points[i].first;
            int y = points[i].second;

            // 在当前列添加点
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + 1);

            // 在当前行添加点
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);

            // 如果可以添加横纵坐标均增加的点,则更新状态
            if (j < k) {
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
            }
        }
    }

    // 找到最大长度
    int max_length = 0;
    for (int i = 0; i <= k; ++i) {
        max_length = max(max_length, max(dp[n][i], dp[n + i][i]));
    }

    cout << max_length << endl;

    return 0;
}

AI-J00002

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Point {
    int x, y;
};

int maxLen(const vector<Point>& points) {
    int n = points.size();
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
    for (int len = 1; len <= n; ++len) {
        for (int i = 0; i <= n - len; ++i) {
            int j = i + len - 1;
            if (points[j].x > points[i].x) {
                dp[i + 1][j] = dp[i][j];
            } else {
                dp[i + 1][j] = dp[i][j - 1] + 1;
            }
        }
    }
    return dp[n][n - 1];
}

int main() {
    int n, k;
    cin >> n >> k;
    vector<Point> points(n);
    for (int i = 0; i < n; ++i) {
        cin >> points[i].x >> points[i].y;
    }
    cout << maxLen(points) << endl;
    return 0;
}

AI-J00003

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

const int MAXN = 505;
typedef pair<int, int> PII;

int n, k;
PII points[MAXN];
int dp[MAXN]; // dp[i] 表示以 point[i] 为结尾的最长序列长度
int ans; // 记录最终答案

bool cmp(const PII &a, const PII &b) {
    return a.first < b.first || (a.first == b.first && a.second < b.second);
}

void update(int i, int val) {
    ans = max(ans, val);
    while (i) {
        dp[i] = max(dp[i], val);
        i -= i & -i;
    }
}

int query(int i) {
    int ret = 0;
    while (i <= n) {
        ret = max(ret, dp[i]);
        i += i & -i;
    }
    return ret;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> points[i].first >> points[i].second;
    }

    // 首先处理给定的点
    sort(points, points + n, cmp);
    for (int i = 0; i < n; ++i) {
        int j = i - 1;
        while (j >= 0 && points[j].first == points[i].first) {
            if (points[j].second + 1 == points[i].second) {
                update(j, query(j) + 1);
            }
            --j;
        }
        j = i - 1;
        while (j >= 0 && points[j].second == points[i].second) {
            if (points[j].first + 1 == points[i].first){
                update(j, query(j) + 1);
            }
            --j;
        }
    }

    // 接下来处理可以自由添加的点
    for (int dx = -1; dx <= 1; ++dx) {
        for (int dy = -1; dy <= 1; ++dy) {
            if (dx == 0 && dy == 0) continue;
            PII p = {points[n - 1].first + dx, points[n - 1].second + dy};
            int j = n - 1;
            while (j >= 0 && points[j].first == p.first) {
                if (points[j].second + 1 == p.second) {
                    update(j, query(j) + 1);
                }
                --j;
            }
            j = n - 1;
            while (j >= 0 && points[j].second == p.second) {
                if (points[j].first + 1 == p.first) {
                    update(j, query(j) + 1);
                }
                --j;
            }
        }
    }

    cout << ans << endl;
    return 0;
}