BZOJ 1257 [CQOI2007]余数之和sum

题解:a%b=a-[a/b]*b;

ans=n*k-sigma([k/i]*i]);

[k/i]有sqrt(k)种取值

last=k/(k/last);

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long Lint;

int n,m;
Lint ans;
int main(){
	cin>>n>>m;
	ans=1LL*n*m;
	if(m>n){
		int last;
		for(int i=1;i<=n;i=last+1){
			last=min(n,m/(m/i));
			ans=ans-(last-i+1)*1LL*(last+i)*(m/i)/2;
		}
	}else{
		int last;
		for(int i=1;i<=m;i=last+1){
			last=m/(m/i);
			ans=ans-(last-i+1)*1LL*(last+i)*(m/i)/2;
		}
	}
	cout<<ans<<endl;
	return 0;
}

  

 

posted @ 2018-02-20 12:02  ws_zzy  阅读(112)  评论(0编辑  收藏  举报