結果

問題 No.438 Cwwプログラミング入門
ユーザー happy-beanshappy-beans
提出日時 2017-03-18 18:18:02
言語 PHP
(8.3.4)
結果
TLE  
実行時間 -
コード長 3,578 bytes
コンパイル時間 380 ms
コンパイル使用メモリ 12,048 KB
実行使用メモリ 49,200 KB
最終ジャッジ日時 2023-09-18 02:31:48
合計ジャッジ時間 3,952 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
16,848 KB
testcase_01 AC 7 ms
12,264 KB
testcase_02 AC 8 ms
12,264 KB
testcase_03 TLE -
testcase_04 AC 456 ms
49,200 KB
testcase_05 AC 194 ms
32,560 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
testcase_93 -- -
testcase_94 -- -
testcase_95 -- -
testcase_96 -- -
testcase_97 -- -
testcase_98 -- -
testcase_99 -- -
testcase_100 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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