1789E - Serval and Music Game - CodeForces Solution


brute force math number theory

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#include <stdio.h>
#include <algorithm>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define clr(x) vector<int>().swap(x);
#define all(x) x.begin(), x.end()
#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define popf pop_front
#define ll long long
#define ld long double
#define ull unsigned long long
#define random mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define F first
#define S second
const ll INF=1e18;
const ll MN=-1e9;
const ll MX=1e7+9;
const ll MXX=1e9;
const ll SQ=4e2;
const ll MOD=998244353;
const ll PP=1e6+3;
const ld PI=3.141592653589793;
const ld eps=1e-6;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;

int n, m, k, cur, ans, f[MX];

int mul(int x, int y) {
    return 1ll*x*y % MOD;
}

int add(int x, int y) {
    x += y;
    if (x >= MOD) {
        x -= MOD;
    }
    return x;
}

int get(int x) {
    int res = n;
    for (int y=1; y<x && x*(y-1)+y<=m; y++) {
        int l = x*(y-1)+y;
        int r = x*y-1;
        r = min(r, m);
        res -= f[r] - f[l-1];
    }
    return res;
}

int go(int x) {
    int res = 0;
    for (int y=x; y<=m; y+=x) {
        res += f[y] - f[y-1];
    }
    return res;
}

void init() {
    ans = 0;
    for (int i=1; i<=m; i++) {
        f[i] = 0;
    }
}

int main() {
    FAST;
    int t;
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i=1; i<=n; i++) {
            cin >> m;
            f[m]++;
        }
        for (int x=1; x<=m; x++) {
            f[x] += f[x-1];
        }
        for (int x=1; x<=m; x++) {
            k = m/x;
            if (m % x) {
                if (k < m/(x-1)) {
                    cur = get(k);
                }
            }
            else {
                cur = go(k);
            }
            ans = add(ans, mul(x, cur));
        }
        cout << ans << endl;
        init();
    }
}

/*



*/


Comments

Submit
0 Comments
More Questions

232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name