直接打表。
#include <cstdio> #define MAX 500000 + 2 int a[MAX]; void count(){ int t = MAX / 2, i, j; for(i = 1; i != t; ++i) for(j = i * 2; j <= MAX; j += i) a[j] += i; } int main(){ int t, n; count(); scanf("%d", &t); while(t-- && scanf("%d", &n)) printf("%d\n", a[n]); return 0; }
原文:http://blog.csdn.net/chang_mu/article/details/19701517