Solving AtCoder Beginner Contest 356 Problems

Problem A: Array Segment Reversal

Given an array of integers from 1 to n, reverse a specified segment between indices l and r.

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n, l, r;
    cin >> n >> l >> r;
    int arr[n+1];
    for(int i=1; i<=n; i++) arr[i] = i;
    reverse(arr+l, arr+r+1);
    for(int i=1; i<=n; i++) cout << arr[i] << " ";
    return 0;
}

Problem B: Nutrient Check

Verify if the total nutrients from multiple food items meet daily requirements.

#include <iostream>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int req[m], total[m] = {0};
    for(int i=0; i<m; i++) cin >> req[i];
    for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
            int x; cin >> x;
            total[j] += x;
        }
    }
    bool sufficient = true;
    for(int i=0; i<m; i++) {
        if(total[i] < req[i]) {
            sufficient = false;
            break;
        }
    }
    cout << (sufficient ? "Yes" : "No");
    return 0;
}

Problem C: Key Combination Validation

Count valid key combiantions that satisfy all given test condisions.

#include <iostream>
using namespace std;

int main() {
    int n, m, k;
    cin >> n >> m >> k;
    int tests[m], masks[m];
    bool results[m];
    for(int i=0; i<m; i++) {
        int c; cin >> c;
        tests[i] = c;
        masks[i] = 0;
        for(int j=0; j<c; j++) {
            int a; cin >> a;
            masks[i] |= (1 << (a-1));
        }
        char res; cin >> res;
        results[i] = (res == 'o');
    }
    int count = 0;
    for(int mask=0; mask<(1<<n); mask++) {
        bool valid = true;
        for(int i=0; i<m; i++) {
            int bits = __builtin_popcount(mask & masks[i]);
            if((bits >= k) != results[i]) {
                valid = false;
                break;
            }
        }
        if(valid) count++;
    }
    cout << count;
    return 0;
}

Problem D: Binary Bit Counting

Calculate the count of set bits in specific positions across a range of numbers.

#include <iostream>
using namespace std;

const int MOD = 998244353;

int main() {
    long long n, m;
    cin >> n >> m;
    long long result = 0;
    for(int i=0; i<60; i++) {
        long long full_cycles = (n+1) / (1LL << (i+1));
        long long count = full_cycles * (1LL << i);
        long long remainder = (n+1) % (1LL << (i+1));
        if(remainder > (1LL << i)) count += remainder - (1LL << i);
        if(m & (1LL << i)) result = (result + count) % MOD;
    }
    cout << result;
    return 0;
}

Problem E: Sum of Integer Divisions

Compute the sum of floor divisions between all pairs of numbers in a sorted array.

#include <iostream>
#include <algorithm>
using namespace std;

const int MAX = 1e6+5;
int freq[MAX] = {0};

int main() {
    int n;
    cin >> n;
    int arr[n], max_val = 0;
    long long answer = 0;
    for(int i=0; i<n; i++) {
        cin >> arr[i];
        freq[arr[i]]++;
        max_val = max(max_val, arr[i]);
    }
    for(int i=1; i<=max_val; i++) {
        if(freq[i]) answer += (long long)freq[i] * (freq[i]-1) / 2;
        freq[i] += freq[i-1];
    }
    sort(arr, arr+n);
    for(int i=0; i<n; i++) {
        int val = arr[i];
        for(int j=1; j*j <= val; j++) {
            int lower = val/(j+1) + 1;
            int upper = min(val/j, val-1);
            if(lower <= upper) answer += j * (freq[upper] - freq[lower-1]);
        }
        for(int j=1; (val/j) > val/(j+1); j++) {
            if(j >= val) continue;
            answer += (val/j) * (freq[j] - freq[j-1]);
        }
    }
    cout << answer;
    return 0;
}

Tags: competitive-programming AtCoder algorithm problem-solving

Posted on Sat, 08 Aug 2026 17:00:05 +0000 by Buchead