Divisor Summation

Problem Description

Give a natural number n (1 <= n <= 500000), please tell the summation of all its proper divisors. Definition: A proper divisor of a natural number is the divisor that is strictly less than the number.

e.g. number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.

Input

An integer stating the number of test cases, and that many lines follow each containing one integer between 1 and 500000.

Output

One integer each line: the divisor summation of the integer given respectively.

Sample Input

3

2

10

20

Sample Output

1

8

22

参考代码

int sum[500001];
int main()
{
int n, a; sum[1] = 0;
for (int k = 2; k <= 500000; k++)
sum[k] = 1;
for (int i = 2; i <=250000; i++)
{
for (int j = 2; j <= 500000/i; j++)
sum[j*i] += i;
}
//freopen("d:\\out.txt", "w", stdout);
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n;i++)//坑啊 改了两次竟然没发现少写了个循环 一定要细心
{
scanf("%d", &a); printf("%d\n", sum[a]);
} }
//system("pause");
}

如果用一般的暴力解法的话会超时,用打表法反而更快

Divisor Summation_的更多相关文章

  1. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  2. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  3. Divisor counting [线性筛积性函数]

    Divisor counting 题目大意:定义f(n)表示整数n的约数个数.给出正整数n,求f(1)+f(2)+...+f(n)的值. 注释:1<=n<=1000,000 想法:我们再次 ...

  4. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

  5. Divisor Subtraction

    Description You are given an integer number nn. The following algorithm is applied to it: if n=0, th ...

  6. 845. Greatest Common Divisor

    描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...

  7. SPOJ 74. Divisor Summation 分解数字的因子

    本题有两个难点: 1 大量的数据输入.没处理好就超时 - 这里使用buffer解决 2 因子分解的算法 a)暴力法超时 b)使用sieve(筛子),只是当中的算法逻辑也挺不easy搞对的. 数值N因子 ...

  8. codeforces#505--B Weakened Common Divisor

    B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...

  9. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

随机推荐

  1. 关于C++数组的几点讨论

    数组名为何物? int main() { , , , , }; int *pnumber = number; cout << sizeof(number) << endl; c ...

  2. VBS脚本合集(自制脚本)

    一.视频搜索器选择: Selectitem() '######################################### Function  Selectitem() 'Set Vedio ...

  3. OpenCV中Mat的列向量归一化

    OpenCV中Mat的列向量归一化 http://blog.csdn.net/shaoxiaohu1/article/details/8287528 OpenCV中Mat的列向量归一化 标签: Ope ...

  4. java中static关键字解析

    static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...

  5. hdu_1403_Longest Common Substring(后缀数组的应用)

    题目链接:hdu_1403_Longest Common Substring 题意: 给你两个字符串,然你找最长的公共子串 题解: 后缀数组的经典应用,要找两个字符串的公共子串,那么就相当于找两个串的 ...

  6. Floodfill算法——求独立子图个数

    Floodfill--漫水填充法(也称种子填充法)简单来说就是求一个图中独立子图的个数并将其描述出不同的状态.Floodfill在计算机图形学有着非常广泛的运用,比如图像分割.物体识别之类.基于Flo ...

  7. masonry使用问题

    2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...

  8. Mycat 依赖包解读

    1.curator - zookeeper开源客户端框架 2.dom4j - xml解析包 3.druid - 阿里巴巴推出的国产数据库连接池,同时具备监控功能,性能优于JDBC和C3P0 4.ehc ...

  9. zf-删除重复数据只保留一条(转)

    http://blog.csdn.net/anya/article/details/6407280

  10. PID控制学习笔记(二)

    不管是基本的PID控制还是变形的PID控制算法,其核心都是对输入信号(设定值信号.测量信号或者偏差信号等)做基本的比例.积分.微分运算,最终提供给被控过程良好的调节信号. 在过程控制仪表,特别是在数字 ...