Suhendry’s Blog

When in doubt, do math ;-)

ICPC Indonesia National Contest 2009

with 4 comments

C - Why is Math Book So Sad?

Problem: C - Why is Math Book So Sad?
Author: Andoko Chandra

Batasan output non-negative dan 16-bit data membuat soal ini bisa di-bruteforce pada nilai x. Dari persamaan x^2 + y^2 + z = A, dapat disimpulkan bahwa x dan y harus lebih kecil dari 2^8, karena jika tidak maka nilai A akan lebih besar dari 2^16 yang bertentangan dengan batasan nilai input.


Solusi C/C++
oleh Suhendry Effendy

#include <cstdio>
#include <algorithm>
using namespace std;

int power(int n, int p) {
  int ret = 1;
  while ( p-- ) ret = ret * n;
  return ret;
}

int main()
{
  int a, b, c, d;
  while ( scanf( "%d %d %d %d", &a, &b, &c, &d ) == 4 ) {
    bool found = false;
    for ( int x = 0; x < (1 << 16) && !found; x++ ) {
      int y = x * x - d;
      int z = a - x * x - y * y;
      if ( power(x + y, b) + y == c ) {
        printf( "%d %d %d\n", x, y, z );
        found = true;
      }
    }
    if ( !found ) puts( "No solution" );
  }

  return 0;
}



Solusi JAVA
oleh Suhendry Effendy

import java.util.*;

public class C {
  int power(int n, int p) {
    int ret = 1;
    while ( p > 0 ) { ret *= n; p--; }
    return ret;
  }

  void solve() {
    Scanner scan = new Scanner(System.in);
    while ( scan.hasNextInt() ) {
      int a = scan.nextInt();
      int b = scan.nextInt();
      int c = scan.nextInt();
      int d = scan.nextInt();

      Boolean found = false;
      for ( int x = 0; x < (1 << 16) && !found; x++ ) {
        int y = x * x - d;
        int z = a - x * x - y * y;
        if ( power(x + y, b) + y == c ) {
          System.out.printf( "%d %d %d\n", x, y, z );
          found = true;
        }
      }
      if ( !found ) System.out.print( "No solution\n" );
    }
  }

  public static void main(String[] args){
    new C().solve();
  }
}





Pages: 1 2 3 4 5 6 7

Written by suhendry

December 20th, 2009 at 2:14 am

4 Responses to 'ICPC Indonesia National Contest 2009'

Subscribe to comments with RSS

  1. hahaha…akhirnya,,,,setelah sekian lama n agak basi…hehe.. nulis write-upny jg…dah di tagih ma mas felix tuh…hehe..

    brainplusplus

    20 Dec 09 at 8:56 am

  2. Yoi Mon! Thx!

    Btw, nge-blog background-story (+ foto2 pilihan) nya juga donk yang menceritakan bagaimana jalannya lomba ;) Tapi keknya setelah di delay begini lama, udah lupa >..< Gw lagi mao nulis report juga :(

    Felix Halim

    20 Dec 09 at 1:27 pm

  3. gw bahkan udah lupa, soalnya kek gmana gara2 nih write up kelamaan… wkwkkwkwkkw…

    Felix J

    23 Dec 09 at 1:58 pm

  4. thanks buat pembahasannya. Keren!
    Walaupun skrg saya udah jarang main2 problem solving. He3, but nice to read a comprehensive blog like this.

    samsu

    23 Dec 09 at 6:43 pm

Leave a Reply