首页 > 代码库 > uva 1363 - Joseph's Problem(数论)
uva 1363 - Joseph's Problem(数论)
题目链接:uva 1363 - Joseph‘s Problem
题目大意:给定n,k,求∑i=1n(k%i).
解题思路:参考别人的,自己想了很久,详细题解
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
ll solve (ll n, ll k) {
ll sum = 0;
if (n > k)
sum += (n-k) * k;
ll a = sqrt(k+0.5), b = k / a;
for (ll i = a; i > 1; i--) {
ll s = k / i;
ll e = k / (i-1);
if (s > n)
break;
if (e > n)
e = n;
sum += ( (k%(s+1) + k%e) * (e - s) / 2);
}
for (ll i = 2; i <= b && i <= n; i++)
sum += k%i;
return sum;
}
int main () {
ll n, k;
while (scanf("%lld%lld", &n, &k) == 2) {
printf("%lld\n", solve(n, k));
}
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。