题意:输入$k,n$,求$\sum_{i=1}^n k \mod i$
$k \mod i=k-i*\lfloor \frac{k}{i} \rfloor $,$n$个$k$直接求和,后面那个东西像比较套路的分段求和
算k/(k/i)这种东西的时候还要注意判一下分母为0什么的…
#include<cstdio> typedef long long lint; lint n,k,ans; inline lint min(lint a,lint b){return a<b?a:b;} inline lint getsum(lint i) { return i*(i+1)/2; } int main() { scanf("%lld%lld",&n,&k); ans=n*k; for(register lint i=1,pos;i<=n;i=pos+1) { pos=(k/i?min(n,k/(k/i)):n); ans-=(getsum(pos)-getsum(i-1))*(k/i); } printf("%lld",ans); return 0; }
原文地址:https://www.cnblogs.com/yoooshinow/p/8428468.html
时间: 2024-10-10 19:28:06