[UVA] 962 - Taxicab Numbers
Problem
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, [; +], specified by its lower limit, , and the size of the interval, . 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 and the range we are interested in, . 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
題目描述:
找到區間內所有可以用兩種不同方式(i*i*i + j*j*j) 組成 N。
總之建表
#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int i, j, k;
map<int, int> R;
for(i = 0; i*i*i <= 1000000000; i++) {
for(j = i; ; j++) {
k = i*i*i + j*j*j;
if(k > 1000000000) break;
R[k]++;
}
}
vector<int> V;
for(map<int, int>::iterator it = R.begin();
it != R.end(); it++) {
if(it->second > 1)
V.push_back(it->first);
}
int l, r;
while(scanf("%d %d", &l, &r) == 2) {
r += l;
int x = lower_bound(V.begin(), V.end(), l) - V.begin();
int flag = 0;
for(; x < V.size(); x++) {
if(V[x] > r) break;
printf("%d\n", V[x]), flag = 1;
}
if(!flag)
puts("None");
}
return 0;
}