H
H
Homemade2021-01-17 11:49:55
C++ / C#
Homemade, 2021-01-17 11:49:55

How to overcome overflow?

I decide

this
https://codeforces.com/problemset/problem/1471/A
task.
My
the code
#include <bits/stdc++.h>

using namespace std;

long long div_up(int x, int y)
{
    return (x - 1) / y + 1;
}

int main()
{
    int t;
    cin>>t;
    while(t--){
        long long n,x;
        cin>>n>>x;
        vector<int> a(n);
        for(int i=0;i<n;i++)
            cin>>a[i];
        long long sum = 0;
        for(int i=0;i<n;i++)
            sum = sum + a[i];

        long long sum2 = 0;
        for(int i=0;i<n;i++)
            sum2 = sum2 + div_up(a[i],x);

        long long mn = div_up(sum,x),mx = sum2;

        cout<<mn<<" "<<mx<<endl;
    }
    return 0;
}

On a large number, it gave an error:

Input
1
100000 501901246 856802259 964126361 36942580
586287303 899177135 447296776 ...
Output
2 149973
Answer
99678 149973 but I don't know how.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-01-17
@Homemade

Analyze how beauty changes during the operation of adding elements and work with the remainder of the division.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question