zoukankan      html  css  js  c++  java
  • 欧拉计划第12题题解

    Highly divisible triangular number

    The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
    Let us list the factors of the first seven triangle numbers:

    • 1: 1
    • 3: 1,3
    • 6: 1,2,3,6
    • 10: 1,2,5,10
    • 15: 1,3,5,15
    • 21: 1,3,7,21
    • 28: 1,2,4,7,14,28

    We can see that 28 is the first triangle number to have over five divisors.

    What is the value of the first triangle number to have over five hundred divisors?

    高度可约的三角形数

    三角形数数列是通过逐个加上自然数来生成的。例如,第7个三角形数是 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28。三角形数数列的前十项分别是:

    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
    让我们列举出前七个三角形数的所有约数:

    • 1: 1
    • 3: 1,3
    • 6: 1,2,3,6
    • 10: 1,2,5,10
    • 15: 1,3,5,15
    • 21: 1,3,7,21
    • 28: 1,2,4,7,14,28

    我们可以看出,28是第一个拥有超过5个约数的三角形数。

    第一个拥有超过500个约数的三角形数是多少?

    解题思路

    (1) 开始枚举每一个数,判断它有多少个约数。

    判断约数存在 (O( sqrt{n} )) 时间复杂度的算法:

    我们可以从 (1)(lfloor sqrt{n} floor) 去枚举每一个数 (i),如果 (i) 能整除 (n),则 (frac{n}{i}) 也能够整除 (n)

    实现代码如下(其中 cal(n) 用于返回n的约数个数):

    #include <bits/stdc++.h>
    using namespace std;
    int cal(long long n) {
        int cnt = 0;
        for (long long i = 1; i*i <= n; i ++) {
            if (n % i == 0) {
                if (i*i == n) cnt ++;
                else cnt += 2;
            }
        }
        return cnt;
    }
    int main() {
        int n = 0;
        for (long long i = 1; ; i ++) {
            n += i;
            if (cal(n) > 500) {
                cout << n << endl;
                break;
            }
        }
        return 0;
    }
    

    得到答案为 (76576500)

  • 相关阅读:
    ORA28056:Writing audit records to Windows EventLog failed
    oracle odp.net
    C#共享内存
    JQuery EasyUI Tree和tab右键菜单实现
    Query EasyUI Tree树使用介绍
    UML类图
    Oracle 创建表空间和用户
    Oracle 11g ORA12514:TNS:监听程序当前无法识别连接描述符中请求的服务 .
    Client使用c#和odp.net连接server oracle
    WPF 4文字模糊不清晰解决方法
  • 原文地址:https://www.cnblogs.com/quanjun/p/12323196.html
Copyright © 2011-2022 走看看