朴素能得个差不多吧……
这题改进算法真恶心
pascal一直过不了,难道非得转c++?
代码:(pascal)
var n,k,i,l,r,m:longint;
ans:qword;
function ceil(x:real):longint;
begin
if trunc(x)<x then exit(trunc(x)+) else exit(trunc(x));
end;
procedure main;
begin
readln(n,k);
ans:=;
if n>k then
begin
inc(ans,(n-k)*k);
n:=k;
end;
m:=ceil(sqrt(k));
for i:= to m do inc(ans,k mod i);
for i:= to m do
begin
l:=(k div (i+))+;
r:=(k div i);
if l<=m then l:=m+;
if r>n then r:=n;
if r<l then continue;
inc(ans,(((k<<)-i*(l+r))*(r-l+)>>));
end;
writeln(ans);
end;
begin
main;
end.
代码:(c++)
#include <iostream>
#include <cmath>
using namespace std; long long ans,n,k;
int main() {
ios::sync_with_stdio(false);
cin>>n>>k;
if (n>k) {
ans+=k*(n-k);
n=k;
}
long long sqrtk=ceil(sqrt(k));
for (long long i=;i<=sqrtk;++i) ans+=k%i;
for (long long a=;a<=sqrtk;++a) {
long long L=floor(k/(a+))+;
long long R=floor(k/a);
if (L<=sqrtk) L=sqrtk+;
if (R>n) R=n;
if (R<L) continue;
ans+=((k<<)-a*L-a*R)*(R-L+)>>;
}
cout<<ans;
}
另一种分块方法,pascal还是过不了……
#include <cmath>
#include <cstdio>
#include <algorithm> long long n, k, ans;
int main()
{
scanf("%lld%lld", &n, &k);
if (n > k)
{
ans += (n-k)*k;
n = k;
}
ans += n * k;
for (long long i = , last; i <= n; i = last+)
{
last = std::min(n, k/(k/i));
ans -= (k/i) * (i+last) * (last-i+1) / 2;
}
printf("%lld", ans);
}