码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3175 Number of Containers 分块加速

时间:2015-08-13 12:24:07      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

题意:计算n以内i的倍数的个数,不包括i本身,求和

解:直接暴力for一遍一定是不行的,不过用一个分块加速就可以了

#include <stdio.h>
#include <string.h>
int main()
{
    int t;
    long long n;
    while(scanf("%d",&t)!=-1)
    {
        while(t--)
        {
            scanf("%lld",&n);                  //要用lld错了N发的教训
            long long sum=0;
            for(int i=1,last;i<=n;i=last+1)
            {
                last=n/(n/i);                 //分块加速,所有n/i结果相同的一起计算
                sum+=((n/i-1)*(last-i+1));
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

zoj 3175 Number of Containers 分块加速

标签:

原文地址:http://blog.csdn.net/trq1995/article/details/47606557

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!