P2261 [CQOI2007] remainder sum [divisible block]

Subject to the effect

Given a positive integer n and k calculated \ (G (n, k) = k \ \ bmod \ 1 + k \ \ bmod \ 2 + k \ \ bmod \ 3 + \ cdots + k \ \ bmod \ n \) of wherein the value \ (k \ \ BMOD \ i \) represents the remainder of i divided by k.

Resolve

A typical example of a divisible block.


Divisible blocking solution is of the form
\ [\ sum ^ n_ {i
= 1} ~ \ lfloor \ frac {n} {i} \ rfloor \] problem that complexity is \ (O (\ sqrt {n } ) \) .

Regularity is actually a class of problems, some of the playing table can be found for the continuous \ (I \) , \ (\ lfloor \ {n-FRAC {I}} \ rfloor \) have the same value. Specifically, for a \ (I \) , in an interval \ (i \ sim \ lfloor \ frac {n} {\ lfloor \ frac {n} {i} \ rfloor} \ rfloor \) , the \ (\ lfloor \ frac {n} {i } \ rfloor \) have the same value.

That is, for this problem, we just need to accumulate divisible block and each block on the line.


Back to this question, the meaning of problems into mathematical language
\ [\ sum_ {i = 1
} ^ n ~ k \ mod i \] The modular arithmetic definition can be written
\ [\ sum_ {i = 1 } ^ n ~ k- \ lfloor \ frac {k
} {i} \ rfloor * i \] i.e.
\ [n * k- \ sum_ { i = 1} ^ n ~ \ lfloor \ frac {k} {i} \ rfloor * i \]
things behind the block is divisible, for a block \ (L \ R & lt SIM \) , there are
\ [(r-l + 1 ) * k- (r-l + 1) \ lfloor \ frac {k} { i} \ rfloor * \ sum_ {
i = l} ^ r ~ \] so for a block, \ (\ sum_ {I} = L ^ R & lt ~ I \) is actually an arithmetic sequence, we find together out of it.

Reference Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define ll long long
using namespace std;
ll n,k;
int main()
{
    scanf("%d%d",&n,&k);
    ll tmp=0;
    for(int l=1,r=0;l<=n;l=r+1){
        if(!(k/l)) r=n;
        else r=min(k/(k/l),n);
        tmp+=(r-l+1)*(k/l)*(l+r)/2;
    }
    printf("%lld\n",n*k-tmp);
    return 0;
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11440608.html