题目
a ^ 3 = b ^ 3 + c ^ 3 + d ^ 3
求有多少a, b, c, d满足a ^ 3 = b ^ 3 + c ^ 3 + d ^ 3,其中a, b, c, d满足1 <= a, b, c, d <= n
输入格式:
一个正整数n,含义见题目描述
n <= 1000
输出格式:
一个正整数表示答案
限制:
空间限制:128MByte
时间限制:1秒
样例:
输入:
10
输出:
12
O(n^4):
#include<bits/stdc++.h>
using namespace std;
int main()
{
int ans = 0;
int a,b,c,d;
for(a=6;a<201;a++)
{
for(b=2;b<a;b++)
{
for(c=b;c<a;c++)
{
for(d=c;d<a;d++)
{
if(a*a*a==b*b*b+c*c*c+d*d*d)
{
ans++;
}
}
}
}
}
cout << ans;
return 0;
}
O(n ^ 2logn):
#include <iostream>
#include <map>
using namespace std;
const int N = 1e3 + 100;
int pow3[N];
map <int,int> vis;
int main()
{
int n;
cin >> n;
int ans = 0;
for(int i = 1; i <= n; i++)
{
pow3[i] = i * i * i;
}
for(int a = 1; a <= n; a++)
{
for(int b = 1; b <= a; b++)
{
vis[pow3[a] - pow3[b]]++;
}
}
for(int c = 1; c <= n; c++)
{
for(int d = 1; d <= n; d++)
{
if(vis[pow3[c] + pow3[d]])
{
ans += vis[pow3[c] + pow3[d]];
}
}
}
cout << ans << endl;
return 0;
}
O(n ^ 2):
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
const int N = 1e6 + 100;
int pow3[1100];
int val1[N], cnt1;
int val2[N], cnt2;
int main()
{
int n;
cin >> n;
int ans = 0;
for(int w = 1; w <= n; w++)
{
pow3[w] = w * w * w;
}
for(int a = 1; a <= n; a++)
{
for(int b = 1; b <= a; b++)
{
val1[cnt1 ++] = pow3[a] - pow3[b];
}
}
for(int c = 1; c <= n; c++)
{
for(int d = 1; d <= n; d++)
{
val2[cnt2 ++] = pow3[c] + pow3[d];
}
}
sort(val1, val1 + cnt1);
sort(val2 , val2 + cnt2);
val1[cnt1] = -1;
val2[cnt2] = -1;
int i = 0, j = 0;
while(i < cnt1 && j < cnt2)
{
if(val1[i] == val2[j])
{
int a = 1, b = 1;
while(val1[i + 1] == val1[i])
{
i++;
a++;
}
while(val2[j + 1] == val2[j])
{
j++;
b++;
}
i++, j++;
ans += a * b;
}
else if (val1[i] < val2[j])
{
i++;
}
else
{
j++;
}
}
cout << ans << endl;
return 0;
}
最暴力:
#include <iostream>
using namespace std;
long long n[10010] =
{
0,
0,
0,
0,
0,
6,
6,
6,
12,
12,
12,
18,
18,
18,
18,
18,
18,
30,
36,
42,
42,
42,
42,
48,
54,
54,
60,
66,
72,
78,
78,
78,
78,
78,
78,
90,
90,
96,
96,
102,
114,
120,
120,
126,
132,
144,
144,
150,
150,
156,
156,
156,
162,
180,
180,
186,
192,
204,
204,
216,
216,
216,
222,
222,
222,
228,
234,
234,
240,
246,
252,
270,
270,
270,
282,
294,
294,
300,
300,
306,
318,
336,
336,
354,
360,
360,
384,
402,
408,
432,
432,
444,
450,
450,
456,
468,
474,
474,
480,
492,
492,
498,
504,
504,
510,
516,
516,
546,
546,
552,
558,
564,
570,
582,
588,
606,
612,
612,
612,
630,
636,
642,
660,
660,
666,
684,
690,
690,
696,
696,
696,
714,
720,
732,
738,
738,
744,
768,
774,
792,
798,
810,
810,
834,
846,
846,
852,
852,
852,
876,
882,
894,
900,
900,
900,
912,
912,
912,
924,
936,
936,
966,
966,
990,
990,
990,
996,
1014,
1014,
1026,
1050,
1056,
1056,
1098,
1104,
1128,
1134,
1146,
1152,
1182,
1188,
1188,
1188,
1200,
1212,
1230,
1236,
1242,
1254,
1266,
1272,
1290,
1296,
1302,
1302,
1308,
1314,
1338,
1344,
1356,
1368,
1380,
1392,
1398,
1416,
1434,
1446,
1446,
1464,
1488,
1488,
1494,
1506,
1518,
1518,
1554,
1560,
1572,
1596,
1614,
1614,
1626,
1626,
1632,
1656,
1662,
1662,
1686,
1698,
1716,
1734,
1758,
1758,
1776,
1794,
1794,
1794,
1800,
1812,
1830,
1836,
1842,
1854,
1866,
1866,
1902,
1908,
1908,
1914,
1920,
1926,
1956,
1956,
1968,
1974,
1992,
1992,
2010,
2010,
2022,
2052,
2058,
2058,
2088,
2094,
2106,
2112,
2124,
2136,
2172,
2172,
2172,
2172,
2178,
2190,
2214,
2214,
2220,
2244,
2262,
2268,
2286,
2286,
2304,
2310,
2310,
2328,
2364,
2370,
2394,
2400,
2418,
2430,
2442,
2448,
2448,
2460,
2466,
2466,
2496,
2496,
2520,
2532,
2544,
2550,
2562,
2568,
2580,
2604,
2604,
2610,
2622,
2622,
2622,
2634,
2634,
2646,
2664,
2670,
2682,
2694,
2706,
2718,
2778,
2784,
2790,
2808,
2832,
2832,
2850,
2856,
2862,
2874,
2886,
2898,
2922,
2922,
2922,
2928,
2952,
2952,
2982,
2982,
2994,
3006,
3012,
3012,
3060,
3066,
3084,
3096,
3132,
3132,
3156,
3168,
3198,
3198,
3204,
3204,
3246,
3258,
3270,
3282,
3294,
3300,
3312,
3312,
3336,
3372,
3384,
3390,
3420,
3426,
3438,
3450,
3456,
3462,
3504,
3510,
3534,
3546,
3558,
3564,
3600,
3606,
3618,
3630,
3648,
3654,
3660,
3672,
3684,
3690,
3708,
3714,
3768,
3768,
3774,
3780,
3804,
3804,
3834,
3834,
3846,
3858,
3888,
3894,
3924,
3924,
3954,
3966,
3990,
3990,
4020,
4026,
4026,
4044,
4062,
4074,
4116,
4116,
4122,
4140,
4146,
4170,
4206,
4206,
4224,
4224,
4230,
4242,
4296,
4296,
4302,
4338,
4356,
4368,
4404,
4404,
4440,
4452,
4452,
4452,
4464,
4476,
4482,
4494,
4500,
4500,
4548,
4560,
4566,
4596,
4614,
4614,
4638,
4638,
4656,
4680,
4710,
4716,
4740,
4740,
4770,
4788,
4788,
4794,
4818,
4824,
4842,
4854,
4866,
4866,
4872,
4896,
4914,
4938,
4962,
4980,
5028,
5028,
5040,
5046,
5064,
5076,
5106,
5112,
5130,
5142,
5148,
5154,
5232,
5250,
5262,
5274,
5280,
5286,
5304,
5310,
5334,
5340,
5346,
5358,
5394,
5412,
5430,
5436,
5460,
5466,
5484,
5484,
5502,
5538,
5544,
5562,
5598,
5622,
5622,
5634,
5646,
5652,
5700,
5706,
5712,
5730,
5742,
5748,
5790,
5796,
5802,
5832,
5856,
5868,
5886,
5892,
5916,
5928,
5946,
5946,
6006,
6018,
6018,
6024,
6024,
6030,
6036,
6054,
6060,
6066,
6084,
6096,
6132,
6132,
6138,
6156,
6162,
6168,
6210,
6210,
6228,
6252,
6264,
6264,
6294,
6318,
6318,
6342,
6360,
6366,
6390,
6390,
6396,
6420,
6444,
6468,
6516,
6552,
6558,
6570,
6606,
6606,
6618,
6630,
6648,
6654,
6678,
6690,
6720,
6726,
6738,
6750,
6750,
6750,
6786,
6792,
6804,
6822,
6834,
6840,
6882,
6888,
6900,
6918,
6948,
6960,
7020,
7020,
7032,
7062,
7086,
7086,
7104,
7116,
7134,
7158,
7182,
7188,
7236,
7242,
7254,
7284,
7290,
7296,
7314,
7332,
7332,
7350,
7350,
7356,
7404,
7410,
7416,
7440,
7458,
7470,
7494,
7494,
7512,
7536,
7560,
7560,
7602,
7608,
7638,
7644,
7656,
7668,
7746,
7752,
7758,
7770,
7782,
7800,
7842,
7848,
7878,
7908,
7908,
7908,
7956,
7962,
7980,
7986,
8004,
8022,
8040,
8064,
8100,
8106,
8148,
8154,
8190,
8196,
8208,
8238,
8244,
8250,
8274,
8286,
8316,
8316,
8322,
8322,
8370,
8394,
8400,
8412,
8424,
8430,
8466,
8478,
8484,
8514,
8514,
8520,
8586,
8598,
8616,
8622,
8658,
8664,
8694,
8712,
8754,
8784,
8784,
8790,
8820,
8826,
8850,
8862,
8898,
8910,
8922,
8922,
8946,
8970,
8982,
8982,
9036,
9054,
9078,
9090,
9114,
9132,
9150,
9150,
9162,
9192,
9198,
9210,
9240,
9252,
9252,
9270,
9294,
9300,
9360,
9366,
9384,
9390,
9402,
9402,
9444,
9456,
9468,
9492,
9510,
9510,
9540,
9546,
9552,
9558,
9570,
9588,
9654,
9660,
9678,
9684,
9714,
9720,
9750,
9750,
9768,
9792,
9804,
9804,
9858,
9864,
9882,
9888,
9900,
9906,
9936,
9942,
9960,
9966,
9990,
10014,
10044,
10050,
10080,
10116,
10134,
10134,
10164,
10170,
10188,
10188,
10194,
10200,
10278,
10284,
10296,
10320,
10332,
10338,
10362,
10368,
10404,
10434,
10452,
10458,
10494,
10506,
10506,
10524,
10536,
10536,
10590,
10590,
10632,
10650,
10656,
10674,
10704,
10716,
10716,
10728,
10782,
10788,
10818,
10818,
10854,
10884,
10884,
10890,
10938,
10944,
10968,
10986,
10986,
10992,
11028,
11046,
11076,
11100,
11136,
11136,
11190,
11196,
11202,
11220,
11226,
11226,
11286,
11292,
11304,
11310,
11340,
11346,
11406,
11412,
11424,
11454,
11472,
11472,
11478,
11484,
11502,
11526,
11550,
11562,
11634,
11640,
11646,
11664,
11676,
11682,
11760,
11766,
11790,
11802,
11826,
11832,
11886,
11904,
11904,
11928,
11970,
11970,
12000,
12000,
12000,
12030,
12036,
12042,
12054,
12060,
12090,
12120,
12132,
12150,
12186,
12198,
12216,
12228,
12240,
12258,
12318,
12324,
12348,
12360,
12372,
12378,
12444,
12456,
12480,
12510,
12522,
12534,
12558,
12558,
12558,
12582,
12618,
12618,
12660,
12672,
12702,
12720,
12732,
12738,
12786,
12804,
12816,
12858,
12888,
12894,
12930,
12942,
12948,
12978,
12984,
13002,
13032,
13050,
13062,
13068,
13104,
13104,
13128,
13146,
13170,
13188,
13194,
13194,
13206,
13206,
13242,
13260,
13290,
13290,
13326,
13332,
13356,
13392,
13410,
13422,
13476,
13482,
13482,
13500,
13530,
13536,
13572,
13578,
13626,
13656,
13680,
13686,
13752,
13764,
13770,
13794,
13818,
13836,
13878,
13902,
13920,
13944,
13950,
13962,
14052,
14070,
14100,
14112,
14130,
14130,
14196,
14202,
14220,
14232,
14244,
14256,
14286,
14286,
14292,
14304,
14346
};
int main()
{
int m;
cin >> m;
cout << n[m - 1];
return 0;
}