Sum of Cubes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 815 Accepted Submission(s): 182
Problem Description
According to Goldbach’s conjecture, every even number can be expressed as a sum of two odd primes. Which numbers can be expressed as the sum of two cubes?


Input
For each test case, a line will contain a positive integer n which will be at most one million.
The last line will contain the integer 0, which should not be processed.
The last line will contain the integer 0, which should not be processed.
Output
For each test case, output two integers x and y such that x3 + y3 = n. If there are many such pairs of numbers, choose x to be as small as possible. If there are no such pairs, output “Impossible”.
Sample Input
1
2
3
1000000
0
Sample Output
0 1
1 1
Impossible
0 100
1: #include<iostream>2: using namespace std;
3: const int maxn=1000000,inf=10000;
4: struct Cube{
5: int x,y;
6: }; 7: Cube cube[maxn+1];8: int flag[maxn+1];
9: int main(){
10: //freopen("in.txt","r",stdin);
11: memset(flag,0,sizeof(flag));
12: for(int i=0; i<=maxn; i++){
13: cube[i].x=inf; 14: cube[i].y=inf; 15: }16: for( int i=-800;i<=800;i++){
17: for(int j=-800;j<=800;j++){
18: int temp=i*i*i+j*j*j;
19: if(temp>=0 && temp<=maxn && i < cube[temp].x){
20: flag[temp]=1; 21: cube[temp].x=i; 22: cube[temp].y=j; 23: } 24: } 25: }26: for(int n;cin>>n && n;){
27: if (flag[n]==0)
28: cout<<"Impossible\n";
29: else
30: cout<<cube[n].x<<' '<<cube[n].y<<'\n';
31: } 32: }用傻逼的方法生成n(n=x^3+y^3),存到数组里,把范围改到800才过,原先的计算错了以为200^3-199^3>10^6,把范围写成200,结果还有可能出现更小的x.
悲剧。。。
本文探讨了如何通过编程解决特定数学问题:寻找两个整数的立方和等于给定正整数n的所有可能对(x, y),其中x和y的立方和等于n。文章提供了详细的算法实现,包括初始化数组、遍历所有可能的整数对并更新数组中存储的最小x值,最终输出结果或Impossible。此问题源于竞赛编程,展示了通过编程解决复杂数学问题的方法。

141

被折叠的 条评论
为什么被折叠?



