不是怎么又是你
2024-03-03 21:48:18
发布于:广东
19阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int cube, i, j, k;
}ans[100005];
int ct, n;
bool cmp(node a, node b){
if(a.cube == b.cube){
if(a.k == b.k){
if(a.j == b.j) return a.i < b.i;
return a.j < b.j;
}return a.k < b.k;
}
return a.cube < b.cube;
}int pow3(int x){
return x * x * x;
}
int sqrt3(int x){
int left = 1, right = n;
while(left < right){
int mid = (left + right) / 2;
if(pow3(mid) < x) left = mid + 1;
else right = mid;
}return left;
}
int main(){
cin >> n;
for(int i = 2; i <= n; i++){
for(int j = 2; j <= i; j++){
int l = sqrt3(pow3(i) + pow3(j)), r = sqrt3(pow3(i) + pow3(j) * 2);
for(int _ = l; _ <= r; _++){
int k = pow3(_) - pow3(i) - pow3(j);
int left = 1, right = n;
while(left <= right){
int mid = (left + right) / 2;
if(pow3(mid) == k){
if(mid <= j && _ <= n && mid >= 2) ans[++ct] = {_, i, j, mid};
break;
}
if(pow3(mid) > k) right = mid - 1;
else left = mid + 1;
}
}
}
}sort(ans + 1, ans + ct + 1, cmp);
for(int i = 1; i <= ct; i++) printf("Cube = %d, Triple = (%d,%d,%d)\n", ans[i].cube, ans[i].k , ans[i].j, ans[i].i);
return 0;
}
这里空空如也
有帮助,赞一个