682A - Alyona And Numbers
31 Dec 2020 — Tags: math,modulo — URLSo, we must pair numbers with remainder $x$ with those with remainder $5-x$, $\mod 5$. Since I’m lazy I just precomputed such counts in $O(\max(n, m))$.
Time complexity: $O(1)$
Memory complexity: $O(\max(n, m))$
Click to show code.
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
ll solve(int n, int m)
{
vi nrem(5, 0), mrem(5, 0);
for (int i = 1; i <= max(n, m); ++i)
{
int rem = i % 5;
if (i <= n)
nrem[rem]++;
if (i <= m)
mrem[rem]++;
}
ll ans = 0;
for (int i = 0; i < 5; ++i)
ans += ll(nrem[i]) * ll(mrem[(5 - i) % 5]);
return ans;
}
int main(void)
{
ios::sync_with_stdio(false), cin.tie(NULL);
int n, m;
cin >> n >> m;
cout << solve(n, m) << endl;
return 0;
}