fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. typedef pair<int,int> expNumber ;
  9. typedef pair<expNumber,expNumber> ansPair;
  10.  
  11. bool isFact(int a,vector<expNumber> &v){
  12. int power=2;
  13. bool flag=false;
  14. do{
  15. int base = pow((double)a,1./(double)power)+1.e-7;
  16. double fa = pow((double)base,(double)power);
  17. if(fabs(fa-a)<1.e-7){
  18. v.emplace_back(base,power);
  19. flag=true;
  20. }
  21. power++;
  22. }while(pow(a,1./power)>=2.);
  23. return flag;
  24. }
  25.  
  26. vector<ansPair> search(int num){
  27. vector<ansPair> ans;
  28. for (int a = 4; a <= (num+1)/2; a++) {
  29. vector<expNumber> eA,eB;
  30. int b = num-a;
  31. if(!isFact(b,eB))continue;
  32. if(!isFact(a,eA))continue;
  33. for (auto&& ita : eA) {
  34. for (auto&& itb : eB) {
  35. ans.emplace_back(ita,itb);
  36. }
  37. }
  38. }
  39. return ans;
  40. }
  41.  
  42. void printAnsPair(int num,vector<ansPair> v){
  43. if(v.empty()){
  44. cout<<"o^o+o^o="<<num<<" is nothing"<<endl;
  45. return;
  46. }
  47. for (auto&& it : v) {
  48. printf("%d^%d+%d^%d=%d\n",it.first.first,it.first.second,
  49. it.second.first,it.second.second,num);
  50. }
  51. cout <<endl;
  52. }
  53.  
  54. int main(void){
  55. vector<int> query={2017,20017,200017,2000017};
  56. for(auto&& n: query)printAnsPair(n,search(n));
  57. return 0;
  58. }
  59.  
  60.  
Success #stdin #stdout 3.5s 3472KB
stdin
Standard input is empty
stdout
9^2+44^2=2017
3^4+44^2=2017
17^2+12^3=2017

39^2+136^2=20017
81^2+116^2=20017
9^4+116^2=20017
3^8+116^2=20017

264^2+361^2=200017
264^2+19^4=200017

o^o+o^o=2000017 is nothing