E. Sum of Remainders(数论分块)

154 阅读1分钟

题目

Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + ... + n mod m. As the result can be very large, you should print the value modulo 109 + 7 (the remainder when divided by 109 + 7).

The modulo operator a mod b stands for the remainder after dividing a by b. For example 10 mod 3 = 1.

计算和的值:n mod 1 + n mod 2 + n mod 3 + ... + 由于结果可能非常大,你应该打印109+7的模数(除以109+7后的余数)。

模运算符a mod b代表a除以b后的余数,例如10 mod 3 = 1。

输入

The only line contains two integers n, m (1 ≤ n, m ≤ 1013) — the parameters of the sum. 唯一一行包含两个整数n,m(1≤n,m≤1013)--和的参数。

输出

Print integer s — the value of the required sum modulo 109 + 7. 打印整数s--所要求的和的值,即109+7的模数。

解法

题目是求i=1m\sum_{i=1}^mn%i的值然后对1e9取模我们可知n%m = n -[n/m] *m所有化简后为nmi=1m[n/i]in ∗ m − ∑_{i=1}^m[n/i]*i

Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define endl '\n'
#define pb push_back
#define NO cout << "NO" << endl;
#define YES cout << "YES" << endl;
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<VI> VII;
ll MOD = 998244353;
ll powmod(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1)
ll powmod(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
mt19937 mrand(random_device{}()); 
const int N = 2e5 + 10;
const int M = 2e5 + 101;
//123456
//123
void solve()
{
    ll n, m;
    scanf("%lld %lld", &n, &m);
    ll inv = qmi(2ll, MOD - 2);
    ll res = 0;
    ll ans = (n % MOD) * (m % MOD) % MOD;
    for (ll l = 1, r; l <= min(n, m); l ++) {
        r = min(n / (n / l), m);
        ll len = (r - l + 1) % MOD;
        res += (n / l) % MOD * ((l + r) % MOD) % MOD * len % MOD * inv % MOD;
        res %= MOD;
        l = r;
    }
    printf("%lld", (ans - res + MOD) % MOD);
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    // int T;cin >> T;
    // while ( T -- )
    solve();
   }
```cpp