bzoj1257: [CQOI2007]余数之和 整除分块

题意:给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值其中k mod i表示k除以i的余数。例如j(5, 3)=3 mod 1 + 3 mod 2 + 3 mod 3 + 3 mod 4 + 3 mod 5=0+1+0+3+3=7
题解k%i=k-\(\left\lfloor\frac{k}{i}\right\rfloor\) \(*i\),然后\(\left\lfloor\frac{k}{i}\right\rfloor\)只会有\(\sqrt{n}\)个取值,所以可以通过整除分块来一次性算出相同因子的贡献

/**************************************************************
    Problem: 1257
    User: walfy
    Language: C++
    Result: Accepted
    Time:64 ms
    Memory:1288 kb
****************************************************************/
 
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000003
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)
 
using namespace std;
 
const double eps=1e-6;
const int N=1000+10,maxn=1000000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
 
 
int main()
{
    ll n,k,ans=0;
    scanf("%lld%lld",&n,&k);
//    ll ans1=0;
//    for(ll i=1;i<=n;i++)ans1+=k%i;
//    printf("%lld\n",ans1);
    if(n>=k)
    {
        ans+=k*(n-k);
        n=k-1;
    }
    ans+=n*k;
    for(ll i=1,j;i<=n;i=j+1)
    {
        j=k/(k/i);
//        cout<<i<<" "<<j<<" "<<k/i<<endl;
        ll te=k/i;
        ans-=te*(min(n,j)+i)*(min(n,j)-i+1)/2;
        if(j>=n)break;
    }
    printf("%lld\n",ans);
    return 0;
}
/********************
 
********************/

posted @ 2018-05-28 19:44  walfy  阅读(128)  评论(0编辑  收藏  举报