BZOJ 1257: [CQOI2007]余数之和sum

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的时候答案是固定的...

代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 //by NeighThorn
 7 using namespace std;
 8 //大鹏一日同风起,扶摇直上九万里 
 9 
10 int n,k,m; 
11 
12 long long ans;
13 
14 signed main(void){
15     scanf("%d%d",&n,&k);
16     ans=(long long)n*(long long)k;
17     if(n>k)n=k;
18     for(int i=1,l,r,x;i<=n;i=r+1){
19         x=k/i;r=k/x,l=k/(x+1)+1;
20         if(r>n)
21             r=n;
22         ans-=(long long)(r+l)*(long long)(r-l+1)*x/2;
23     }
24     printf("%lld\n",ans);
25     return 0;
26 }
View Code

by NeighThorn

posted @ 2016-12-23 11:22  NeighThorn  阅读(204)  评论(0编辑  收藏  举报