bzoj1257: [CQOI2007]余数之和sum
分段优化。a=k/i=>[l,r]=>a*i<=k,i<=k/a=k/(k/i)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define clr(x,c) memset(x,c,sizeof(x)) #define ll long long int main(){ ll n,k;scanf("%lld%lld",&n,&k); ll ans=0,last; if(n>k) ans+=k*(n-k),n=k; for(ll i=1;i<=n;i=last+1){ last=min(n,k/(k/i)); ans+=(last-i+1)*(k%last+k%i)/2; } printf("%lld\n",ans); return 0; }
1257: [CQOI2007]余数之和sum
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 3516 Solved: 1630
[Submit][Status][Discuss]
Description
给出正整数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
Input
输入仅一行,包含两个整数n, k。
Output
输出仅一行,即j(n, k)。
Sample Input
5 3
Sample Output
7
HINT
50%的数据满足:1<=n, k<=1000 100%的数据满足:1<=n ,k<=10^9