BZOJ1257 [CQOI2007]余数之和sum

[Problem]

给出正整数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


[Solution]

It's now not easy to find such an easy problem on BZOJ.
Consider k % i = k - i * [k / i] => ans = k * n - sigma(i * [k / i]).
[k / i] is something just like we do using Function Mobius. The optimization can make the time complex sqrt(k).
By the way, when i >= k, [k / i] = 0. At that time, you should break or the program can get crashed.
Then it's very easy to code.


[Code]

#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long qw;

#ifdef WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif

qw k, n, s;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif

	scanf(lld lld, &n, &k);
	s = n * k;
	for (qw i = 1, j; i <= n; i = j + 1)
		if (k / i == 0)
			break;
		else {
			j = min(n, k / (k / i));
			s -= (j - i + 1) * (j + i) * (k / i) / 2;
		}
	printf(lld "\n", s);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值