跳到正文

题解

题解:P17736 [ICPC 2019 Xuzhou R] The Answer to the Ultimate Question of Life, The Universe

lmfandz约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年10月22日
收录
2026年10月23日
更新
2026年11月18日
篇幅
约 12 分钟
题目
P17736

版本

  1. v1当前

1.打表是个好东西

本题的 xx 非常小,只有 201201 种值,可以想到直接提前把 201201 个答案存起来。
预处理所有立方数对于每一个数,考虑枚举 aa 和 bb,hash判断 cc,时间复杂度 O(200×108)O(200\times10^8 ),想快点的话还可以分多个程序一起跑。
二次打表注意到 00 到 50005000 的立方模 50015001 后没有冲突,hash模数可以选 50015001 ,无O2运行只需约1.5分钟,开O2后只需约15秒。

打表程序
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MX=5000,MN=-5000,P=5001;//hash模数为5001时无hash冲突
int mp[P];//哈希表
int check(int a,int b,int x){
	x-=a*a*a+b*b*b;
	int f=1;
	if(x<0) f=-1,x=-x;
	if(mp[x%P]*mp[x%P]*mp[x%P]==x) return mp[x%P]*f;
	return LLONG_MIN;
}
signed main(){
	freopen("tmp.txt","w",stdout);
	for(int i=0;i<P;++i) mp[i]=MN-1;
	for(int i=0;i<=MX;++i){
		mp[i*i*i%P]=i;
	}
	for(int i=0;i<=200;++i){
		bool flag=0;
		for(int j=MN;j<=MX;++j){
			for(int k=j;k<=MX;++k){
				int x=check(j,k,i);
				if(x!=LLONG_MIN){
					printf("\"%lld %lld %lld\",\n",j,k,x);
					flag=1;
					break;
				}
			}
			if(flag) break;
		}
		if(flag==0) printf("\"impossible\",\n");
	}
}
最终程序
C++
#include<bits/stdc++.h>
using namespace std;
string s[201]={
  "0 0 0",
  "-5000 1 5000",
  "-4373 -486 4375",
  "-5 4 4",
  "impossible",
  "impossible",
  "-637 -205 644",
  "-169 44 168",
  "-5000 2 5000",
  "-216 -52 217",
  "-650 -353 683",
  "-695 -641 843",
  "-11 7 10",
  "impossible",
  "impossible",
  "-265 -262 332",
  "-4114 -588 4118",
  "-3331 2195 2977",
  "-1373 -1276 1671",
  "-95 47 91",
  "-2816 -741 2833",
  "-401 -287 445",
  "impossible",
  "impossible",
  "-10 8 8",
  "-2683 1839 2357",
  "-2107 237 2106",
  "-5000 3 5000",
  "-2268 -249 2269",
  "-233 -69 235",
  "impossible",
  "impossible",
  "impossible",
  "impossible",
  "-1555 -244 1557",
  "-1120 -509 1154",
  "-3223 2358 2731",
  "-444 -84 445",
  "-27 16 25",
  "impcssible",
  "impossible",
  "impdssible",
  "impessible",
  "-823 -307 837",
  "-7 -5 8",
  "-2369 1709 2025",
  "-758 -473 815",
  "-141 49 139",
  "-3950 -1247 3991",
  "imbyssible",
  "lmfandzble",
  "-796 602 659",
  "impbssible",
  "-2370 1518 2141",
  "-3885 -648 3891",
  "-3329 1837 3131",
  "-672 505 559",
  "-998 361 982",
  "impossible",
  "impossible",
  "-1201 -163 1202",
  "-966 668 845",
  "-2744 -1561 2903",
  "-161 102 146",
  "-5000 4 5000",
  "-929 403 903",
  "1 1 4",
  "impossible",
  "impossible",
  "-403 134 398",
  "-2359 824 2325",
  "-533 401 443",
  "-432 -104 434",
  "-335 -146 344",
  "impossible",
  "impossible",
  "impossible",
  "impossible",
  "-2080 -829 2123",
  "-706 -196 711",
  "-1300 -706 1366",
  "-2368 -1719 2638",
  "-1317 847 1188",
  "-3707 1315 3651",
  "impossible",
  "impossible",
  "impossible",
  "-4126 -1972 4271",
  "-1390 -1282 1686",
  "-2514 1953 2036",
  "-1803 365 1798",
  "-3389 -2912 3992",
  "-4052 861 4039",
  "-248 -98 253",
  "impossible",
  "impossible",
  "-22 14 20",
  "-3168 -991 3200",
  "-2101 -1638 2391",
  "-893 -622 984",
  "-1797 -903 1870",
  "-2327 319 2325",
  "-239 118 229",
  "impossible",
  "impossible",
  "-7 -4 8",
  "-2689 -1165 2760",
  "-1309 947 1117",
  "-1165 -948 1345",
  "-2948 853 2924",
  "impossible",
  "-4793 -2312 4966",
  "impossible",
  "impossible",
  "impossible",
  "-12 8 11",
  "-1906 -757 1945",
  "-896 -555 962",
  "-4328 383 4327",
  "-3677 -1673 3789",
  "-2804 1219 2725",
  "impossible",
  "impossible",
  "-37 -16 38",
  "-1 0 5",
  "-5000 5 5000",
  "-2212 -419 2217",
  "-4034 -3881 4988",
  "-3989 -726 3997",
  "-1580 -1238 1801",
  "impossible",
  "impossible",
  "-1 2 5",
  "-399 167 389",
  "-3013 -1766 3203",
  "-1351 -629 1395",
  "-1116 816 946",
  "-758 -428 801",
  "-86 -77 103",
  "impossible",
  "impossible",
  "-139 104 116",
  "-7 -3 8",
  "impossible",
  "-2746 -2552 3342",
  "-8 -7 10",
  "-327 -263 376",
  "-2366 1528 2131",
  "impossible",
  "impossible",
  "-367 260 317",
  "-463 215 447",
  "-805 486 741",
  "-3736 -695 3744",
  "-2135 -516 2145",
  "-3693 -1049 3721",
  "impossible",
  "impossible",
  "impossible",
  "-1534 383 1526",
  "-3874 -1654 3972",
  "-4767 -2476 4980",
  "-4125 -1417 4180",
  "-3423 -2943 4033",
  "-66 -59 79",
  "impossible",
  "impossible",
  "impossible",
  "-802 -574 890",
  "-1354 -1012 1521",
  "-3834 -2149 4047",
  "-1328 891 1178",
  "impossible",
  "impossible",
  "-335 -170 349",
  "impossible",
  "impossible",
  "-1168 -160 1169",
  "-13 -10 15",
  "-2839 1503 2691",
  "impossible",
  "-4874 974 4861",
  "-90 -29 91",
  "-4889 976 4876",
  "impossible",
  "impossible",
  "-4 5 5",
  "-1885 -1092 2000",
  "-1639 318 1635",
  "-1702 -1403 1974",
  "-4812 -593 4815",
  "-377 -215 399",
  "-20 16 16",
  "impossible",
  "impossible",
  "impossible",
  "-1057 -579 1112",
  "-2867 -1606 3026",
  "-3752 -1347 3809",
  "-2208 508 2199",
  "-2318 -638 2334"
};
signed main(){
	int T,x;
	cin>>T;
	while(T--){
		cin>>x;
		cout<<s[x]<<endl;
	}
}

抄题解是个坏习惯虽然这份代码藏了某些神秘的东西。

2.在线做法

我们发现暴力打表代码跑 201201 个数在开O2后只需不到 2020 秒,也就是说 1010 个查询的期望用时不到 11 秒,这启示我们对暴力打表代码进行卡常优化(其实只是加个inline和把部分longlong改为int)。

AC code
C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MX=5000,MN=-5000,P=5001;
int mp[P];
inline ll check(int a,int b,int x){
	ll y=x-1ll*a*a*a-1ll*b*b*b;
	int f=1;
	if(y<0){
        y=-y;
        int z=y%P;
        if(1ll*mp[z]*mp[z]*mp[z]==y) return -mp[z];
    }
	else{
        int z=y%P;
        if(1ll*mp[z]*mp[z]*mp[z]==y) return mp[z];
    }
	return LLONG_MIN;
}
signed main(){
	for(int i=0;i<P;++i) mp[i]=MN-1;
	for(int i=0;i<=MX;++i){
		mp[1ll*i*i*i%P]=i;
	}
	int T;cin>>T;
	while(T--){
		int i;
		cin>>i;
		bool flag=0;
		for(int j=MN;j<=MX;++j){
			for(int k=j;k<=MX;++k){
				ll x=check(j,k,i);
				if(x!=LLONG_MIN){
					printf("%d %d %lld\n",j,k,x);
					flag=1;
					break;
				}
			}
			if(flag) break;
		}
		if(flag==0) printf("impossible\n");
	}
}

AC记录

原文