BZOJ1257: [CQOI2007]余数之和sum
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1257
n%i=n-n/i*x 然后我们就可以枚举这个n/i,发现这是一个等差数列
#include<cstring> #include<iostream> #include<algorithm> #include<cstdio> #define maxn 109 #define rep(i,l,r) for (int i=l;i<=r;i++) #define down(i,l,r) for (int i=l;i>=r;i--) #define clr(x,y) memset(x,y,sizeof(x)) #define ll long long #define inf int(1e9) using namespace std; int k,n,r,l,t; ll ans; int read(){ int x=0,f=1; char ch=getchar(); while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); } while (isdigit(ch)){ x=x*10+ch-'0'; ch=getchar(); } return x*f; } int main(){ k=read(); n=read(); if (k>n) { ans+=(ll)(k-n)*n; k=n; } for (int i=1;i<=k;i=r+1){ t=n/i; r=min(k,n/t); l=i; ans+=(ll)(r-l+1)*n-(ll)(r-l+1)*(r+l)*t/2; } printf("%lld\n",ans); return 0; }