现在的位置: 首页 > 综合 > 正文

hdu 1215

2018年04月21日 ⁄ 综合 ⁄ 共 795字 ⁄ 字号 评论关闭

如果使用下面的方法肯定会T,忘记考虑了外面还有一个循环

TLE的代码。
果断发现忘记考虑了
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
int main(){
    int T,x;
    scanf("%d",&T);
    while(T--){//当T = 500000,时间复杂度有多高就不用说了。
        scanf("%d",&x);
        LL sum = 0;
        for(int i = 1;i <= x / 2;i++){
            if(x % i == 0) sum += i;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}

后面改成先预处理了一下,抱着试试的态度,居然过了才62MS。。。。

AC代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
int const M = 500001;
LL sum[M];
void Count(){
    memset(sum,0,sizeof(sum));
    for(int i = 1;i <= M / 2;i++){//因为M的因子小于 M M最大的因子肯定就是M / 2(如果 M % 2 == 0 )所以直接 1 - M/2 找因子
        for(int j = i + i;j <= M - 1;j += i){//j % i == 0 i 是 j 的因子 
            sum[j] += i;
        }
    }
}
int main(){
    Count();
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        printf("%lld\n",sum[n]);
    }
    return 0;
}

后面看了其他人的代码,发现他们都是sqrt(n) 这个实在不明白为什么这样做。。求解释一下 

Hdu long long 输出数lld - -擦。。WA了好几发,,还以为是Count的问题

【上篇】
【下篇】

抱歉!评论已关闭.