Divisor Summation_
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_的更多相关文章
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- Divisor counting [线性筛积性函数]
Divisor counting 题目大意:定义f(n)表示整数n的约数个数.给出正整数n,求f(1)+f(2)+...+f(n)的值. 注释:1<=n<=1000,000 想法:我们再次 ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- Divisor Subtraction
Description You are given an integer number nn. The following algorithm is applied to it: if n=0, th ...
- 845. Greatest Common Divisor
描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...
- SPOJ 74. Divisor Summation 分解数字的因子
本题有两个难点: 1 大量的数据输入.没处理好就超时 - 这里使用buffer解决 2 因子分解的算法 a)暴力法超时 b)使用sieve(筛子),只是当中的算法逻辑也挺不easy搞对的. 数值N因子 ...
- codeforces#505--B Weakened Common Divisor
B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...
- hdu 5207 Greatest Greatest Common Divisor 数学
Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...
随机推荐
- ASP.NET MVC 使用带有短横线的html Attributes
我们常常需要一个文本框来输入用户名,在asp.net mvc 中可以使用 TextBoxFor.有时候我们想为这个文本框添加一些特性,比如 html5 的data-,如果直接写data-是会提示错误的 ...
- linux时间同步,ntpd、ntpdate
linux时间同步,ntpd.ntpdate 在Windwos中,系统时间的设置很简单,界面操作,通俗易懂.而且设置后,重启,关机都没关系.系统时间会自动保存在Bios的时钟里面,启动计算机的时候,系 ...
- jsp的九大天王
JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1. ...
- divide an integer into X parts (as even as possible)
the algorithm is like this: it evenly spreads an integer N over K cells. for i = 0 to K array[i] = N ...
- ueditor浏览器 无法上传文件.问题
dll也都引用了 路径绝对tmd没问题 最后 我一点一点的调试发现了问题 草tmd百度程序员 */UE.ajax = function() { //创建一个ajaxRequest对象 var fnSt ...
- MySQL主从配置【转载】
1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3.修改root的密码 2.修改主服务器master: #vi /etc/my.cnf ...
- BIND rndc—使用说明
rndc—使用说明 rndc设置(本地) 产生/etc/rndc.key 执行”rndc-confgen -a”指令后,会在/etc目录下产生rndc.key文件, 而所产生的文 ...
- MySQL出现Errcode:28错误提示解决办法
mysql出现Error writing file \'xxx\'( Errcode:28)的原因有很多种,下面我来总结一些常用的关于引起Errcode:28错误原因与解决方法. 问题一,是log ...
- STM32F4系统时钟配置及描述
STM32F4系统时钟配置及描述 stm32f407时钟配置方法(感觉很好,分享一下) STM32F4_RCC系统时钟配置及描述 STM32F4时钟设置分析 stm32f4 - 时钟树分析配置
- asp之Eval()函数
运行字符串: a=Eval("1+1") 结果为:a=2 运行函数: function aa(a) aa=a+1 end function b=Eval("aa(2)&q ...