Codeforces Round 4 Challenges

Codeforces Round 4 Challenges

A-String Construction Challenge

Output several 'you' strings and fill the rest with arbitrary characters

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n,m;
    cin >>n >> m;
    if(n < m * 3){
        cout << -1 << endl;
    }else{
        for(int i = 0;i < m;i ++)
            cout << "you";
        for(int i = 0; i < n - m * 3;i ++)
            cout << 'y';
    }

    return 0;
}


B-Integer Splitting Challenge

Find pairs (a, b) such that a + b = n and a * b is divisible by 3. When n is divisbile by 3, count all multiples of 3 in [1, n). If n is not divisible by 3, multiply the count by two as the pairs are interchangeable.

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    if(n % 3 == 0)
        cout << (n - 1)/ 3  << endl;
    else
        cout << (n - 1) / 3 * 2 << endl;
    
    return 0;
}


C-Integer Operation Challenge

Operation 1 contributes n * x, while operation 2 contributes -n * x when non-negative. Adjust the array by subtracting the minimum value to make all elements non-negative. Track the total contribution from operation 1 and determine if operation 2 can offset it. If not, record the excess and subtract it at the end. The final result is the sum of the adjusted array plus the contribution from operation 1 multiplied by n, modulo 1e9 + 7.

#include <bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

signed main() {

    int n,k;
    cin >> n >> k;
    vector<int> a(n);
    for(auto &i : a) cin >> i;

    int mi = *min_element(a.begin(), a.end());
    int sum = mi, cha = 0;
    const int mod = 1e9 + 7;

    while(k--){
        int op,x;
        cin >> op >> x;
        if(op == 1){
            sum += x;
        }else{
            if(x <= sum)
                sum -= x;
            else{
                cha += x - sum;
                sum = 0;
            }
        }
    }

    for(auto &i : a)
        i = max(i - mi - cha, 0ll);

    int ans = 0;
    for(auto i : a) ans += i;
    ans = (ans % mod + sum % mod * n) % mod;

    cout << ans << endl;

    return 0;
}


D-Factor Calculation Challenge

Compute the factors of a and b, then find the product of each pair of factors. If i divides a and j divides b, then i * j will always be divisible by a * b.

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
   
    int a,b;
    cin >> a >> b;

    int n = a,m = b;
    set<int> ans;
    vector<int> p,q;

    for(int i = 1;i <= sqrt(b) ; i++){
       if(b % i == 0){
           p.push_back(i);
           if(i * i != b)
           p.push_back(b / i);
       }
    }

    for(int i = 1;i <= sqrt(a) ; i++){
        if(a % i == 0){
            q.push_back(i);
            if(i * i != a)
            q.push_back(a / i);
        }
    }

    for(auto i : p){
        for(auto j : q){
            ans.insert(i * j);
        }
    }

    cout << ans.size() << endl;
    for(auto i : ans)
        cout << i << ' ';
    
    
    return 0;
}


E is a large simulation, not implemented.

Tags: Codeforces Competitive Programming algorithms Problem Solving C++

Posted on Sun, 10 May 2026 20:01:07 +0000 by signs