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/ ...
随机推荐
- 如何复原Eclipse的Package Explorer
重新设置,复位透视图吧这样的方法最简便window--->reset perspective 你会喜欢这个操作的
- android 瀑布流效果(仿蘑菇街)
我们还是来看一款示例:(蘑菇街) 看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此.就如我们的方角图形,斯通见惯后也就出 ...
- 如何使用 AngularJS 的 ngShow 和 ngHide
今天我们来看看怎样使用Angular的ngShow 和ngHide 指令来完成它们听起来应该完成的,显示和隐藏! 它们应该做的事 ngShow 和ngHide 允许我们显示或隐藏不同的元素.这有助于创 ...
- C#中的Virtual、Override和new关键词理解
来源:http://blog.csdn.net/jackiezhw/article/details/2673992 在 C# 中,派生类可以包含与基类方法同名的方法. 基类方法必须定义为 virtua ...
- 登录验证的js;JS验证邮箱 验证密码
var auth_email = 0, auth_pwd = 0, auth_captcha = 0, url_ajax = "/protected/user_ajax.php"; ...
- 扩展方法之ToDictionary()
Person类: public class Person { public int Id { set; get; } public string WorkNo { set; get; } public ...
- UVa11235 RMQ
input 1<=n,q<=100000 升序序列a1 a2 a3 ... an -100000<=ai<=100000 q行i j 1<=i,j<=n 输入结束标 ...
- MP3拷贝程序---字节流
package cn.itcast.p7.io.bytestream.test; import java.io.BufferedInputStream; import java.io.Buffered ...
- HDU 1072/BFS
题目链接 Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- mysql char varchar 区别
数据库建表 字符串字段类型的选择 char与varchar的区别: char 长度固定,char(M)类型的数据列里,每个值都占用M个字节,如果某个长度小于M,MySQL就会在它的右边用空 ...