ZJUT Practice 3.3 F - Sum of Cubes(HDU - 3113)

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.
 
Output
For each test case, output two integers x and y such that x 3 + y 3 = 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.

悲剧。。。

转载于:https://www.cnblogs.com/ZJUT-jiangnan/archive/2013/03/15/2960619.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值