텍스트파일 첨부가 안되넹


걍 코드 긁어붙ㅇㅣㅁ 아래



#include <stdio.h>

#include <vector>


typedef std::vector< std::pair<int,int> > DT;


DT factor_product (int x) {

  DT ret;


  for ( int a = 2 ; a <= 50 ; a++ ) {

    int q = x / a;

    int m = q * a;


    if ( (m == x) && (q <= 50) && (a <= q) )

      ret.push_back (std::pair<int,int> (a,q));

  }


  return ret;

}


DT factor_sum (int x) {

  DT ret;


  for ( int a = 2 ; a <= 50 ; a++ ) {

    int b = x - a;


    if ( (b >= 2) && (b <= 50) && (a <= b) )

      ret.push_back (std::pair<int,int> (a,b));

  }


  return ret;

}


bool process_S1 (int s, DT sum_pairs) {  // true: if S can't say that

  if ( sum_pairs.size() == 1 )

    return true;


  int nr = 0;


  for ( DT::iterator it = sum_pairs.begin() ; it != sum_pairs.end() ; it++ ) {

    int p = it->first * it->second;

    DT product_pairs = factor_product (p);


    if ( product_pairs.size() == 1 )

      nr++;

  }


  if ( nr >= 1 )

    return true;


  return false;

}


bool process_P1 (int p, DT product_pairs) {  // true: if P can't say that

  if ( product_pairs.size() == 1 )

    return true;


  int nr = 0;


  for ( DT::iterator it = product_pairs.begin() ; it != product_pairs.end() ; it++ ) {

    int s = it->first + it->second;

    DT sum_pairs = factor_sum (s);


    if ( process_S1(s, sum_pairs) )

      nr++;

  }


  if ( (product_pairs.size() - nr) == 1 )  // P knows the answer

    return true;

  else if ( product_pairs.size() == nr )  // S cannot say that

    return true;


  return false;

}


bool process_S2 (int s, DT sum_pairs) {

  int nr = 0;


  for ( DT::iterator it = sum_pairs.begin() ; it != sum_pairs.end() ; it++ ) {

    int p = it->first * it->second;

    DT product_pairs = factor_product (p);


    if ( process_P1(p, product_pairs) == false )

      nr++;

  }


  if ( nr != 1 )

    return true;


  return false;

}


bool process_P2 (int p, DT product_pairs) {

  int nr = 0;


  for ( DT::iterator it = product_pairs.begin() ; it != product_pairs.end() ; it++ ) {

    int s = it->first + it->second;

    DT sum_pairs = factor_sum (s);


    if ( process_S2(s, sum_pairs) == false )

      nr++;

  }


  if ( nr != 1 )

    return true;


  return false;

}


int main () {

  int nr = 0;


  for ( int a = 2 ; a <= 50 ; a++ ) {

    for ( int b = a ; b <= 50 ; b++ ) {

      int p = a * b;

      int s = a + b;


      // w.r.t. S

      DT sum_pairs = factor_sum (s);


      if ( process_S1(s, sum_pairs) )

        continue;


      // w.r.t. P

      DT product_pairs = factor_product (p);


      if ( process_P1(p, product_pairs) )

        continue;


      // w.r.t. S

      if ( process_S2(s, sum_pairs) )

         continue;


      // w.r.t. P (this is redundant because we get the answer right after process_S2)

      if ( process_P2(p, product_pairs) )

        continue;


      printf ("answer: %d %d\n", a, b);

      nr++;

    }

  }


  printf ("%d\n", nr);


  return 0;

}