BZOJ2956: molding the product and - the block is divisible

The meaning of problems

Request $ \ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ m (n \ mod \ i) * (m \ mod \ j) $ ($ i \ neq j $), $ n, m \ leq 10 ^ 9 $ $ 19,940,417 $ answer to the modulo.

analysis:

Since the modulus may be rounded into the form, $ k \ mod \ i = k - \ left \ lfloor \ frac {k} {i} \ right \ rfloor * i $, refer to the number of I and BZOJ1257 .

易知,$\sum_{i=1}^n \sum_{j=1}^m (n \ mod \ i)*(m \ mod \ j) = \sum_{i=1}^n(n \ mod \ i)\sum_{j=1}^m(m \ mod \ j)$

So the answer is in two parts and the remainder of the product is equal to minus $ i $ $ j $ circumstances, the

当 $i=j$ 时,
$$
\begin{aligned}
\sum_{i=1}^{min(n,m)}(n \ mod \ i)(m \ mod \ i)  & = \sum_{i=1}^{min(n,m)}(n - \left \lfloor \frac{n}{i}  \right  \rfloor  i)(m - \left \lfloor \frac{m}{i} \right \rfloor  i) \\
&= \sum_{i=1}^{min(n,m)}(nm - m\left \lfloor \frac{n}{i} \right \rfloor  i - n\left \lfloor \frac{m}{i} \right \rfloor i + \left \lfloor \frac{n}{i} \right \rfloor \left \lfloor \frac{m}{i} \right \rfloor i^2)  \\
\end{aligned}$$

Code:

 

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll mod = 19940417;
const ll inv6 = 3323403;
ll n, m;

//\sum_1^n [k/i]*i
ll S1(ll n, ll k)
{
    ll ret = 0;
    if(k <= n)   //需要分类讨论
    {
        for(ll i = 1,j;i <= k;i = j+1)
        {
            j = k / (k / i);

            ret = (Right + (i + j) * (j-i + 1 ) / 2 % v * (k / l)% v)% v; 
        } 
    } 
    Else 
    { 
        for (II i = 1 , j; i <= n; i = j + 1 ) 
        { 
            j = min (k / (k / i), n); 
            right = (Right + (i + j) * (j-i + 1 ) / 2 % v * (k / l)% v)% v; 
        } 
    } 

    Return right; 
} 

Ll S 2 (II n) 
{ 
    n % = v;
    Return n * (n + 1 ) * v% (2*n+1) % mod * inv6 % mod;
}

//[n/i][m/i]i^2
ll S3(ll n, ll m)
{
    ll ret = 0;
    for(ll i = 1,j;i <= min(n, m);i = j+1)
    {
        j = min(n/(n/i), m/(m/i));
        ret = (ret + (n/i) * (m/i) % mod * (S2(j) - S2(i-1)) % mod) % mod;
    }
    return ret;
}

int main()
{
    scanf("%d%d", &n, &m);
    if(m > n) swap(n, m);
    ll ans = 1;
    ans = ans * (n*n%mod - S1(n, n) + mod) % mod;
    ans = ans * (m*m%mod - S1(m, m) + mod) % mod;
    ll ans2 = n *m % mod * m % mod;
    ans2 = (ans2 - m * S1(m, n) % mod) % mod;
    ans2 = (ans2 - n * S1(m, m) % mod) % mod;
    ans2 = (ans2 + S3(n, m)) % mod;
    //printf("%lld %lld\n", ans, ans2);
    printf("%lld\n", (ans - ans2 + 2*mod) % mod);
    return 0;
}

 

 

 

 

Reference Links: https://www.cnblogs.com/henry-1202/p/10201032.html

Guess you like

Origin www.cnblogs.com/lfri/p/11348561.html