牛客网NOIP赛前集训营-普及组(第六场)A题金币

题目链接:https://www.nowcoder.com/acm/contest/170/A
开始想用平方和公式,二分确定r 和l所在的N,再用立方和公式求和。结果二分没处理好,死循环。
参考大佬的代码,发现for就行了 ,不会超时。一般什么数据一遍for 都不会超时(哭)

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
    long long sum,i,l,r,j;
    scanf("%lld%lld",&l,&r);
    sum=0;
    for(i=1,j=1;j<=r;i++,j+=i*i)
    {
        if(j>=l)
        {
            sum+=(j-l+1)*i;
            l=j+1;
        }
    }
    sum+=(r-l+1)*i;
    printf("%lld\n",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiaoshazheng/article/details/83243624
今日推荐