-
Notifications
You must be signed in to change notification settings - Fork 160
/
Specialnumber.cpp
51 lines (33 loc) · 994 Bytes
/
Specialnumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Find all numbers less than N, such that n = a^3 + b^3 = c^3 + d^3, where a, b, c, d are non negative numbers.
*/
/*
solution: store the qubic of all factors between 1 to n^1/3. Iterate i, j, suppose the sum of their qubic is k,
record the time of k occurance in an arr, if arr[k] == 2, output k.
O(n^2/3) time, O(n) space
*/
#include<iostream>
#include<vector>
using namespace std;
void TaxiCub(int n) {
int limit = n;
vector<int> factor(n+1,0);
vector<int> visited(n+1,0);
for (int i = 1 ;i * i * i <= n; i++) {
factor[i] = i*i*i;
}
for (int i = 1 ; i * i *i <= n;i++) {
for (int j = i; j * j * j <= n;j++){
int sum = factor[i] + factor[j];
if (sum > limit) continue;
if ( visited[sum] < 2 ) visited[sum]++;
if (visited[sum] == 2) {
cout<<sum<<endl;
}
}
}
}
int main(){
TaxiCub(10000);
return 0;
}