UVA962 Taxicab Numbers【数】

The famous mathematician Hardy relates the following episode with the (now also famous) Indian mathematician Ramanujan:

I remember once going to see him when he was ill at Putney. I had ridden in taxi cab number 1729 and remarked that the number seemed to me rather a dull one, and that I hoped it was not an unfavorable omen. “No,” he replied, “it is a very interesting number; it is the smallest number expressible as the sum of two positive cubes in two different ways.”

Your objective is to print cab numbers in a given range, [n1; n1 + rg], specified by its lower limit, n1, and the size of the interval, rg. A number is a cab number if it can be expressed as the sum of two positive cubes, in at least two different ways.
Input
Input contains several test cases. For each test case, you are given two numbers in two rows, the lower limit n1 and the range we are interested in, rg. The lower limit is between 1 and 1000000000 (1E+9). The range is between 1 and 100000. EOF indicates the end of the input.
Output
For each test case, you should output a list of cab numbers, in the specified range. The numbers should be separated by newlines. If there is no cab number in the range, you should output one single line with the word ‘None’.
Sample Input
1000
20000
Sample Output
1729
4104
13832
20683

问题链接UVA962 Taxicab Numbers
问题简述:(略)
问题分析
    数学中数的问题,不解释。
    打表是必要的。
程序说明
    程序代码中用到了一些C++语言程序的高级语法特征,看for语句。这些特征的用法简化的代码的书写。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA962 Taxicab Numbers */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 1e9 + 1e5;
map<LL, int> m;
vector<int> v;

void init()
{
    for(LL i = 1; i * i * i < N; i++)
        for(LL j = i; ; j++) {
            LL tmp = i * i * i + j * j * j;
            if(tmp <= N) m[tmp]++;
            else break;
        }

    for(auto &x : m)
        if(x.second > 1) v.push_back(x.first);
}

int main()
{
    init();

    int a, b;
    while(~scanf("%d%d", &a, &b)) {
        b += a;
        bool flag = true;
        for(auto &x : v) {
            if(x > b) break;
            if(x >= a) {
                flag = false;
                printf("%d\n", x);
            }
        }
        if(flag) printf("None\n");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/89427632