cf C. Floor and Mod

cf C. Floor and Mod

\(\sum_{j=1}^Nmin(j-1,{l\over j+1})\)

$当j-1={l\over j+1}时,l=j^2-1,j=\sqrt{l+1} $

\(所以原式=\sum_{j=\sqrt{l+1}+1}^N{l\over j+1}\)

数论分块

#define inf 2333333333333333
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2021.2.16
using namespace std;
long long T,l,r,cnt,k,ans;
void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

signed main(){
    in(T);
    while(T--){
    	in(l);in(r);
    	k=sqrt(l+1);
    	if(r<=k)ans=r*(r-1)/2;
    	if(r>k){
    		ans=k*(k-1)/2;
    		long long t,j=k+1,u;
    		for(t=j+1;t<=r+1 && t<=l;t=u+1){
    			u=min(r+1,l/(l/t));
    			ans+=l/t*(u-t+1);
    		}
    	}
    	o(ans);p('\n');
    }
    return 0;
}
posted @ 2021-02-16 21:48  WeiAR  阅读(42)  评论(0编辑  收藏  举报