Ice(n, k)(数论)

Ice Rain------I was waiting for a girl, or waiting for been addicted to the bitter sea. Love for irrigation in silence. No one considered whether the flowers came out or wither. Love which I am not sure swing left and right. I had no choice but to put my sadness into my heart deeply.

Yifenfei was waiting for a girl come out, but never.

His love is caught by Lemon Demon. So yifenfei ’s heart is “Da Xue Fen Fei” like his name.

The weather is cold. Ice as quickly as rain dropped. Lemon said to yifenfei, if he can solve his problem which is to calculate the value of
, he will release his love.

Unluckily, yifenfei was bored with Number Theory problem, now you, with intelligent, please help him to find yifenfei’s First Love.

InputGiven two integers n, k(1 <= n, k <= 10
9).OutputFor each n and k, print Ice(n, k) in a single line.
Sample Input
5 4
5 3
Sample Output
5
7
题意:求k对于大于等于1且小于等于n的正整数取余和为多少;
解题思想:首先想到的时一个循环就解决的事,其实并没有那么简单,这样的暴力解法肯定会超时,所以这样是不行的,仔细思考,就按照样列分析一下,4%1=0,4%2=0,4%3=1,4%4=0,4%5=4;这里可以看出n比k大的部分余数就为k;然后小于k的那些也有同样段,那就是当 ij 的商是一样的,那么他们之间的余数就是一个公差为1递增的数列,所以可以分段求解;k % i==k−i×t,t为商转换求解的公式,可以看出每一项都是这个公式,那么ans的初始值就为k*n,然后每一段都减去一个等差数列的和就可以了;

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    long long n,k;
    while(~scanf("%lld%lld",&n,&k))
        {
            long long ans=n*k;
            if(n>k) n=k;
            for(long long i=1;i<=n;)
                {
                    long long d=k/i;
                    long long j=k/d;
                    if(j>n) j=n;
                    ans-=(j-i+1)*(i+j)/2*d;
                    i=j+1;
                }
            cout<<ans<<endl;
        }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值