BZOJ1257: [CQOI2007]余数之和

设数列 Ai=X mod i 
分析可得   余数是一些等差数列头尾相接组合起来的  然后就是二分找这些等差数列喽


#include<cstdio> 
#define SUM(L,R) (((L+R)*(R-L+1))>>1) 
using namespace std;
#define LL long long 
LL n,k,ans; 

int main()
{ 	
  scanf("%lld%lld",&n,&k);
  ans=n*k;
  LL s=1,L,R,M;
  for(;s<=n&&s<=k;s=L+1)
  { 
    		L=s;R=n;
   		    while(L<R)
			{ 			
			   M=(L+R+1)>>1;
			   if(k/s>k/M)
			         R=M-1;
	 		    else L=M;
	 		}
	 		ans-=SUM(s,R)*(k/s);
	} 	
   printf("%lld\n",ans);
 	return 0;
}

你可能感兴趣的:(二分,bzoj)