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. 《剑指Offer》算法题——二维数组查找

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. class Solutio ...

  2. js函数大全(2)

    原文地址:http://phperbar.blog.163.com/blog/static/162596182201032935815391/ 1.常规函数 javascript常规函数包括以下9个函 ...

  3. selenium高级用法

    http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp# WebDriver: Advanced Usage Explicit and Im ...

  4. SQL Server不能通过外部IP访问,解决方法

    SQL Server不能通过外部IP访问,解决方法   版本:SQL server 2008 express with tools   打开配置管理器,开启 TCP,右键属性设置TCP端口:   设置 ...

  5. dfs Codeforces Round #356 (Div. 2) D

    http://codeforces.com/contest/680/problem/D 题目大意:给你一个大小为X的空间(X<=m),在该空间内,我们要尽量的放一个体积为a*a*a的立方体,且每 ...

  6. window窗体程序意外崩溃,EventType clr20r3错误的解决方法

    EventType clr20r3, P1 ggreadcard.exe, P2 1.0.0.0, P3 51d3d283, P4 zljy.common, P5 1.0.0.0, P6 4fc6c2 ...

  7. oracle中的turnc,round,floor,ceil,coalesce函数

    这四个函数有点类似java中的函数,首先是 trunc(number,[decimals]) 这个函数类似截取函数 number:表示你要输入的数 decimals(小数): 表示你要截取的位数[正数 ...

  8. OpenGL中shader读取实现

    1.需要shader在OpenGL中工作,必须经过如下过程 2.代码实现 /********** * loadshader.h **********/ #pragma once #define _CR ...

  9. OpenGL------在Windows系统中显示文字

    增加了两个文件,showline.c, showtext.c.分别为第二个和第三个示例程序的main函数相关部分.在ctbuf.h和textarea.h最开头部分增加了一句#include <s ...

  10. 查找mysql数据库中所有包含特定名字的字段所在的表

    整个数据库查找 placement 字段: select * from INFORMATION_SCHEMA.columns where COLUMN_NAME Like '%placement%'; ...