結果

問題 No.438 Cwwプログラミング入門
ユーザー happy-beans
提出日時 2017-03-18 18:18:02
言語 PHP
(843.2)
結果
TLE  
実行時間 -
コード長 3,578 bytes
コンパイル時間 716 ms
コンパイル使用メモリ 31,124 KB
実行使用メモリ 69,056 KB
最終ジャッジ日時 2024-07-04 19:19:02
合計ジャッジ時間 5,039 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 2 TLE * 2 -- * 94
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php

// No.438
$args = explode(" ", trim(fgets(STDIN)));
$x = $args[0];
$y = $args[1];
$z = $args[2];

$cww = new Cww($x, $y, $z);
$cww->main();
if (strlen($cww->result) <= 10000) {
  echo $cww->result.PHP_EOL;
}
else {
  echo "NO".PHP_EOL;
}
return;

class Cww
{
  public $stack;
  public $x;
  public $y;
  public $z;
  public $result;

  public $a;
  public $b;

  public function __construct($x, $y, $z)
  {
    $this->result = "";
    $this->stack = array();
    $this->x = $x;
    $this->y = $y;
    $this->z = $z;

    $this->a = 0;
    $this->b = 0;
  }

  public function main()
  {
    if ($this->calc() == false) {
      $this->result = "NO";
      return;
    }
    // echo "a, b = $this->a, $this->b".PHP_EOL;

    // 負数の可能性のあるbを先にスタックに保存
    for ($i = 0; $i < abs($this->b); $i++) {
      $this->pushSW();
    }

    // a を加算
    if ($this->a >= 1) {
      $this->pushSC();
    }
    for ($i = 1; $i < $this->a; $i++) {
      $this->plusX();
    }

    // b を加算または減算
    if ($this->b < 0) {
      while (count($this->stack) >= 2) {
        $this->pushBW();
      }
    }
    else if ($this->b >= 0){
      while (count($this->stack) >= 2) {
        $this->pushBC();
      }
    }
  }

  public function calc()
  {
    $hasAnswer = false;
    // z = ax + by をみたす a, bを求める。
    // x,y,z > 0 から、a,bがともに負になることはないので、a>=0 とする。
    if ($this->y == 0) {
      if ($this->x == 0) {
        $this->a = 0;
        $this->b = 0;
        return true;
      }
      else if (($this->z % $this->x) != 0) {
        // echo "false?".PHP_EOL;
        return false;
      }
      else {
        // echo "truee".PHP_EOL;
        $this->a = $this->z / $this->x;
        $this->b = 0;
        return true;
      }
    }
    for ($i = 0; $i<=$this->z; $i++) {
      if ((abs($this->z - $i*$this->x) % $this->y) == 0) {
        $this->a = $i;
        $this->b = ($this->z-$this->a*$this->x)/$this->y;
        $hasAnswer = true;
        break;
      }
    }
    //  "a,b = $this->a, $this->b".PHP_EOL;
    return $hasAnswer;
  }

  // 最上位スタックの数字をチェック
  public function check()
  {
    $c = count($this->stack);
    return ($c >= 1) ? $this->stack[$c - 1] : false;
  }

  // 最上位スタックの数字にxを加算
  public function plusX()
  {
    $this->pushSC();
    return $this->pushBC();
  }

  // 最上位のスタックの数字にyを加算
  public function plusY()
  {
    $this->pushSW();
    return $this->pushBC();
  }

  // 'c' を読込
  public function pushSC()
  {
    array_push($this->stack, $this->x);
    $this->result .= "c";
    return true;
  }

  // 'w' を読込
  public function pushSW()
  {
    array_push($this->stack, $this->y);
    $this->result .= "w";
    return true;
  }

  // 'C' を読込
  // スタックに2つ以上要素がない場合は false
  public function pushBC()
  {
    if (count($this->stack) >= 2) {
      $tmp = array_pop($this->stack);
      $tmp += array_pop($this->stack);
      array_push($this->stack, $tmp);
      $this->result .= "C";
      return true;
    }
    else {
      return false;
    }
  }

  // 'W' を読込
  // スタックに2つ以上要素がない場合は false
  public function pushBW()
  {
    if (count($this->stack) >= 2) {
      $tmp = array_pop($this->stack);
      $tmp -= array_pop($this->stack);
      array_push($this->stack, $tmp);
      $this->result .= "W";
      return true;
    }
    else {
      return false;
    }
  }
}
0