[Number Theory block] BZOJ 1257 and the remainder of the

Description

Given a positive integer n and k, calc j (n, k) = k mod 1 + k mod 2 + k mod 3 + ... + k mod n is
Where k mod i represents the remainder of k divided by i.
例如 j (5, 3) = 3 3 + v 1 v 2 + v 3 3 + v 3 4 + v 3 5 = 0 + 1 + 0 + 3 + 3 = 7

Input

Only the input line, comprising two integers n, k.
1<=n ,k<=10^9

 

Output

Output only one row, i.e., j (n, k).

Sample Input

5 3

Sample Output

7

analysis

$\sum _{i=1}^n k\ mod\ i$

易得$\sum _{i=1}^n k-\left \lfloor \frac{k}{i} \right \rfloor i$

$nk-\sum _{i=1}^n \left \lfloor \frac{k}{i} \right \rfloor i$

Because there is a demand interval is divisible by the same value, the block can be divisible

 

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
ll n,k,ans;

int main() {
    scanf("%lld%lld",&n,&k);ans=n*k;
    for (ll l=1,r=0;l<=n;l=r+1) {
        if (k/l) r=min(n,k/(1*k/l)); else r=n;
        ans-=(r-l+1)*(l+r)*(k/l)>>1;
    }
    printf("%lld",ans);
}
View Code

 

Guess you like

Origin www.cnblogs.com/mastervan/p/11367357.html