The meaning of problems
求 $ \ sum _ {i = 1} ^ nk \ mod \ i $ ($ 1 \ leq n, k \ leq 10 ^ 9 $).
analysis
Such a large data range $ O (n) $ complexity also word within ah
The modulo sense, $ k \ mod \ i = k - \ left \ lfloor \ frac {k} {i} \ right \ rfloor * i $,
Therefore it is divisible by the block, note classification and discuss the relationship between $ k $ $ $ n-of.
#include<bits/stdc++.h> using namespace std; typedef long long ll; int n, k; ll solve() { ll ret = 1LL * n * k; if(k <= n) //需要分类讨论 { for(int i = 1,j;i <= k;i = j+1) { j = k / (k / i); ret -= 1LL * (i+j) * (j-i+1) / 2 * (k / i); } } else { for(int i = 1,j;i <= n;i = j+1) { j = min(k / (k / i), n); ret -= 1LL * (i+j) * (j-i+1) / 2 * (k / i); } } return ret; } int main() { scanf("%d%d", &n, &k); printf("%lld\n", solve()); return 0; }
Reference Links: https://zhuanlan.zhihu.com/p/77687419