1257: [CQOI2007]余数之和sum
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 3690 Solved: 1699
[ 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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 1E5 + 10;
typedef long long LL;
int n,k,tot,Num[maxn],res[maxn];
LL ans;
LL Sum(LL L,LL R)
{
return (L+R)*(R-L+1)/2;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> k; Num[0] = ~0U>>1;
for (int last = 1; last <= k; last++)
{
last = max(last,k/(k/last));
res[++tot] = last; Num[tot] = k/last;
}
for (int i = 0; i <= tot; i++)
if (Num[i+1] < n)
{
LL sum = Sum(Num[i+1]+1,min(Num[i],n));
LL g = min(Num[i],n) - Num[i+1];
ans += 1LL*k*g - 1LL*res[i]*sum;
}
cout << ans;
return 0;
}