1257: [CQOI2007]余数之和sum
Time Limit: 5 Sec Memory Limit: 162 MB
Submit: 3769 Solved: 1734
[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
Source
分析:
最近学习数学...先写道水题压压惊TAT...
Σ(1<=i<=n) k%i
=Σ(1<=i<=n) k-(k/i)*i
=n*k-Σ(1<=i<=n) (k/i)*i
发现k/i的值不超过sqrt(n)种,可以分段计算,而且貌似n>k的时候答案是固定的...
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
//by NeighThorn
using namespace std;
//大鹏一日同风起,扶摇直上九万里 int n,k,m; long long ans; signed main(void){
scanf("%d%d",&n,&k);
ans=(long long)n*(long long)k;
if(n>k)n=k;
for(int i=,l,r,x;i<=n;i=r+){
x=k/i;r=k/x,l=k/(x+)+;
if(r>n)
r=n;
ans-=(long long)(r+l)*(long long)(r-l+)*x/;
}
printf("%lld\n",ans);
return ;
}
by NeighThorn