zoukankan      html  css  js  c++  java
  • 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
    ", sum[a]);
    	}
    	
    }
    //system("pause");
    }
    

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

  • 相关阅读:
    多线程02
    多线程01
    CSS
    Mybatis CRUD中万能Map的用法及优势
    Tomcat配置
    Node.js+Vue+Webpack
    Java的几种常见排序算法
    maven插件 mybatis逆向工程
    ssm依赖
    mybatis spring整合依赖配置
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/5196374.html
Copyright © 2011-2022 走看看