[BZOJ1257][CQOI2007]余数之和sum
1257: [CQOI2007]余数之和sum
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4604 Solved: 2143
[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
x%y=x-(int)(x/y)*y
我们找出所有(int)(x/y)相等的y,按等差数列求和即可得出答案
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 long long n,k; 9 long long ans; 10 int main() 11 { 12 scanf("%lld%lld",&n,&k); 13 if(n>k) ans=(n-k)*k,n=k; 14 for(long long i=1;i<=n;) 15 { 16 long long t=k/i,r=k/t; 17 if(r>=n)r=n; 18 ans+=(r-i+1)*k-(r-i+1)*(i+r)/2*t; 19 i=r+1; 20 } 21 printf("%lld\n",ans); 22 return 0; 23 }
O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~