首页 > 其他 > 详细

uva 1363 - Joseph's Problem(数论)

时间:2014-07-08 19:53:01      阅读:413      评论:0      收藏:0      [点我收藏+]

题目链接: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;
}

uva 1363 - Joseph's Problem(数论),布布扣,bubuko.com

uva 1363 - Joseph's Problem(数论)

原文:http://blog.csdn.net/keshuai19940722/article/details/37100087

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!