 <?php
   fscanf(STDIN,"%d%d",$a,$v);
   fscanf(STDIN,"%d%d",$b,$w);
   fscanf(STDIN,"%d",$t);

$sum_a = $a+$v*$t;
$sum_b = $b+$w*$t;

if($sum_a>=$sum_b){
  echo "YES";
}else{
  echo "NO";
}

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

-10^9 \leq A,B \leq 10^9

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


<?php
// 標準入力取得
  $hanter = fgets(STDIN);
  $hanter = str_replace(PHP_EOL, '', $hanter);

  list($hanterZ, $hanterV) = preg_split('/\s+/', $hanter);

  // $nums = preg_split('/\s+/', $line);
  $runner = fgets(STDIN);
  $runner = str_replace(PHP_EOL, '', $runner);

  list($runnerZ, $runnerV) = preg_split('/\s+/', $runner);

  $distance = abs($hanterZ - $runnerZ); // 鬼と逃げる人の距離
  $V = $hanterV - $runnerV; // 1秒に縮まる距離

  // 追いかける時間
  $chaceTime = fgets(STDIN);

    // 最初に同じ位置におらず、差が縮まらない場合には捕まえられない
  if ($V <= 0 && $distance > 0) {
    echo "NO";
    exit();
  }

  $tijimaruKyori = (int)$chaceTime * (int)$V;

  if ($tijimaruKyori > $distance) {
    echo "YES";
    exit();
  }

  echo "NO";

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


<?php
fscanf(STDIN,"%d %d",$a,$b);
echo $a * $b.PHP_EOL;

Score : 100 points

Problem Statement

Compute A \times B.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the value A \times B as an integer.

Sample Input 1

2 5

Sample Output 1

10

We have 2 \times 5 = 10.

Sample Input 2

100 100

Sample Output 2

10000


<?php
// Your code here!
$n = trim(fgets(STDIN));

if($n == 1000000007 || $n == 2){
    echo 1;
    exit;
}
if($n ==1){
    echo 0;
    exit;
}

$i= 2; //１以外の最初の素数
while ($n > $i) {
    while ($i < 0xFFFFFF) { //$iの上限を設定
        if ($n % $i == 0) { //$nが$iで割り切れた場合
            $result[] = $i; //$iを素因数として配列に格納
            $n /= $i; //$nを$iで割って$nに代入。
        }
        $i++; //$iに1をプラス
    }
}
echo count($result);
?>

Score : 400 points

Problem Statement

Given is a positive integer N. Consider repeatedly applying the operation below on N:

First, choose a positive integer z satisfying all of the conditions below:

z can be represented as z=p^e, where p is a prime number and e is a positive integer;

z divides N;

z is different from all integers chosen in previous operations.

Then, replace N with N/z.

Find the maximum number of times the operation can be applied.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum number of times the operation can be applied.

Sample Input 1

24

Sample Output 1

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=12.)

Choose z=3 (=3^1). (Now we have N=4.)

Choose z=4 (=2^2). (Now we have N=1.)

Sample Input 2

1

Sample Output 2

0

We cannot apply the operation at all.

Sample Input 3

64

Sample Output 3

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=32.)

Choose z=4 (=2^2). (Now we have N=8.)

Choose z=8 (=2^3). (Now we have N=1.)

Sample Input 4

1000000007

Sample Output 4

1

We can apply the operation once by, for example, making the following choice:

z=1000000007 (=1000000007^1). (Now we have N=1.)

Sample Input 5

997764507000

Sample Output 5

7


<?php
// Your code here!
$n = trim(fgets(STDIN));

if($n = 1000000007){
    echo 1;
    exit;
}

$i= 2; //１以外の最初の素数
while ($n !== 1) { //$nが1になるまで繰り返す
    while ($i < 0xFFFFFF) { //$iの上限を設定
        if ($n % $i == 0) { //$nが$iで割り切れた場合
            $result[] = $i; //$iを素因数として配列に格納
            $n /= $i; //$nを$iで割って$nに代入。
        }
        $i++; //$iに1をプラス
    }
}
echo count($result);
?>


Score : 600 points

Problem Statement

Given are a sequence of N positive integers A_1, A_2, \ldots, A_N and another positive integer S.

For a non-empty subset T of the set \{1, 2, \ldots , N \}, let us define f(T) as follows:

f(T) is the number of different non-empty subsets \{x_1, x_2, \ldots , x_k \} of T such that A_{x_1}+A_{x_2}+\cdots +A_{x_k} = S.

Find the sum of f(T) over all 2^N-1 subsets T of \{1, 2, \ldots , N \}. Since the sum can be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq S \leq 3000

1 \leq A_i \leq 3000

Input

Input is given from Standard Input in the following format:

N S
A_1 A_2 ... A_N

Output

Print the sum of f(T) modulo 998244353.

Sample Input 1

3 4
2 2 4

Sample Output 1

6

For each T, the value of f(T) is shown below. The sum of these values is 6.

f(\{1\}) = 0

f(\{2\}) = 0

f(\{3\}) = 1 (One subset \{3\} satisfies the condition.)

f(\{1, 2\}) = 1 (\{1, 2\})

f(\{2, 3\}) = 1 (\{3\})

f(\{1, 3\}) = 1 (\{3\})

f(\{1, 2, 3\}) = 2 (\{1, 2\}, \{3\})

Sample Input 2

5 8
9 9 9 9 9

Sample Output 2

0

Sample Input 3

10 10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

3296


<?php
fscanf(STDIN, "%s", $T);
$T = str_replace('?', 'D', $T);
echo $T;
//calc($T);

function calc($S) {
    $score = 0;
    $len = strlen($S);
    $arr = str_split($S);
    for ($i = 0; $i < $len; $i++) {
        if ($arr[$i] === 'D') {
            $score++;
        }
        if ($i > 0 && $arr[$i - 1] === 'P') {
            $score++;
        }
    }
    echo PHP_EOL . $score;
}

Score : 200 points

Problem Statement

For a string S consisting of the uppercase English letters P and D, let the doctoral and postdoctoral quotient of S be the total number of occurrences of D and PD in S as contiguous substrings. For example, if S = PPDDP, it contains two occurrences of D and one occurrence of PD as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3.

We have a string T consisting of P, D, and ?.

Among the strings that can be obtained by replacing each ? in T with P or D, find one with the maximum possible doctoral and postdoctoral quotient.

Constraints

1 \leq |T| \leq 2 \times 10^5

T consists of P, D, and ?.

Input

Input is given from Standard Input in the following format:

T

Output

Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each ? in T with P or D.
If there are multiple such strings, you may print any of them.

Sample Input 1

PD?D??P

Sample Output 1

PDPDPDP

This string contains three occurrences of D and three occurrences of PD as contiguous substrings, so its doctoral and postdoctoral quotient is 6, which is the maximum doctoral and postdoctoral quotient of a string obtained by replacing each ? in T with P or D.

Sample Input 2

P?P?

Sample Output 2

PDPD


<?php
  list($s) = explode(" ", trim(fgets(STDIN)));

  echo str_replace("?", "D", $s);


<?php

fscanf(STDIN, "%s", $N);
$array = str_split($N);
$length = count($array);
$list = [];
$copy = $array;
$k=1;
$ans = 0;
$last = -1;
for($i=0;$i<$length-1;$i++){
    if($array[$i] === "1"){
        if($array[$i+1] === "0"){
            $list[] = $i;
            $list[] = $i+1;
            unset($copy[$i]);
            unset($copy[$i+1]);
            $i++;

            $ans += $k*2;
            $k++;
            $last = -1;
        }else{

            if(isset($array[$i+2])){
                if($array[$i+2] === "1"){
                    $list[] = $i;
                    $list[] = $i+1;
                    unset($copy[$i]);
                    unset($copy[$i+1]);
                    $i++;

                    $ans += $k*2;
                    $k++;
                    $last = -1;
                }else{
                    $list[] = $i;
                    $list[] = $i+2;
                    unset($copy[$i]);
                    unset($copy[$i+2]);
                    $i+=2;

                    $ans += $k*2;
                    $k++;
                    $last = -1;
                }

            }else{
                $last = $i;
            }
        }
    }
}
if($last >= 0 ){
    $ans += $k;
    $list[] = $last;
    unset($copy[$i]);
}elseif($array[$length-1] === "1") {
    $ans += $k;
    $list[] = $length-1;
    unset($copy[$i]);
}else{
    if(count($list) === 0){
        echo 0;
        exit;
    }
    $k--;
}
krsort($copy);
$reverse = 0;
$truth = 0;
$next = count($list)-1;
//$next = $list[$list_count];
foreach($copy as $key=>$value){

    if( $next >= 0 && $key < $list[$next]){

            while ($key < $list[$next]) {
                if($array[$list[$next]] === "1"){

                    if($next % 2 === 0){
                        $truth++;
                        $k--;
                    }else{
                        $reverse++;
                    }

                }
//                if ($next === 0) {
//                    break;
//                }

                $next--;
                if($next < 0){
                    break;
                }
            }

    }
    if( $next === -1 || $key > $list[$next]){

        if($value === "1"){
            if($next % 2 === 0){
                $truth++;
            }else{
                $reverse++;
            }

        }
        $tmp = $truth ;
        $truth = $reverse;
        $reverse = $tmp;
        $ans += $k + $truth;
    }
}
echo $ans;

Score : 900 points

Problem Statement

Takahashi has an empty string S and a variable x whose initial value is 0.

Also, we have a string T consisting of 0 and 1.

Now, Takahashi will do the operation with the following two steps |T| times.

Insert a 0 or a 1 at any position of S of his choice.

Then, increment x by the sum of the digits in the odd positions (first, third, fifth, ...) of S. For example, if S is 01101, the digits in the odd positions are 0, 1, 1 from left to right, so x is incremented by 2.

Print the maximum possible final value of x in a sequence of operations such that S equals T in the end.

Constraints

1 \leq |T| \leq 2 \times 10^5

T consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

T

Output

Print the maximum possible final value of x in a sequence of operations such that S equals T in the end.

Sample Input 1

1101

Sample Output 1

5

Below is one sequence of operations that maximizes the final value of x to 5.

Insert a 1 at the beginning of S. S becomes 1, and x is incremented by 1.

Insert a 0 to the immediate right of the first character of S. S becomes 10, and x is incremented by 1.

Insert a 1 to the immediate right of the second character of S. S becomes 101, and x is incremented by 2.

Insert a 1 at the beginning of S. S becomes 1101, and x is incremented by 1.

Sample Input 2

0111101101

Sample Output 2

26


<?php
[$T] = ints();
for($i = 0; $i < $T; $i++){
  [$N, $A, $B, $C, $D] = ints();
  echo solve(),PHP_EOL;
}

function solve(){
  global $memo, $N;
  $memo = [];
  return cost($N);
}
function cost($i){
  global $N, $A, $B, $C, $D, $memo;
  if($i == 0)return 0;
  if($i == 1)return $D;
  if(isset($memo[$i]))return $memo[$i];
  $min = abs($i)*$D;

  $up2 = intdiv($i+1,2)*2;
  chmin($min, abs($up2-$i)*$D+$A+cost($up2/2));
  $down2 = intdiv($i,2)*2;
  chmin($min, abs($down2-$i)*$D+$A+cost($down2/2));

  $up3 = intdiv($i+2,3)*3;
  chmin($min, abs($up3-$i)*$D+$B+cost($up3/3));
  $down3 = intdiv($i,3)*3;
  chmin($min, abs($down3-$i)*$D+$B+cost($down3/3));

  $up5 = intdiv($i+4,5)*5;
  chmin($min, abs($up5-$i)*$D+$C+cost($up5/5));
  $down5 = intdiv($i,5)*5;
  chmin($min, abs($down5-$i)*$D+$C+cost($down5/5));

  $memo[$i] = $min;
  return $min;
}

function str(){return trim(fgets(STDIN));}
function ints($n = false){
  if($n===false){
    return array_map('intval',explode(' ',trim(fgets(STDIN))));
  }else{$ret = [];for($i = 0; $i < $n; $i++)foreach(array_map('intval',explode(' ',trim(fgets(STDIN)))) as $j => $v)$ret[$j][] = $v;return $ret;}
}
function int(){return intval(trim(fgets(STDIN)));}
function chmax(&$a,$b){if($a<$b){$a=$b;return 1;}return 0;}
function chmin(&$a,$b){if($a>$b){$a=$b;return 1;}return 0;}
function popcount($x){$c=0;while($x){$x&=$x-1;++$c;}return$c;}
function o(...$val){
    if(count($val)==1)$val=array_shift($val);
    echo debug_backtrace()[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0)echo "empty array";
        elseif(!is_array(current($val)))echo "array: ",implode(" ", addIndex($val)),"\n";
        else{
            echo "array:array\n";
            if(isCleanArray($val))foreach($val as $row)echo implode(" ", addIndex($row)),"\n";
            else foreach($val as $i => $row)echo "[".$i."] ".implode(" ", addIndex($row)),"\n";
        }
    }else echo $val."\n";
}
function addIndex($val){if(!isCleanArray($val)){$val = array_map(function($k, $v){return $k.":".$v;}, array_keys($val), $val);}return $val;}
function isCleanArray($array){$clean=true;$i = 0;foreach($array as $k=>$v){if($k != $i++)$clean = false;}return $clean;}
// 座圧対象の配列 -> [圧縮された配列,復元用のMap,圧縮用のMap]
function atsu($array){$a = array_flip($array);$fuku=array_flip($a);sort($fuku);$atsu = array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function gcdAll($array){$gcd=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$gcd=gcd($gcd,$array[$i]);}return $gcd;}
function gcd($m, $n){if(!$n)return $m;return gcd($n, $m % $n);}
function lcmAll($array){$lcm=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$lcm=lcm($lcm,$array[$i]);}return $lcm;}
function lcm($a, $b) {return $a / gcd($a, $b) * $b;}

function dfs($current, $from = -1, $d = 0){
  global $G, $distance;
  $distance[$current] = $d;
  foreach($G[$current] as $to){
    if($from != $to){
      dfs($to, $current, $d + 1);
    }
  }
}

function loadTree($N = false){
  if($N === false)$N = $GLOBALS['N'];
  return loadGraph($N, $N-1);
}
function loadGraph($N = false, $M = false, $both = true){
  if($N === false)$N = $GLOBALS['N'];
  if($M === false)$M = $GLOBALS['M'];
  $G = array_fill(1, $N, []);
  for($i = 0; $i < $M; $i++){
    $values = ints();
    if(count($values) == 2){
      list($a, $b) = $values;
      if($both == 0){
        $G[$a][] = $b;
      }elseif($both == 1){
        $G[$a][] = $b;
        $G[$b][] = $a;
      }else{
        $G[$b][] = $a;
      }
    }else{
      list($a, $b, $d) = $values;
      if($both == 0){
        $G[$a][] = [$b, $d];
      }elseif($both == 1){
        $G[$a][] = [$b, $d];
        $G[$b][] = [$a, $d];
      }else{
        $G[$b][] = [$a, $d];
      }
    }
  }
  return $G;
}

Score : 400 points

Problem Statement

You start with the number 0 and you want to reach the number N.

You can change the number, paying a certain amount of coins, with the following operations:

Multiply the number by 2, paying A coins.

Multiply the number by 3, paying B coins.

Multiply the number by 5, paying C coins.

Increase or decrease the number by 1, paying D coins.

You can perform these operations in arbitrary order and an arbitrary number of times.

What is the minimum number of coins you need to reach N?

You have to solve T testcases.

Constraints

1 \le T \le 10

1 \le N \le 10^{18}

1 \le A, B, C, D \le 10^9

All numbers N, A, B, C, D are integers.

Input

The input is given from Standard Input. The first line of the input is

T

Then, T lines follow describing the T testcases.
Each of the T lines has the format

N A B C D

Output

For each testcase, print the answer on Standard Output followed by a newline.

Sample Input 1

5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321

Sample Output 1

20
19
26
3821859835
23441258666

For the first testcase, a sequence of moves that achieves the minimum cost of 20 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 1 to multiply by 2 (x = 4).

Pay 2 to multiply by 3 (x = 12).

Pay 8 to decrease by 1 (x = 11).

For the second testcase, a sequence of moves that achieves the minimum cost of 19 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 2 to multiply by 5 (x = 10).

Pay 8 to increase by 1 (x = 11).


<?php
[$N] = ints();
$A = ints();
for($y = 0; $y < $N; $y++){
    for($x = 0;$x < $N; $x++){
        $map[$y][$x] = min($x, $y, $N-$y-1, $N-$x-1);
        $fill[$y][$x] = 1;
    }
}
$cost = 0;
foreach($A as $a){
    $a--;
    $x = $a % $N;
    $y = intdiv($a, $N);
    $cost+=$map[$y][$x];
    $currentX[] = $x;
    $currentY[] = $y;
    $fill[$y][$x] = 0;
    while($currentX){
        $nextX = [];
        $nextY = [];
        foreach($currentX as $i => $x){
            $y = $currentY[$i];
            $f = $fill[$y][$x];
            if($x > 0){
                if($map[$y][$x-1] > $map[$y][$x] + $f){
                    $map[$y][$x-1] = $map[$y][$x] + $f;
                    $nextY[] = $y;
                    $nextX[] = $x-1;
                }
            }
            if($y > 0){
                if($map[$y-1][$x] > $map[$y][$x] + $f){
                    $map[$y-1][$x] = $map[$y][$x] + $f;
                    $nextY[] = $y-1;
                    $nextX[] = $x;
                }
            }
            if($x < $N-1){
                if($map[$y][$x+1] > $map[$y][$x] + $f){
                    $map[$y][$x+1] = $map[$y][$x] + $f;
                    $nextY[] = $y;
                    $nextX[] = $x+1;
                }
            }
            if($y < $N-1){
                if($map[$y+1][$x] > $map[$y][$x] + $f){
                    $map[$y+1][$x] = $map[$y][$x] + $f;
                    $nextY[] = $y+1;
                    $nextX[] = $x;
                }
            }
        }
        $currentX = $nextX;
        $currentY = $nextY;
    }
}
echo $cost;
//o($map);

function str(){return trim(fgets(STDIN));}
function ints($n = false){
  if($n===false){
    return array_map('intval',explode(' ',trim(fgets(STDIN))));
  }else{$ret = [];for($i = 0; $i < $n; $i++)foreach(array_map('intval',explode(' ',trim(fgets(STDIN)))) as $j => $v)$ret[$j][] = $v;return $ret;}
}
function int(){return intval(trim(fgets(STDIN)));}
function chmax(&$a,$b){if($a<$b){$a=$b;return 1;}return 0;}
function chmin(&$a,$b){if($a>$b){$a=$b;return 1;}return 0;}
function popcount($x){$c=0;while($x){$x&=$x-1;++$c;}return$c;}
function o(...$val){
    if(count($val)==1)$val=array_shift($val);
    echo debug_backtrace()[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0)echo "empty array";
        elseif(!is_array(current($val)))echo "array: ",implode(" ", addIndex($val)),"\n";
        else{
            echo "array:array\n";
            if(isCleanArray($val))foreach($val as $row)echo implode(" ", addIndex($row)),"\n";
            else foreach($val as $i => $row)echo "[".$i."] ".implode(" ", addIndex($row)),"\n";
        }
    }else echo $val."\n";
}
function addIndex($val){if(!isCleanArray($val)){$val = array_map(function($k, $v){return $k.":".$v;}, array_keys($val), $val);}return $val;}
function isCleanArray($array){$clean=true;$i = 0;foreach($array as $k=>$v){if($k != $i++)$clean = false;}return $clean;}
// 座圧対象の配列 -> [圧縮された配列,復元用のMap,圧縮用のMap]
function atsu($array){$a = array_flip($array);$fuku=array_flip($a);sort($fuku);$atsu = array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function gcdAll($array){$gcd=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$gcd=gcd($gcd,$array[$i]);}return $gcd;}
function gcd($m, $n){if(!$n)return $m;return gcd($n, $m % $n);}
function lcmAll($array){$lcm=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$lcm=lcm($lcm,$array[$i]);}return $lcm;}
function lcm($a, $b) {return $a / gcd($a, $b) * $b;}

function dfs($current, $from = -1, $d = 0){
  global $G, $distance;
  $distance[$current] = $d;
  foreach($G[$current] as $to){
    if($from != $to){
      dfs($to, $current, $d + 1);
    }
  }
}

function loadTree($N = false){
  if($N === false)$N = $GLOBALS['N'];
  return loadGraph($N, $N-1);
}
function loadGraph($N = false, $M = false, $both = true){
  if($N === false)$N = $GLOBALS['N'];
  if($M === false)$M = $GLOBALS['M'];
  $G = array_fill(1, $N, []);
  for($i = 0; $i < $M; $i++){
    $values = ints();
    if(count($values) == 2){
      list($a, $b) = $values;
      if($both == 0){
        $G[$a][] = $b;
      }elseif($both == 1){
        $G[$a][] = $b;
        $G[$b][] = $a;
      }else{
        $G[$b][] = $a;
      }
    }else{
      list($a, $b, $d) = $values;
      if($both == 0){
        $G[$a][] = [$b, $d];
      }elseif($both == 1){
        $G[$a][] = [$b, $d];
        $G[$b][] = [$a, $d];
      }else{
        $G[$b][] = [$a, $d];
      }
    }
  }
  return $G;
}

Score : 700 points

Problem Statement

Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2.

At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat.
To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column).
While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever.

Compute the number of pairs of viewers (x, y) such that y will hate x forever.

Constraints

2 \le N \le 500

The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \{1, 2, \dots, N^2\}.

Input

The input is given from Standard Input in the format

N
P_1 P_2 \cdots P_{N^2}

Output

If ans is the number of pairs of viewers described in the statement, you should print on Standard Output

ans

Sample Input 1

3
1 3 7 9 5 4 8 6 2

Sample Output 1

1

Before the end of the movie, the viewers are arranged in the cinema as follows:

1 2 3
4 5 6
7 8 9

The first four viewers leaving the cinema (1, 3, 7, 9) can leave the cinema without going through any seat, so they will not be hated by anybody.

Then, viewer 5 must go through one of the seats where viewers 2, 4, 6, 8 are currently seated while leaving the cinema; hence he will be hated by at least one of those viewers.

Finally the remaining viewers can leave the cinema (in the order 4, 8, 6, 2) without going through any occupied seat (actually, they can leave the cinema without going through any seat at all).

Sample Input 2

4
6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8

Sample Output 2

3

Sample Input 3

6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30

Sample Output 3

11


<?php
$N = trim(fgets(STDIN));

$array = str_split($N, 1);
if (preg_match('/[24579]/', end($array))) {
    echo 'hon';
} else if(preg_match('/[0168]/', end($array))){
    echo 'pon';
} else if(preg_match('/[3]/', end($array))) {
    echo 'bon';
}

Score: 100 points

Problem Statement

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a positive integer N not exceeding 999 is as follows:

hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;

pon when the digit in the one's place of N is 0, 1, 6 or 8;

bon when the digit in the one's place of N is 3.

Given N, print the pronunciation of "本" in the phrase "N 本".

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

The digit in the one's place of 16 is 6, so the "本" in "16 本" is pronounced pon.

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon


<?php

$maxLength = trim(fgets(STDIN));
$input = trim(fgets(STDIN));
$inputLength = strlen($input);

if ($maxLength <= $inputLength) {
    $input = substr($input, 0, $maxLength);
}
echo $input;

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

K is an integer between 1 and 100 (inclusive).

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


<?php
fscanf(STDIN,"%d",$K);
$S = trim(fgets(STDIN));
if(strlen($S) <= $K){
    echo $S;
}else{
    print substr($S,0,$K)."...";
}
?>


<?php
fscanf(STDIN,"%d",$n);
$s = fgets(STDIN);
$s_n = strlen($s);
echo $s_n;
if($s_n <= $n){
    echo $s;
}else{
    echo substr($s,0,$n)."...";
}
?>


<?php

$k = trim(fgets(STDIN));
$s = trim(fgets(STDIN));

$s_length = strlen($s);

if ($s_length <= $k) {
    echo $s . "\n";
} else {
    echo substr($s, 0, ((int)$k)) . '...';
}


<?php
list($A, $B, $H, $M) = array_map(function ($n) {
    return (int)$n;
}, explode(' ', fgets(STDIN)));

$long_omega = 0.5;
$short_omega = 6.0;

$min = $H * 60 + $M;
$long_angle = $long_omega * $min;
$short_angle = $short_omega * $min % 360.0;

$angle = abs($long_angle - $short_angle);

echo sqrt($A * $A + $B * $B - 2 * $A * $B * cos(2 * pi() * ($angle / 360.0)));

Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.


Score: 500 points

Problem Statement

We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.

We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.

The i-th and j-th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0.

In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

-10^{18} \leq A_i, B_i \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

There are five ways to choose the set of sardines, as follows:

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4

Sample Output 2

479


<?php
$name = rtrim(fgets(STDIN));
$namePlus = rtrim(fgets(STDIN));
$add = rtrim(fgets(STDIN));

if (strlen($add) == 1  && substr($name, -1) == $name) {
    echo 'Yes';
} else {
    echo 'No';
}

Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

He tried to register himself with the ID S, which turned out to be already used by another user.

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

chokudaiz can be obtained by appending z at the end of chokudai.

Sample Input 2

snuke
snekee

Sample Output 2

No

snekee cannot be obtained by appending one character at the end of snuke.

Sample Input 3

a
aa

Sample Output 3

Yes


<?php
  $first = input();
  $second = input();

  echo preg_match("/^{$first}.$/", $second) === 1 ? 'Yes' : 'No';

function input()
{
  return trim(fgets(STDIN));
}


<?php

$stdin = trim(fgets(STDIN));
$stdin1 = trim(fgets(STDIN));

if (substr($stdin, 0, strlen($stdin)) === substr($stdin1, 0, strlen($stdin))
 && strlen($stdin1) - strlen($stdin) === 1
   ) {
  echo 'Yes';
} else {
  echo 'No';
}


<?php
  $s = trim(fgets(STDIN));
  $t = trim(fgets(STDIN));
  $e = strlen($t);
  $s .= $t[$e-1];
  if($s == $t){
      echo "Yes\n";
  }else{
      echo "No\n";
  }

?>


<?php
list($a, $b, $c, $k) = explode(' ', trim(fgets(STDIN)));

if ($a >= $k) {
  echo $k;
} else if ($a + $b >= $k) {
  echo $a;
} else {
  echo $a + ($k - ($a + $b)) * -1;
}

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


<?php

$input = str_replace('　', ' ', fgets(STDIN));
$line = explode(' ', $input);

$A = $line[0];
$B = $line[1];
$C = $line[2];
$K = $line[3];

if(full_up_with_ab($A, $B, $K)){
  echo $A;
}else {
  if(full_up_with_abc($A, $B, $C, $K)){
    $C = $K - ($A + $B);
  }
  echo $A - $C;
}

function full_up_with_ab($A, $B, $K){
  return ($A + $B) >= $K;
}

function full_up_with_abc($A, $B, $C, $K){
  return ($A + $B + $C) >= $K;
}


<?php
  fscanf(STDIN,"%d %d %d %d",$a,$b,$c,$k);
  $total = 0;

  $total = min($a,$k);

  if(($a+$b)<$k){
      $total -= min($c,abs($k-($a+$b)));
  }
  echo $total."\n";


<?php
fscanf(STDIN, '%d %d %d %d', $a, $b, $c, $k);

if ($a <= $k) {
  $sum = $a;
  $k -= $a;
} elseif ($k < $a) {
  $sum = $k;
  echo $sum;
  exit;
}

if ($b <= $k) {
  $k -= $b;
} else {
  echo $sum;
  exit;
}

if ($c <= $k) {
  $sum -= $c;
} else {
  $sum -= $k;
}

echo $sum;


<?php
$S = trim(fgets(STDIN));  // 入力
if ($S == ABC) {
    echo 'ARC';
} elseif ($S == ARC) {
    echo 'ABC';
}

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

There are two types of contests called ABC and ARC, and just one of them is held at a time.

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

Print the string representing the type of the contest held this week.

Sample Input 1

ABC

Sample Output 1

ARC

They held an ABC last week, so they will hold an ARC this week.


<?php
$s = trim(fgets(STDIN));

echo $s == "ABC" ? "ARC" : "ABC";
echo PHP_EOL;


<?php
fscanf(STDIN, "%d %d", $N, $K);
$arr = [];
for ($i = 0; $i < $K; $i++) {
    fscanf(STDIN, "%d", $dummy);
    $A = explode(' ', trim(fgets(STDIN)));
    foreach ($A as $n) {
        $arr[$n] = 1;
    }
}
printf("%d", $N - count($arr));

Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


<?php
  list($n, $k) = explode(" ", trim(fgets(STDIN)));

  $snuke = array_fill(1, $n, 0);

  for($i=0; $i<$k; $i++){
    list($d) = explode(" ", trim(fgets(STDIN)));
    $a = explode(" ", trim(fgets(STDIN)));
    for($j=0; $j<$d; $j++){
      $snuke[$a[$j]]++;
    }
  }

  $ans = 0;
  for($i=1; $i<=$n; $i++){
    if($snuke[$i] == 0){
      $ans++;
    }
  }

  echo $ans;


<?php
$arr = explode(' ', trim(fgets(STDIN)));

$n = $arr[0];
$m = $arr[1];

$h_arr = explode(' ', trim(fgets(STDIN)));

for ($i=1; $i <= $n; $i++) {
    $h_nums[] = $i;
}

for ($i=0; $i < $m; $i++) {
    $arr2 = explode(' ', trim(fgets(STDIN)));
    $a = $arr2[0];
    $b = $arr2[1];

    // echo "anum =".$a.PHP_EOL;
    // echo "bnum =".$b.PHP_EOL;
    // echo "a =".$h_arr[$a - 1].PHP_EOL;
    // echo "b =".$h_arr[$b - 1].PHP_EOL;

    if($h_arr[$a - 1] > $h_arr[$b - 1]) {
        $ng_nums[] = $b;
    } elseif($h_arr[$a - 1] == $h_arr[$b - 1]) {
        $ng_nums[] = $a;
        $ng_nums[] = $b;
    } else
    {
        $ng_nums[] = $a;
    }
}
$ng_nums = array_unique($ng_nums);
// for ($i=0; $i < count($ng_nums); $i++) {
//     echo "ng_nums = ".$ng_nums[$i].PHP_EOL;
// }

$h_nums = array_diff($h_nums, $ng_nums);
$h_nums = array_values($h_nums);
// for ($i=0; $i < count($h_nums); $i++) {
//     echo "h_nums = ". $h_nums[$i].PHP_EOL;
// }

echo count($h_nums);

return;
?>

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

4 3
1 2 3 4
1 3
2 3
2 4

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

6 5
8 6 9 1 2 1
1 3
4 2
4 3
4 6
4 6

Sample Output 2

3


<?php
list($n,$m)=explode(" ",trim(fgets(STDIN)));
$h=explode(" ",trim(fgets(STDIN)));
$i=0;
while($i<$m){
  list($a,$b)=explode(" ",trim(fgets(STDIN)));
  $map[$a][$b]=$h[$b-1];
  $map[$b][$a]=$h[$a-1];
  ++$i;
}
$ans=0;
$i=1;
while($i<=$n){
  if(isset($map[$i])){
    if($h[$i-1]>max($map[$i])){
      ++$ans;
    }
  }else{
    ++$ans;
  }
  ++$i;
}
echo $ans;


Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


<?php
$n = trim(fgets(STDIN));

while (true) {

    $a = floor(nthRoot($n, 5));

    $tmp = $n - pow($a,5);
    $b = nthRoot($tmp, 5);

    if ($b == floor($b)){
        break;
    }

    $a = $a + 1;
}

if (!$b == 0) {
    $b = $b * (-1);
}

echo  $a . " " . $b;

function nthRoot($x,$n){
    return pow($x, 1 / $n);
}

?>


<?php

fscanf(STDIN, "%d", $n);
$A = explode(' ', trim(fgets(STDIN)));

/**
 * A[i] + A[j] = j - i (i < j)
 * 両辺にiとjが散らばっているのと考えづらいので
 * 式変形して左辺はi、右辺はjにする。これは定石らしい
 * i + A[i] = j - A[j]
 *
 * 探索するときはjを動かしていく
 * jを動かしていきi(<j)の中に条件を満たすものがないかを調べていく
 * ただし、ここでiをループで調べると実行時間オーバーする
 *
 * これまでの結果を持っておく配列を用意する。
 * この配列から調べる <==> i(<j)から調べる
 */

$map = [];
$cnt = 0;
for ($j = 0; $j < $n; ++$j) {
    $key = $j - $A[$j];

    $cnt += $map[$key];

    ++$map[$j + $A[$j]];
}

print_r($map);

echo $cnt;

Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5

Sample Output 3

22


fscanf(STDIN, "%d", $k);
fscanf(STDIN, "%d %d", $a,$b);

for(; $a < $b;$a++){
  if ($a % $k == 0) {
   $r = 'OK';
   break;
  }else {
    $r = 'NG';
    continue;
  }
}
echo $r;

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

If he can achieve the objective, print OK; if he cannot, print NG.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

If he can achieve the objective, print OK; if he cannot, print NG.

Sample Input 1

7
500 600

Sample Output 1

OK

Among the multiples of 7, for example, 567 lies between 500 and 600.

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


<?php
fscanf(STDIN, "%d", $number); // STDIN から数値を読み込む
fscanf(STDIN, "%d %d", $number2,$number3); // STDIN から数値を読み込む
$x1 = int($number2 / $number);
$x2 = int($number3 / $number);
for($s=$x1;$s<$x2;$s++){
  if($number == $s){
    echo "OK";
  }else{
  echo "NG";
  }

}
?>


fscanf(STDIN, "%d %d %d", $k,$a,$b);
//$input = fgets(STDIN);
//$k = (int)explode('', $input)[0];
//$a = (int)explode('', $input)[1];
//$b = (int)explode('', $input)[2];

for($i = $a; $i < $b; $i++) {
  if (($i % $k) === 0) {
    echo 'OK';
  }else {
    echo 'NO';
  }
}


<?php
while($input_line = fgets(STDIN)){
    $tmp[] = trim($input_line);
}

$k = array_shift($tmp);

$a = explode(" " ,$tmp[0]);

$b = $a[0];
$c = $a[1];

$i=1;
while($i<=$c){
    if($i%$k == 0){
        if($i>=$b){
           $result[] = $i;
        }
    }
    $i++;
}
if(isset($result)){
    echo "OK";
}else{
    echo "NG";
}
?>


<?php
[$K] = ints();
[$A, $B] = ints();
$flag = false;
for($i = $A; $i<=$B; $i++)if($i%$K==0)$flag = true;
echo $flag ? "OK":"NG";

function str(){return trim(fgets(STDIN));}
function ints(){return array_map('intval',explode(' ',trim(fgets(STDIN))));}
function int(){return intval(trim(fgets(STDIN)));}
function chmax(&$a,$b){if($a<$b){$a=$b;return 1;}return 0;}
function chmin(&$a,$b){if($a>$b){$a=$b;return 1;}return 0;}
function popcount($x){$c=0;while($x){$x&=$x-1;++$c;}return$c;}
function o(...$val){
    if(count($val)==1)$val=array_shift($val);
    echo debug_backtrace()[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0)echo "empty array";
        elseif(!is_array(current($val)))echo "array: ",implode(" ", addIndex($val)),"\n";
        else{
            echo "array:array\n";
            if(isCleanArray($val))foreach($val as $row)echo implode(" ", addIndex($row)),"\n";
            else foreach($val as $i => $row)echo "[".$i."] ".implode(" ", addIndex($row)),"\n";
        }
    }else echo $val."\n";
}
function addIndex($val){if(!isCleanArray($val)){$val = array_map(function($k, $v){return $k.":".$v;}, array_keys($val), $val);}return $val;}
function isCleanArray($array){$clean=true;$i = 0;foreach($array as $k=>$v){if($k != $i++)$clean = false;}return $clean;}
// 座圧対象の配列 -> [圧縮された配列,復元用のMap,圧縮用のMap]
function atsu($array){$a = array_flip($array);$fuku=array_flip($a);sort($fuku);$atsu = array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function gcdAll($array){$gcd=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$gcd=gcd($gcd,$array[$i]);}return $gcd;}
function gcd($m, $n){if(!$n)return $m;return gcd($n, $m % $n);}
function lcmAll($array){$lcm=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$lcm=lcm($lcm,$array[$i]);}return $lcm;}
function lcm($a, $b) {return $a / gcd($a, $b) * $b;}

function dfs($current, $from = -1, $d = 0){
  global $G, $distance;
  $distance[$current] = $d;
  foreach($G[$current] as $to){
    if($from != $to){
      dfs($to, $current, $d + 1);
    }
  }
}

function loadTree($N = false){
  if($N === false)$N = $GLOBALS['N'];
  return loadGraph($N, $N-1);
}
function loadGraph($N = false, $M = false, $both = true){
  if($N === false)$N = $GLOBALS['N'];
  if($M === false)$M = $GLOBALS['M'];
  $G = array_fill(1, $N, []);
  for($i = 0; $i < $M; $i++){
    $values = ints();
    if(count($values) == 2){
      list($a, $b) = $values;
      $G[$a][] = $b;
      if($both)$G[$b][] = $a;
    }else{
      list($a, $b, $d) = $values;
      $G[$a][] = [$b, $d];
      if($both)$G[$b][] = [$a, $d];
    }
  }
  return $G;
}


<?php
[$N] = fscanf(STDIN, "%d");

$start = 100;
$n = 0;
while($start < $N){
    $start = bcmul($start, 1.01, 0);
    $n++;
}
echo $n;

Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


<?php
# スペース区切りの整数の入力
fscanf(STDIN, "%d %d", $S, $W);
# 出力
if($W >= $S){
    print("unsafe");
} else {
    print("safe");
}

Score : 100 points

Problem Statement

There are S sheep and W wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

1 \leq S \leq 100

1 \leq W \leq 100

Input

Input is given from Standard Input in the following format:

S W

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe


<?php

$N = fgets(STDIN);

$results = [];

for($i=0; $i<$N; $i++) {
  $results[fgets(STDIN)] = 1;
}

echo count($results)."\n";

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

1 \leq N \leq 2\times 10^5

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


<?php
fscanf(STDIN, '%d %d', $n, $m);
$numbers = fgets(STDIN);
$exploded_numbers = explode(' ', trim($numbers));

$result = $n - array_sum($exploded_numbers);
if ($result < 0) {
  echo '-1';
} else {
  echo $result;
}

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


<?php
// C
fscanf(STDIN, '%d', $n);
$a = array_map('intval', explode(' ', trim(fgets(STDIN))));
$buka = array_fill(0, $n, 0);
for ($i = 0; $i < $n - 1; $i++) {
    $buka[$a[$i] - 1]++;
}
echo implode(PHP_EOL, $buka);

Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


<?php
  while ($line = fgets(STDIN)) {
      $inputs[] = trim($line);
  };
  $n = (int)$inputs[0];
  $a_s = explode(" ", $inputs[1]);
$n = (int) $n;

$ans = array();
for ($i=0; $i < $n - 1; $i++) {
  $index = (int) $a_s[$i];
  if (!isset($ans[$index])) {
    $ans[$index] = 1;
  } else {
    $ans[$index]++;
  }
}
for ($i=1; $i < $n + 1; $i++) {
  if (!isset($ans[$i])) {
    echo 0 . "\n";
  } else {
    echo $ans[$index] . "\n";
  }
}


<?php
fscanf(STDIN, "%d", $n);
$a = explode(' ', trim(fgets(STDIN)));
$count = array_count_values($a);

for ($i = 1; $i <= $n; $i++) {
    if (!isset($count[$i])) {
        echo "0\n";
        continue;
    }

    echo "{$count[$i]}\n";
}


<?php
define("MOD", 10**9+7);

[$N, $K] = ints();
$c = 0;
for($i = $K; $i<=$N+1; $i++){
    if($i == $N+1){
        $a = 1;
    }else{
        $a = $i*($N+1-$i)+1;
    }
//    o($i, $a);
    $c+=$a;
    $c%=MOD;
}
echo $c;
/**
 * とても高速なnCm関数です
 *  prepareModで事前計算しておき、
 *  nCmを呼び出しましょう。
 *  AtCoderの2秒制約ならN=10^6ぐらいまで通ります
 */

function prepareMod($n){
    global $f, $invf;
    $f[0] = $invf[0] = $invf[1] = 1;
    for($i = 1; $i <= $n; $i++)$f[$i] = $f[$i-1]*$i%MOD;
    for($i = 2; $i <= $n; $i++)$invf[$i] = (MOD - intdiv(MOD, $i))*$invf[MOD%$i]%MOD;
    for($i = 2; $i <= $n; $i++)$invf[$i] = $invf[$i]*$invf[$i-1]%MOD;
}
function nCm($n, $m){
    global $f, $invf;
    if($n < 0 || $n - $m < 0)return 0;
    return $f[$n] * $invf[$n-$m] % MOD * $invf[$m] % MOD;
}
function nHm($n, $m){
    global $f, $invf;
    return nCm($n+$m-1, $m);
}
function str(){return trim(fgets(STDIN));}
function ints(){return array_map('intval',explode(' ',trim(fgets(STDIN))));}
function int(){return intval(trim(fgets(STDIN)));}
function chmax(&$a,$b){if($a<$b){$a=$b;return 1;}return 0;}
function chmin(&$a,$b){if($a>$b){$a=$b;return 1;}return 0;}
function popcount($x){$c=0;while($x){$x&=$x-1;++$c;}return$c;}
function o(...$val){
    if(count($val)==1)$val=array_shift($val);
    echo debug_backtrace()[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0)echo "empty array";
        elseif(!is_array(current($val)))echo "array: ",implode(" ", addIndex($val)),"\n";
        else{
            echo "array:array\n";
            if(isCleanArray($val))foreach($val as $row)echo implode(" ", addIndex($row)),"\n";
            else foreach($val as $i => $row)echo "[".$i."] ".implode(" ", addIndex($row)),"\n";
        }
    }else echo $val."\n";
}
function addIndex($val){if(!isCleanArray($val)){$val = array_map(function($k, $v){return $k.":".$v;}, array_keys($val), $val);}return $val;}
function isCleanArray($array){$clean=true;$i = 0;foreach($array as $k=>$v){if($k != $i++)$clean = false;}return $clean;}
// 座圧対象の配列 -> [圧縮された配列,復元用のMap,圧縮用のMap]
function atsu($array){$a = array_flip($array);$fuku=array_flip($a);sort($fuku);$atsu = array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function gcdAll($array){$gcd=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$gcd=gcd($gcd,$array[$i]);}return $gcd;}
function gcd($m, $n){if(!$n)return $m;return gcd($n, $m % $n);}
function lcmAll($array){$lcm=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$lcm=lcm($lcm,$array[$i]);}return $lcm;}
function lcm($a, $b) {return $a / gcd($a, $b) * $b;}

function dfs($current, $from = -1, $d = 0){
  global $G, $distance;
  $distance[$current] = $d;
  foreach($G[$current] as $to){
    if($from != $to){
      dfs($to, $current, $d + 1);
    }
  }
}

function loadTree($N = false){
  if($N === false)$N = $GLOBALS['N'];
  return loadGraph($N, $N-1);
}
function loadGraph($N = false, $M = false, $both = true){
  if($N === false)$N = $GLOBALS['N'];
  if($M === false)$M = $GLOBALS['M'];
  $G = array_fill(1, $N, []);
  for($i = 0; $i < $M; $i++){
    $values = ints();
    if(count($values) == 2){
      list($a, $b) = $values;
      $G[$a][] = $b;
      if($both)$G[$b][] = $a;
    }else{
      list($a, $b, $d) = $values;
      $G[$a][] = [$b, $d];
      if($both)$G[$b][] = [$a, $d];
    }
  }
  return $G;
}


<?php
    fscanf(STDIN,"%d",$n);
    $sum = 0;
    for($index = 1;$index <= $n; ++$index){
        if($index % 3 === 0 || $index % 5 === 0){
            continue;
        }
        $sum += $index;
    }
    echo $sum.PHP_EOL;
?>

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


<?php

    // AtCoder Beginner Contest 161
    // C - Replacing Integer

    list($n, $k) = in_ints();

    $n = $n % $k;
    while(true) {
        $wk = abs($n - $k);
        if ($n <= $wk) {
            break;
        }
        $n = $wk;
    }
    out($n);

    /**
    * お手伝い関数さんたち
    */
    function in() {
        return trim(fgets(STDIN));
    }
    function in_int() {
        return (int)in();
    }
    function in_vals() {
        return explode(' ', in());
    }
    function in_ints() {
        return array_map('intval', in_vals());
    }
    function out($s) {
        echo $s . PHP_EOL;
    }
    function yes() {
        out('Yes');
    }
    function no() {
        out('No');
    }

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


<?php

class scan {
    private $arr = [];
    private $count = 0;
    private $pointer = 0;
    public function next() {
        if($this->pointer >= $this->count) {
            $str = trim(fgets(STDIN));
            $this->arr = explode(' ', $str);
            $this->count = count($this->arr);
            $this->pointer = 0;
        }
        $result = $this->arr[$this->pointer];
        $this->pointer++;
        return $result;
    }
    public function hasNext() {
        return $this->pointer < $this->count;
    }
    public function nextInt() {
        return (int)$this->next();
    }
    public function nextDouble() {
        return (double)$this->next();
    }
}

$sc = new scan();
$n = $sc->nextInt();
$k = $sc->nextInt();

if ($n % $k == 0) {
    echo 0;
} else {
    $flag = true;

    while ($flag) {
        if (abs($n-$k) < $n) {
            $n = abs($n-$k);
        } else {
            $flag = false;
        }
    }

    echo $n;
}


<?php
//error_reporting(0);
//$N = (int)trim(fgets(STDIN));
//$S = trim(fgets(STDIN));
list($N,$K) = array_map('intval',explode(" ",trim(fgets(STDIN))));
//$a = array_map('intval',explode(" ",trim(fgets(STDIN))));

printf("%d\n",min($N%$K,abs(($N%$K)-$K)));


<?php
list($N, $K) = ints();
echo min($N % $K, $K - $N % $K);

function str(){return trim(fgets(STDIN));}
function ints(){return array_map('intval',explode(' ',trim(fgets(STDIN))));}
function int(){return intval(trim(fgets(STDIN)));}
function chmax(&$a,$b){if($a<$b){$a=$b;return 1;}return 0;}
function chmin(&$a,$b){if($a>$b){$a=$b;return 1;}return 0;}
function popcount($x){$c=0;while($x){$x&=$x-1;++$c;}return$c;}
function o(...$val){
    if(count($val)==1)$val=array_shift($val);
    echo debug_backtrace()[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0)echo "empty array";
        elseif(!is_array(current($val)))echo "array: ",implode(" ", addIndex($val)),"\n";
        else{
            echo "array:array\n";
            if(isCleanArray($val))foreach($val as $row)echo implode(" ", addIndex($row)),"\n";
            else foreach($val as $i => $row)echo "[".$i."] ".implode(" ", addIndex($row)),"\n";
        }
    }else echo $val."\n";
}
function addIndex($val){if(!isCleanArray($val)){$val = array_map(function($k, $v){return $k.":".$v;}, array_keys($val), $val);}return $val;}
function isCleanArray($array){$clean=true;$i = 0;foreach($array as $k=>$v){if($k != $i++)$clean = false;}return $clean;}
// 座圧対象の配列 -> [圧縮された配列,復元用のMap,圧縮用のMap]
function atsu($array){$a = array_flip($array);$fuku=array_flip($a);sort($fuku);$atsu = array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function gcdAll($array){$gcd=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$gcd=gcd($gcd,$array[$i]);}return $gcd;}
function gcd($m, $n){if(!$n)return $m;return gcd($n, $m % $n);}
function lcmAll($array){$lcm=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$lcm=lcm($lcm,$array[$i]);}return $lcm;}
function lcm($a, $b) {return $a / gcd($a, $b) * $b;}

function dfs($current, $from = -1, $d = 0){
  global $G, $distance;
  $distance[$current] = $d;
  foreach($G[$current] as $to){
    if($from != $to){
      dfs($to, $current, $d + 1);
    }
  }
}

function loadTree($N = false){
  if($N === false)$N = $GLOBALS['N'];
  return loadGraph($N, $N-1);
}
function loadGraph($N = false, $M = false, $both = true){
  if($N === false)$N = $GLOBALS['N'];
  if($M === false)$N = $GLOBALS['M'];
  $G = array_fill(1, $N, []);
  for($i = 0; $i < $M; $i++){
    $values = ints();
    if(count($values) == 2){
      list($a, $b) = $values;
      $G[$a][] = $b;
      if($both)$G[$b][] = $a;
    }else{
      list($a, $b, $d) = $values;
      $G[$a][] = [$b, $d];
      if($both)$G[$b][] = [$a, $d];
    }
  }
  return $G;
}


<?php

$k = trim(fgets(STDIN));

$que = [];
$count = 0;
for($i = 1; $i <= 9; $i++) {
    $que[] = $i;
    $count++;
    if ($count == $k) {
        print $i;
        return;
    }
}

while (true) {
    $num = array_shift($que);
    $last = substr($num, -1);
    if ($last != 0) bfs($num, $last-1);
    bfs($num, $last);
    if ($last != 9) bfs($num, $last+1);
}
function bfs($num, $last) {
    global $que, $count, $k;
    $que[] = $num . $last;
    //print $num.$last . "\n";
    $count++;
    if ($count == $k) {
        print $num . $last;
        exit;
    }
}

Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

Note that the answer may not fit into the 32-bit signed integer type.


<?php
  $a=str_split(trim(fgets(STDIN)));
if($a[2]==$a[3] && $a[4]==$a[5]){
  echo "Yes";
}else{
  echo "No";
}

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


<?php

fscanf(STDIN,"%d", $S);

$alpha_1 = $S[2];
$alpha_2 = $S[3];
$alpha_3 = $S[4];
$alpha_4 = $S[5];

if(($alpha_1 === $alpha_2) and ($alpha_3 === $alpha_4)){
	printf("Yes");
}else{
	printf("No");
}
return;

?>


<?php

$S = intval(trim(fgets(STDIN)));

$alpha_1 = substr($S, 3);
$alpha_2 = substr($S, 4);
$alpha_3 = substr($S, 5);
$alpha_4 = substr($S, 6);

if($alpha_1 == $alpha_2 and $alpha_3 == $alpha_4){
	echo "Yes";
}else{
	echo "No";
}

?>


<?php

    $x = in_int();

    $cnt500 = floor($x / 500);
    $cnt5 = floor(($x - ($cnt500 * 500)) / 5);

    out(($cnt500 * 1000) + ($cnt5 * 5));

    /**
    * お手伝い関数さんたち
    */
    function in() {
        return trim(fgets(STDIN));
    }
    function in_int() {
        return (int)in();
    }
    function in_vals() {
        return explode(' ', in());
    }
    function in_ints() {
        return array_map('intval', in_vals());
    }
    function out($s) {
        echo $s . PHP_EOL;
    }
    function yes() {
        out('Yes');
    }
    function no() {
        out('No');
    }
    function factorial($n) { // 階乗
        if ($n > 0) {
            return $n *= factorial($n - 1);
        }
        return 1;
    }
    function is_palindrome($s) {  // 回文か
        return $s == strrev($s);
    }

Score : 200 points

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)

Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?

(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

Constraints

0 \leq X \leq 10^9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the maximum number of happiness points that can be earned.

Sample Input 1

1024

Sample Output 1

2020

By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned.

Sample Input 2

0

Sample Output 2

0

He is penniless - or yenless.

Sample Input 3

1000000000

Sample Output 3

2000000000

He is a billionaire - in yen.


<?php

$home = explode(" ", trim(fgets(STDIN)));

$distance = explode(" ", trim(fgets(STDIN)));

$max = ($home[0] - $distance[$home[1]-1]) + $distance[0];
$check = 0;
for($i=1; $i<$home[1]; $i++){
	if($distance[$i]-$distance[$i-1] > $max){
		$max = $distance[$i]-$distance[$i-1];
		$check = $i;
	}
}

$ans = 0;
if($check == 0){
	for($i=1; $i<$home[1]; $i++){
		$ans += $distance[$i]-$distance[$i-1];
	}
}else{
	for($i=$check; $i<$home[1]-1; $i++){
		$ans += $distance[$i+1]-$distance[$i];
	}
	$ans += ($home[0] - $distance[$home[1]-1]) + $distance[0];
	for($i=0; $i<$check-1; $i++){
		$ans += $distance[$i+1]-$distance[$i];
	}
}
echo $ans;

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

3 \leq N \leq 2 \times 10^3

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


<?php
// F
define("MOD", 10 ** 9 + 7);
fscanf(STDIN, '%d', $n);
for ($i  = 0; $i < $n - 1; $i++) {
    fscanf(STDIN, '%d %d', $a, $b);
    $g[$a][] = $b;
    $g[$b][] = $a; // 無向グラフの場合
}
dfs1(1);
dfs2(1);
$ans = [];
for ($i = 1; $i <= $n; $i++) {
    $ans[] = $dp[$i][-1]['value'];
}
echo implode(PHP_EOL, $ans);

function dfs1($v, $par = -1)
{
    global $g, $dp;

    $size = 0;
    $value = 1;
    foreach ($g[$v] as $to) {
        if ($to === $par) continue;
        dfs1($to, $v);
        $size += $dp[$to][$v]['size'];
        $value = modMul($value, $dp[$to][$v]['value']);
        $value = modMul($value, nCr($size, $dp[$to][$v]['size']));
    }
    $size++;
    $dp[$v][$par] = compact('value', 'size');
}

function dfs2($v, $par = -1)
{
    global $g, $dp;

    $size = $dp[$v][-1]['size'];
    foreach ($g[$v] as $to) {
        if ($to === $par) continue;
        $value = modMul($dp[$v][-1]['value'], $dp[$to][$v]['size']);
        $value = modDiv($value, $size - $dp[$to][$v]['size']);
        $dp[$to][-1] = compact('value', 'size');
        dfs2($to, $v);
    }
}

// 足し算
function modAdd($x, $y)
{
    return ($x + $y) % MOD;
}

// 引き算
function modSub($x, $y)
{
    return ($x + MOD - $y) % MOD;
}

// 掛け算
function modMul($x, $y)
{
    return ($x * $y) % MOD;
}

// 割り算
function modDiv($x, $y)
{
    return modMul($x, modPow($y, MOD - 2));
}

// 累乗（繰り返し二乗法）
function modPow($n, $x)
{
    if ($x === 0) return 1;
    $res = (modPow($n, $x >> 1) ** 2) % MOD;
    if ($x % 2 === 1) $res = modMul($res, $n);
    return $res;
}

// 階乗
function modFac($n)
{
    if ($n === 0) return 1;
    return modMul($n, modFac($n - 1));
}

// 順列
function nPr($n, $r)
{
    if ($r === 0) return 1;
    return modDiv(modFac($n), modFac($n - $r));
}

// 組み合わせ
function nCr($n, $r)
{
    $r = min($r, $n - $r);
    if ($r === 0) return 1;
    return modDiv(nPr($n, $r), modFac($r));
}

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
For each k=1, ..., N, solve the problem below:

Consider writing a number on each vertex in the tree in the following manner:

First, write 1 on Vertex k.

Then, for each of the numbers 2, ..., N in this order, write the number on the vertex chosen as follows:

Choose a vertex that still does not have a number written on it and is adjacent to a vertex with a number already written on it. If there are multiple such vertices, choose one of them at random.

Find the number of ways in which we can write the numbers on the vertices, modulo (10^9+7).

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For each k=1, 2, ..., N in this order, print a line containing the answer to the problem.

Sample Input 1

3
1 2
1 3

Sample Output 1

2
1
1

The graph in this input is as follows:

For k=1, there are two ways in which we can write the numbers on the vertices, as follows:

Writing 1, 2, 3 on Vertex 1, 2, 3, respectively

Writing 1, 3, 2 on Vertex 1, 2, 3, respectively

Sample Input 2

2
1 2

Sample Output 2

1
1

The graph in this input is as follows:

Sample Input 3

5
1 2
2 3
3 4
3 5

Sample Output 3

2
8
12
3
3

The graph in this input is as follows:

Sample Input 4

8
1 2
2 3
3 4
3 5
3 6
6 7
6 8

Sample Output 4

40
280
840
120
120
504
72
72

The graph in this input is as follows:


Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


<?php
fscanf(STDIN, "%d", $l);
$x = $l/3;
echo pow($x, 3);

Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


<?php
// D
fscanf(STDIN, '%d', $n);
$a = array_map('intval', explode(' ', trim(fgets(STDIN))));
for ($i = 0; $i < $n; $i++) {
    if (isset($cnt[$a[$i]])) {
        $cnt[$a[$i]]++;
    } else {
        $cnt[$a[$i]] = 1;
    }
}
$kumi_sum = 0;
foreach ($cnt as $i => $v) {
    $kumi_sum += $v * ($v - 1) / 2;
}
for ($i = 0; $i < $n; $i++) {
    $ans[] = $kumi_sum - ($cnt[$a[$i]] - 1);
}
echo implode(PHP_EOL, $ans);

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


<?php
// From https://qiita.com/yoshikyoto/items/6924478283dbdd549e4b
class Scanner {
    private $arr = [];
    private $count = 0;
    private $pointer = 0;
    public function next() {
        if($this->pointer >= $this->count) {
            $str = trim(fgets(STDIN));
            $this->arr = explode(' ', $str);
            $this->count = count($this->arr);
            $this->pointer = 0;
        }
        $result = $this->arr[$this->pointer];
        $this->pointer++;
        return $result;
    }
    public function hasNext() {
        return $this->pointer < $this->count;
    }
    public function nextInt() {
        return (int)$this->next();
    }
    public function nextDouble() {
        return (double)$this->next();
    }
}
// From https://qiita.com/yoshikyoto/items/6924478283dbdd549e4b
list($N)=sscanf(trim(fgets(STDIN)),"%d");
$sc = new Scanner();
$A = array();
for($i=0;$i<$N;$i++){
    $A[$i] = $sc->nextInt();
}
for($e=0;$e<$N;$e++){
    $pat=array();
    $cnt=0;
    for($i=0;$i<$N;$i++){
        if($i != $e){
            if($target =(array_key_exists($A[$i],$pat) ) === FALSE){
                $pat[$A[$i]] = 1;
            }else{
                $pat[$A[$i]]+= 1;
            };
        }
    }
    foreach($pat as $value){
        for($i=$value-1;$i>=1;$i--){
            $cnt += $i;
        }
    }
    printf($cnt."\n");
}


Score : 100 points

Problem Statement

Print the K-th element of the following sequence of length 32:

1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51

Constraints

1 \leq K \leq 32

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the K-th element.

Sample Input 1

6

Sample Output 1

2

The 6-th element is 2.

Sample Input 2

27

Sample Output 2

5

The 27-th element is 5.


Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


<?php
  fscanf(STDIN,"%d %d",$h,$w);
  echo round(($h*$w)/2)."\n";


<?php

$input = explode(' ', fgets(STDIN));

$a = intval($input[0]);
$b = intval($input[1]);

if ($a == 1 || $b == 1) {
  echo '1';
  return;
}

echo (int)ceil(($a * $b) / 2);

?>


<?php
list($H,$W)=sscanf(trim(fgets(STDIN)),"%d %d");
$p1 = ceil($H / 2);
$p2 = floor($H / 2);
$calc = 0;

    $calc = $p1 * ceil($W/2);
    $calc += $p2 * floor($W/2);

printf($calc);


<?php
// $n = (int)trim(fgets(STDIN));
$input = explode(' ', trim(fgets(STDIN)));

$h = (int)$input[0];
$w = (int)$input[1];

if ($h === 1 || $w === 1) {
	echo 1;
	exit();
}

$even = (int)ceil($w / 2);
$odd = (int)floor($w / 2);
$sum = (int)ceil($h/2) * $even + (int)floor($h/2) * $odd;
echo $sum;


<?php
$input = file_get_contents('php://stdin');

if(strlen($input)%2!=0){
    echo "No";
    exit();
}
$length = (int)floor(strlen($input)/2);
for($i=0;$i<$length;$i++){
    $output = substr($input, $i*2, 2);
    if($output!=="hi"){
        echo "No";
        exit();
    }
}
echo "Yes";

?>

Score : 100 points

Problem Statement

A Hitachi string is a concatenation of one or more copies of the string hi.

For example, hi and hihi are Hitachi strings, while ha and hii are not.

Given a string S, determine whether S is a Hitachi string.

Constraints

The length of S is between 1 and 10 (inclusive).

S is a string consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a Hitachi string, print Yes; otherwise, print No.

Sample Input 1

hihi

Sample Output 1

Yes

hihi is the concatenation of two copies of hi, so it is a Hitachi string.

Sample Input 2

hi

Sample Output 2

Yes

Sample Input 3

ha

Sample Output 3

No


<?php
fscanf(STDIN, "%s", $s);

$replace = str_replace("hi","",$s);

if($replace == ""){
    echo "Yes";
}else{
    echo "No";

}


Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


<?php
fscanf(STDIN, '%d %d %d', $N, $A, $B);

$aSet = intval(floor($N / ($A + $B))) * $A;
$mod = $N % ($A+$B);

echo $aSet + min($mod, $A);
echo PHP_EOL;

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


<?php
  list($a,$b,$c)=explode(" ",trim(fgets(STDIN)));
$nanko=floor($a/($b+$c));
$nokori=$a%($b+$c);
$sosu=$nanko*$b;
if($nokori<$b){
  echo $sosu+=$nokori;
}else{
  echo $sosu+=$b;
}


<?php

fscanf(STDIN,"%d %d %d", $n,$a,$b);

    $count_loop = (int)($n/($a+$b));
    $left_count = $n%($a+$b);

    echo $count_loop*$a+min($a,$left_count);


<?php
fscanf(STDIN, "%d %d %d", $N, $A, $B);

$numOfBlue = 0;

if ($A !== 0) {

  $I = floor($N / ($A + $B)) ;
  $J = $N % ( $A + $B );

  if ($J > $A) {

    $K = $A;

  } else {

    $K = $J;
  }

  $numOfBlue = $I * $A + $K;
}

echo  $numOfBlue;


<?php
# スペース区切りの整数の入力
fscanf(STDIN, "%d %d %d", $n, $blue, $red);

$result = 0;
$diff = 0;
for($length = 0;;) {
  $length = $length + $blue;
  $result = $result + $blue;
  if ($length >= $n) {
    $diff = $length - $n;
    $result = $result - $diff;
    break;
  }
  $length = $length + $red;
  if ($length >= $n) {
    break;
  }
}
echo $result;


<?php
  list($n, $a, $b) = explode(" ", trim(fgets(STDIN)));
    $ans = floor($n / ($a + $b)) * $a;
    $rem = $n % ($a + $b);
    $ans = $ans + min($a, $rem);
    echo $ans;
 ?>


<?php

  list($n, $a, $b) = explode(" ", trim(fgets(STDIN)));

  $ans = (int)($n/($a+$b))*$a;
  $mod = $n%($a+$b);
  if($mod <= $a){
    $ans+=$mod;
  }else{
    $ans+=$a;
  }
  echo $ans;


Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.


<?php

// 入力値の取得
$AB = explode(" ", fgets(STDIN));
$A = intval($AB[0]);
$B = intval($AB[1]);

for ($price = 1; $price < 1010; $price++) {
    // 消費税が条件を満たしているかチェック
    if (
        (int) floor($price * 0.08) === $A &&
        (int) floor($price * 0.1) === $B)
    {
        echo $price;
        exit;
    }
}

// ここまできてる時は条件を満たすものがないので-1を表示
echo -1;

?>


<?php

$in = file_get_contents('php://stdin');

$inarr = explode(" ", $in);

$a = intval($inarr[0]);
$b = intval($inarr[1]);

$_a = floor($a / 0.08);
$_b = floor($b / 0.1);

$lower = min($_a, $_b);
$upper = max($_a, $_b);

$ans = -1;
for ($i = $lower - 13; $i <= $upper; $i++) {
  $_a = floor($i * 0.08);
  $_b = floor($i * 0.1);
  if ($_a == $a && $b == $_b) {
    $ans = $i;
    break;
  }
}
echo $ans . PHP_EOL;


<?php
$query = trim(fgets(STDIN));
list($a,$b) = sscanf($query,"%d %d");
for($i=1; $i<=10000; $i++){
    if(((intval((float)$i * 0.08) == $a)&&(intval((float)$i * 0.1) == $b))){
        printf($i);
        exit;
    }
}
printf(-1);


<?php
class Scanner {
    private $arr = [];
    private $count = 0;
    private $pointer = 0;
    public function next() {
        if($this->pointer >= $this->count) {
            $str = trim(fgets(STDIN));
            $this->arr = explode(' ', $str);
            $this->count = count($this->arr);
            $this->pointer = 0;
        }
        $result = $this->arr[$this->pointer];
        $this->pointer++;
        return $result;
    }
    public function hasNext() {
        return $this->pointer < $this->count;
    }
    public function nextInt() {
        return (int)$this->next();
    }
    public function nextDouble() {
        return (double)$this->next();
    }
}

class out {
    public static function println($str = "") {
        echo $str . PHP_EOL;
    }
}

$sc = new Scanner();
$S = $sc->next();
$Q = $sc->nextInt();
$reverse = false;

for($i = 0; $i < $Q; ++$i) {
	$T = $sc->nextInt();
	if($T === 1) {
		if($reverse) {
			$reverse = false;
		} else {
			$reverse = true;
		}
	} else {
		if($reverse) {
			$reverse = false;
			$S = strrev($S);
		}
		$F = $sc->nextInt();
		if($F === 1) {
			$S = $sc->next() . $S;
		} else {
			$S = $S . $sc->next();
		}
	}
}
if($reverse) {
	$reverse = false;
	$S = strrev($S);
}

out::println($S);

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


<?php
//error_reporting(0);
$s = trim(fgets(STDIN));
$Q = (int)trim(fgets(STDIN));
//list($A,$B) = explode(" ",trim(fgets(STDIN)));
//$a[] = array_map('intval',explode(" ",trim(fgets(STDIN))));

for($i=0;$i<$Q;$i++) {
  $q = explode(" ",trim(fgets(STDIN)));
  if ($q[0]==1) {
    $s = strrev($s);
  }
  else {
    if ($q[1]==1) {
      $s = $q[2].$s;
    }
    else {
      $s = $s . $q[2];
    }
  }
}
printf("%s\n",$s);


<?php
$a = fgets(STDIN);
$array = explode(' ', $a);
$n = str_replace("\n", '', $array[0]);
$p = str_replace("\n", '', $array[1]);
$str = str_replace("\n", '', fgets(STDIN));
for($i = 0; $i < $n; $i++) {
  $num_arr[] = $str[$i];
  $tmp_str = $str[$i];
  $t = 1;
  while($t < $n-$i) {
    $tmp_str .= $str[$i + $t];
    $num_arr[] = $tmp_str;
    $t++;
  }
}

$result = 0;
foreach($num_arr as $val) {
  if($val % $p == 0) {
    $result++;
  }
}
echo $result;
?>

Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

1 \leq N \leq 2 \times 10^5

S consists of digits.

|S| = N

2 \leq P \leq 10000

P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

3: divisible by 3.

35: not divisible by 3.

354: divisible by 3.

3543: divisible by 3.

5: not divisible by 3.

54: divisible by 3.

543: divisible by 3.

4: not divisible by 3.

43: not divisible by 3.

3: divisible by 3.

Six of these are divisible by 3, so print 6.

Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.

Sample Input 3

20 11
33883322005544116655

Sample Output 3

68


<?php
$query = fgets(STDIN);
if($query%2 == 1){
    $query += 1;
}
printf($query/2);
?>

Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


<?php
$query = trim(fgets(STDIN));
if($query%2 == 1){
    $query += 1;
}
printf($query/2);


<?php
$stdin = trim(fgets(STDIN));

$n = intval($stdin);

$p = intval($n + 1);

echo $p;


Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

1 \leq A_{i, j} \leq 100

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


<?php

  $n = trim(fgets(STDIN));
  $arr = str_split(fgets(STDIN));

  $s = trim(fgets(STDIN));
  for($i=0;$i<$s;$i++){
    list($type, $param1, $param2) = explode(" ", fgets(STDIN));
    if($type==1){
      $arr[$param1] = $param2;
    }else{
      $outarr = array_slice($arr, $param1-1, $param2-$param1+1);
      echo count(array_flip($outarr)).PHP_EOL;
    }
  }