You are given a string $$$s$$$. You need to find two non-empty strings $$$a$$$ and $$$b$$$ such that the following conditions are satisfied:  Strings $$$a$$$ and $$$b$$$ are both subsequences of $$$s$$$.  For each index $$$i$$$, character $$$s_i$$$ of string $$$s$$$ must belong to exactly one of strings $$$a$$$ or $$$b$$$.  String $$$a$$$ is lexicographically minimum possible; string $$$b$$$ may be any possible string. Given string $$$s$$$, print any valid $$$a$$$ and $$$b$$$.Reminder:A string $$$a$$$ ($$$b$$$) is a subsequence of a string $$$s$$$ if $$$a$$$ ($$$b$$$) can be obtained from $$$s$$$ by deletion of several (possibly, zero) elements. For example, "dores", "cf", and "for" are subsequences of "codeforces", while "decor" and "fork" are not.A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds:  $$$x$$$ is a prefix of $$$y$$$, but $$$x \ne y$$$;  in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains one string $$$s$$$ ($$$2 \le |s| \le 100$$$ where $$$|s|$$$ means the length of $$$s$$$). String $$$s$$$ consists of lowercase Latin letters.

For each test case, print the strings $$$a$$$ and $$$b$$$ that satisfy the given conditions. If there are multiple answers, print any.

NoteIn the first test case, there are only two choices: either $$$a =$$$ f and $$$b = $$$ c or $$$a = $$$ c and $$$b = $$$ f. And $$$a = $$$c is lexicographically smaller than $$$a = $$$ f.In the second test case, a is the only character in the string.In the third test case, it can be proven that b is the lexicographically smallest subsequence of $$$s$$$. The second string can be of two variants; one of them is given here.

<?php
class IO {
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }

    public static function res($res) {
        foreach ($res as $r) {
            echo $r . "\n";
        }
    }

    public static function dd($val){
        echo $val; exit;
    }
}

$t = IO::str();

while ($t--) {
    $s = strtolower(IO::str());

    $asci = array();
    $arr = str_split($s);
    $min = 200;
    for ($i = 0; $i < strlen($s); $i++) {
        $k = ord($arr[$i]);
        if ($k < $min) {
            $min = $k;
            $idx = $i;
        }
    }

    $s1 = $arr[$idx];
    unset($arr[$idx]);

    $s2 = implode($arr);

    echo $s1 . ' ' . $s2 . PHP_EOL;
}

<?php
class IO {
    public static $aAlphabet = ['A','B','C','D', 'E', 'F', 'G', 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    //Input/Output
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function asLine($mArr, $sep = ' ') { echo implode($sep, $mArr) . PHP_EOL; }

    //Z-function
    public static function z($sString) {
        $s = str_split($sString);
        $n = count($s);
        $z = array_fill(0, $n, 0);
        $r = $l = 0;
        for ($i = 1; $i < $n; $i++) {
            if ($r >= $i) {
                $z[$i] = min($z[$i - $l], $r - $i + 1);
            }
            while ($z[$i] + $i < $n && $s[$z[$i]] == $s[$z[$i] + $i]) {
                $z[$i]++;
            }
            if ($i + $z[$i] - 1 > $r) {
                $l = $i;
                $r = $i + $z[$i] - 1;
            }
        }
        return $z;
    }
    public static function getCountOfDiffSubstrWithZ($sString)
    {
        $s = str_split($sString);
        $n = count($s);
        $count = 0;
        $t = '';
        for ($i = $n - 1; $i >= 0; $i--) {
            $t = $s[$i] . $t;
            $count += $i + 1 - max(self::z($t));
        }
        return $count;
    }
    public static function getMinPeriodOfSubstrWithZ($sString)
    {
        $s = str_split($sString);
        $res = count($s);
        $z = self::z($sString);
        foreach ($z as $iI => $sV) {
            if ((int) $iI + $z[$iI] == $res && $res % (int) $iI == 0) {
                $res = $iI;
                break;
            }
        }
        return $res;
    }

    //Other functions
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
    public static function factorial($number)
    {
        $factorial = 1;
        for ($i = 1; $i <= $number; $i++){
            $factorial = $factorial * $i;
        }
        return $factorial;
    }

}

//$iQueryCount = 1;
$iQueryCount = IO::str();
$aAllResults = [];
while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
   $s = IO::str();
   $a = str_split($s);
   $min = 'z';
   $mink = 99;
   foreach ($a as $k => $v) {
       if ($v <= $min) {
           $min = $v;
           $mink = $k;
       }
   }
   unset($a[$mink]);
   $s1 = $min;
   $s2 = implode('', $a);
   IO::line($s1 . ' ' . $s2);
}

?>


Polycarp has a poor memory. Each day he can remember no more than $$$3$$$ of different letters. Polycarp wants to write a non-empty string of $$$s$$$ consisting of lowercase Latin letters, taking minimum number of days. In how many days will he be able to do it?Polycarp initially has an empty string and can only add characters to the end of that string.For example, if Polycarp wants to write the string lollipops, he will do it in $$$2$$$ days:   on the first day Polycarp will memorize the letters l, o, i and write lolli;  On the second day Polycarp will remember the letters p, o, s, add pops to the resulting line and get the line lollipops. If Polycarp wants to write the string stringology, he will do it in $$$4$$$ days:   in the first day will be written part str;  on day two will be written part ing;  on the third day, part of olog will be written;  on the fourth day, part of y will be written. For a given string $$$s$$$, print the minimum number of days it will take Polycarp to write it.

The first line of input data contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of a non-empty string $$$s$$$ consisting of lowercase Latin letters (the length of the string $$$s$$$ does not exceed $$$2 \cdot 10^5$$$) — the string Polycarp wants to construct. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single number — minimum number of days it will take Polycarp to write the string $$$s$$$ from memory.

6

lollipops

stringology

abracadabra

codeforces

test

f

<?php

$array = [];
$t = (int)fgets(STDIN);
while ($t--) {

    $str = str_split(trim(fgets(STDIN)));

    $arr = [];
    $x = 0;
    $days = 0;
    for ($i = 0; $i < count($str); $i++) {

        if (!in_array($str[$i], $arr) and $x < 3) {
            array_push($arr, $str[$i]);
            $x++;
        } else {
            if ($x == 3) {
                if (!in_array($str[$i], $arr)) {
                    $arr = [];
                    $x = 0;
                    $days++;
                    $i--;
                }
            }
        }
    }
    array_push($array, $days + 1);
}
foreach ($array as $key => $value)
    print($value . "\r\n");
$end = readline();
?>

<?php

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

$result = "";

for($i=0; $i < $case; $i++){
	$string_array = str_split(trim(fgets(STDIN)));

	$string_count = count($string_array);

	$memory_count = 0; // 현재 기억한 갯수

	$now_memory = array(); // 현재 기억한 것

	$day_count = 0; // 걸린 날짜

	$already_exists = false;

	for($j=0; $j < $string_count; $j++){

		if($memory_count >= 3 && !in_array($string_array[$j], $now_memory)){ // 현재 기억한 것이 3 이상이면
			$memory_count = 0;
			$now_memory = array(); // 초기화
			$day_count++;
		}

		if(in_array($string_array[$j], $now_memory)){ // 이미 있으면
			// 아무것도 안 함
			$already_exists = true;
		}else{ // 없으면
			array_push($now_memory, $string_array[$j]);
			$memory_count++;
			$already_exists = false;
		}
	}

	if($memory_count > 0){
		$day_count++;
	}

	$result .= $day_count."\n";
}

echo $result;

<?php
function solve($st) {
    $arr = str_split($st) ;
    $temp = [] ;
    $days = 0 ;
    foreach($arr as $a) {
        if(!in_array($a,$temp)) {
            $temp[] = $a;
        }
        if(count($temp) > 3) {
            $days += 1 ;
            $temp = array($a) ;
        }
    }
    if(count($temp) > 0 ) {
        $days += 1 ;
    }
    return $days ;
}

$t = (int) fgets(STDIN);
$ans = [] ;
while($t--) {
    $st = trim(fgets(STDIN));
    $ans[] = solve($st);
}
print_r(implode("\n",$ans)) ;


Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $$$x$$$, and the shorter rabbit is currently on position $$$y$$$ ($$$x \lt y$$$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $$$a$$$, and the shorter rabbit hops to the negative direction by $$$b$$$.  For example, let's say $$$x=0$$$, $$$y=10$$$, $$$a=2$$$, and $$$b=3$$$. At the $$$1$$$-st second, each rabbit will be at position $$$2$$$ and $$$7$$$. At the $$$2$$$-nd second, both rabbits will be at position $$$4$$$.Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.

Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Each test case contains exactly one line. The line consists of four integers $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$ ($$$0 \le x \lt y \le 10^9$$$, $$$1 \le a,b \le 10^9$$$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.

For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position. If the two rabbits will never be at the same position simultaneously, print $$$-1$$$.

NoteThe first case is explained in the description.In the second case, each rabbit will be at position $$$3$$$ and $$$7$$$ respectively at the $$$1$$$-st second. But in the $$$2$$$-nd second they will be at $$$6$$$ and $$$4$$$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.


NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to $$$\operatorname{or} z$$$. Can you solve this problem too?You are given a 1-indexed array of $$$n$$$ integers, $$$a$$$, and an integer $$$z$$$. You can do the following operation any number (possibly zero) of times:  Select a positive integer $$$i$$$ such that $$$1\le i\le n$$$. Then, simutaneously set $$$a_i$$$ to $$$(a_i\operatorname{or} z)$$$ and set $$$z$$$ to $$$(a_i\operatorname{and} z)$$$. In other words, let $$$x$$$ and $$$y$$$ respectively be the current values of $$$a_i$$$ and $$$z$$$. Then set $$$a_i$$$ to $$$(x\operatorname{or}y)$$$ and set $$$z$$$ to $$$(x\operatorname{and}y)$$$. Here $$$\operatorname{or}$$$ and $$$\operatorname{and}$$$ denote the bitwise operations OR and AND respectively.Find the maximum possible value of the maximum value in $$$a$$$ after any number (possibly zero) of operations.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$z$$$ ($$$1\le n\le 2000$$$, $$$0\le z<2^{30}$$$). The second line of each test case contains $$$n$$$ integers $$$a_1$$$,$$$a_2$$$,$$$\ldots$$$,$$$a_n$$$ ($$$0\le a_i<2^{30}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.

For each test case, print one integer — the answer to the problem.

NoteIn the first test case of the sample, one optimal sequence of operations is:  Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(3\operatorname{or}3)=3$$$ and $$$z$$$ becomes $$$(3\operatorname{and}3)=3$$$.  Do the operation with $$$i=2$$$. Now $$$a_2$$$ becomes $$$(4\operatorname{or}3)=7$$$ and $$$z$$$ becomes $$$(4\operatorname{and}3)=0$$$.  Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(3\operatorname{or}0)=3$$$ and $$$z$$$ becomes $$$(3\operatorname{and}0)=0$$$. After these operations, the sequence $$$a$$$ becomes $$$[3,7]$$$, and the maximum value in it is $$$7$$$. We can prove that the maximum value in $$$a$$$ can never exceed $$$7$$$, so the answer is $$$7$$$.In the fourth test case of the sample, one optimal sequence of operations is:  Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(7\operatorname{or}7)=7$$$ and $$$z$$$ becomes $$$(7\operatorname{and}7)=7$$$.  Do the operation with $$$i=3$$$. Now $$$a_3$$$ becomes $$$(30\operatorname{or}7)=31$$$ and $$$z$$$ becomes $$$(30\operatorname{and}7)=6$$$.  Do the operation with $$$i=5$$$. Now $$$a_5$$$ becomes $$$(27\operatorname{or}6)=31$$$ and $$$z$$$ becomes $$$(27\operatorname{and}6)=2$$$.

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

for ($t1=1; $t1<=$t; $t1++) {
    list($n, $z) = explode(' ', trim(fgets(STDIN)));
    $a = explode(' ', trim(fgets(STDIN)));

    for ($i=0; $i<$n; $i++) {
        $a[$i] = (int) $a[$i] | (int) $z;
        $z = (int) $a[$i] & (int) $z;
    }

    // print_r($a);
    echo max($a) . PHP_EOL;
}


A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.Determine if it is possible for the islanders to arrange the statues in the desired order.

The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands. The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently placed on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct. The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the ith island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.

Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.

NoteIn the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.In the second sample, the islanders can simply move statue 1 from island 1 to island 2.In the third sample, no sequence of movements results in the desired position.

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
array_unshift($b, " ");
unset($b[0]);
array_unshift($c, " ");
unset($c[0]);
$d = array_search(0, $b);
array_splice($b, $d - 1, 1);
$e = array_search(0, $c);
array_splice($c, $e - 1, 1);
array_unshift($b, " ");
unset($b[0]);
array_unshift($c, " ");
unset($c[0]);
$f = $b[1];
$g = array_search($f, $c);
$h = array_slice($c, 0, $g - 1);
$i = array_slice($c, $g - 1);
$j = array_merge($i, $h);
$k = array_slice($b, 0);
$l = 0;
for($x = 0; $x < $a; $x++)
{
     if($j[$x] != $k[$x])
     {
          $l = 1;
          break;
     }
}
if($l == 0)
{
     print "YES";
}
else
{
     print "NO";
}
?>


You are given two strings $$$s$$$ and $$$t$$$, both consisting only of lowercase Latin letters.The substring $$$s[l..r]$$$ is the string which is obtained by taking characters $$$s_l, s_{l + 1}, \dots, s_r$$$ without changing the order.Each of the occurrences of string $$$a$$$ in a string $$$b$$$ is a position $$$i$$$ ($$$1 \le i \le |b| - |a| + 1$$$) such that $$$b[i..i + |a| - 1] = a$$$ ($$$|a|$$$ is the length of string $$$a$$$).You are asked $$$q$$$ queries: for the $$$i$$$-th query you are required to calculate the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.

The first line contains three integer numbers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 10^3$$$, $$$1 \le q \le 10^5$$$) — the length of string $$$s$$$, the length of string $$$t$$$ and the number of queries, respectively. The second line is a string $$$s$$$ ($$$|s| = n$$$), consisting only of lowercase Latin letters. The third line is a string $$$t$$$ ($$$|t| = m$$$), consisting only of lowercase Latin letters. Each of the next $$$q$$$ lines contains two integer numbers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the arguments for the $$$i$$$-th query.

Print $$$q$$$ lines — the $$$i$$$-th line should contain the answer to the $$$i$$$-th query, that is the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.

NoteIn the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.


You are given array a with n integers and m queries. The i-th query is given with three integers li, ri, xi.For the i-th query find any position pi (li ≤ pi ≤ ri) so that api ≠ xi.

The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of elements in a and the number of queries. The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a. Each of the next m lines contains three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106) — the parameters of the i-th query.

Print m lines. On the i-th line print integer pi — the position of any number not equal to xi in segment [li, ri] or the value  - 1 if there is no such number.

<?php

    class Utils {

        public static function lowerBound($a, $l, $r, $k) {
            $ans = $r; $m = 0;
            while ($l < $r) {
                $m = $l + $r >> 1;
                if ($a[$m] >= $k) $ans = $r = $m; else $l = $m + 1;
            }
            return $ans;
        }

        public static function upperBound($a, $l, $r, $k) {
            $ans = $r; $m = 0;
            while ($l < $r) {
                $m = $l + $r >> 1;
                if ($a[$m] > $k) $ans = $r = $m; else $l = $m + 1;
            }
            return $ans;
        }
    }

    list($n, $m) = explode(' ', trim(fgets(STDIN)));
    $a = explode(' ', trim(fgets(STDIN)));
    $before = array_fill(1, $n, 0);
    $before[$n] = -1;
    for ($i = $n - 1; $i > 0; $i--) {

      $before[$i] = $a[$i] != $a[$i - 1] ? $i + 1 : $before[$i + 1];
    }

    $map = array();
    for ($i = 0; $i < $n; $i++) {
        $v = $a[$i];
        $map[$v][] = $i + 1;
    }

    $ans = "";
    while ($m-- > 0) {
        list($l, $r, $k) = explode(' ', trim(fgets(STDIN)));

        if (isset($map[$k])) {
            $cur = $map[$k];
            $size = count($cur);
            $x = Utils :: lowerBound($cur, 0, $size, $l);
            $y = Utils :: upperBound($cur, 0, $size, $r) - 1;
            if ($x == $size || $cur[$x] > $l) $ans .= "$l" . "\n";
            else $ans .= ($before[$l] > $r ? '-1' : "$before[$l]") . "\n";

        } else $ans .= "$l" . "\n";
    }
    echo $ans;

<?php

    class Utils {

        public static function lowerBound($a, $l, $r, $k) {
            $ans = $r; $m = 0;
            while ($l < $r) {
                $m = $l + $r >> 1;
                if ($a[$m] >= $k) $ans = $r = $m; else $l = $m + 1;
            }
            return $ans;
        }

        public static function upperBound($a, $l, $r, $k) {
            $ans = $r; $m = 0;
            while ($l < $r) {
                $m = $l + $r >> 1;
                if ($a[$m] > $k) $ans = $r = $m; else $l = $m + 1;
            }
            return $ans;
        }
    }

    list($n, $m) = explode(' ', trim(fgets(STDIN)));
    $a = explode(' ', trim(fgets(STDIN)));
    $before = array_fill(1, $n, 0);
    $before[$n] = -1;
    for ($i = $n - 1; $i > 0; $i--) {
      $before[$i] = $a[$i] != $a[$i - 1] ? $i + 1 : $before[$i + 1];
    }

    $map = array();
    for ($i = 0; $i < $n; $i++) {
        $map[$a[$i]][] = $i + 1;
    }

    $ans = "";
    while ($m-- > 0) {
        list($l, $r, $k) = explode(' ', trim(fgets(STDIN)));

        if (isset($map[$k])) {
            $cur = &$map[$k];
            $size = count($cur);
            $x = Utils :: lowerBound($cur, 0, $size, $l);
            $y = Utils :: upperBound($cur, 0, $size, $r) - 1;
            if ($x == $size || $cur[$x] > $l) $ans .= "$l" . "\n";
            else $ans .= ($before[$l] > $r ? '-1' : "$before[$l]") . "\n";

        } else $ans .= "$l" . "\n";
    }
    echo $ans;

<?php

    class Utils {

        public static function lowerBound($a, $l, $r, $k) {
            $ans = $r; $m = 0;
            while ($l < $r) {
                $m = $l + $r >> 1;
                if ($a[$m] >= $k) $ans = $r = $m; else $l = $m + 1;
            }
            return $ans;
        }

        public static function upperBound($a, $l, $r, $k) {
            $ans = $r; $m = 0;
            while ($l < $r) {
                $m = $l + $r >> 1;
                if ($a[$m] > $k) $ans = $r = $m; else $l = $m + 1;
            }
            return $ans;
        }
    }

    list($n, $m) = explode(' ', trim(fgets(STDIN)));
    $a = explode(' ', trim(fgets(STDIN)));
    $before = array_fill(1, $n, 0);
    $before[$n] = -1;
    for ($i = $n - 1; $i > 0; $i--) {

      $before[$i] = $a[$i] != $a[$i - 1] ? $i + 1 : $before[$i + 1];
    }

//   print_r($before);

    $map = array();
    for ($i = 0; $i < $n; $i++) {
        $v = $a[$i];
        $map[$v][] = $i + 1;
    }

//    print_r($map);

    $ans = "";
    while ($m-- > 0) {
        list($l, $r, $k) = explode(' ', trim(fgets(STDIN)));

        if (isset($map[$k])) {
            $cur = $map[$k];
            $size = count($cur);
            $x = Utils :: lowerBound($cur, 0, $size, $l);
            $y = Utils :: upperBound($cur, 0, $size, $r) - 1;
//           echo $size . " " . $k . " " . $x . " " . $y . "\n";
            if ($x == $size || $cur[$x] > $l) $ans .= "$l" . "\n";
            elseif ($y - $x == $r - $l) $ans .= '-1' . "\n";
            else $ans .= ($before[$l] > $r ? '-1' : "$before[$l]") . "\n";

        } else $ans .= "$l" . "\n";
    }
    echo $ans;

<?php

    class Utils {

        public static function lowerBound($a, $l, $r, $k) {
            $ans = $r; $m = 0;
            while ($l < $r) {
                $m = $l + $r >> 1;
                if ($a[$m] >= $k) $ans = $r = $m; else $l = $m + 1;
            }
            return $ans;
        }

        public static function upperBound($a, $l, $r, $k) {
            $ans = $r; $m = 0;
            while ($l < $r) {
                $m = $l + $r >> 1;
                if ($a[$m] > $k) $ans = $r = $m; else $l = $m + 1;
            }
            return $ans;
        }
    }

    list($n, $m) = explode(' ', trim(fgets(STDIN)));
    $a = explode(' ', trim(fgets(STDIN)));
    $before = array_fill(1, $n, 0);
    $before[$n] = -1;
    for ($i = $n - 1; $i > 0; $i--) {

      $before[$i] = $a[$i] != $a[$i - 1] ? $i + 1 : $before[$i + 1];
    }

    $map = array();
    for ($i = 0; $i < $n; $i++) {
        $v = $a[$i];
        $map[$v][] = $i + 1;
    }

    while ($m-- > 0) {
        list($l, $r, $k) = explode(' ', trim(fgets(STDIN)));

        if (isset($map[$k])) {
            $cur = $map[$k];
            $size = count($cur);
            $x = Utils :: lowerBound($cur, 0, $size, $l);
            $y = Utils :: upperBound($cur, 0, $size, $r) - 1;
            if ($x == $size || $cur[$x] > $l) echo "$l" . "\n";
            else echo $before[$l] > $r ? '-1' : "$before[$l]" . "\n";

        } else echo "$l" . "\n";
    }


The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.Kirk has a binary string $$$s$$$ (a string which consists of zeroes and ones) of length $$$n$$$ and he is asking you to find a binary string $$$t$$$ of the same length which satisfies the following conditions: For any $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) the length of the longest non-decreasing subsequence of the substring $$$s_{l}s_{l+1} \ldots s_{r}$$$ is equal to the length of the longest non-decreasing subsequence of the substring $$$t_{l}t_{l+1} \ldots t_{r}$$$; The number of zeroes in $$$t$$$ is the maximum possible.A non-decreasing subsequence of a string $$$p$$$ is a sequence of indices $$$i_1, i_2, \ldots, i_k$$$ such that $$$i_1 < i_2 < \ldots < i_k$$$ and $$$p_{i_1} \leq p_{i_2} \leq \ldots \leq p_{i_k}$$$. The length of the subsequence is $$$k$$$.If there are multiple substrings which satisfy the conditions, output any.

The first line contains a binary string of length not more than $$$10^5$$$.

Output a binary string which satisfied the above conditions. If there are many such strings, output any of them.

NoteIn the first example:  For the substrings of the length $$$1$$$ the length of the longest non-decreasing subsequnce is $$$1$$$;  For $$$l = 1, r = 2$$$ the longest non-decreasing subsequnce of the substring $$$s_{1}s_{2}$$$ is $$$11$$$ and the longest non-decreasing subsequnce of the substring $$$t_{1}t_{2}$$$ is $$$01$$$;  For $$$l = 1, r = 3$$$ the longest non-decreasing subsequnce of the substring $$$s_{1}s_{3}$$$ is $$$11$$$ and the longest non-decreasing subsequnce of the substring $$$t_{1}t_{3}$$$ is $$$00$$$;  For $$$l = 2, r = 3$$$ the longest non-decreasing subsequnce of the substring $$$s_{2}s_{3}$$$ is $$$1$$$ and the longest non-decreasing subsequnce of the substring $$$t_{2}t_{3}$$$ is $$$1$$$; The second example is similar to the first one.

<?php

function kirksFunction($binaryString) {
    $zeros = 0;
    $ones = 0;
    for ($i = strlen($binaryString)-1; $i >= 0; $i--) {
        if ($binaryString[$i] == '0') {
            $zeros++;
        }
        else {
            $ones++;
            if ($ones > $zeros) {
                $binaryString[$i] = '0';
                $ones--;
            }
        }
    }
    return $binaryString;
}

function main() {
    $binaryString = readline();
    echo kirksFunction($binaryString);
}

main();
?>

<?php

function kirksFunction($binaryString) {
    // Keep track of zeros and ones in sequence
    $zeros = 0;
    $ones = 0;
    // Iteration starts at the end. If x = 1 then there are two cases.
    // LIS starts from 1, then x = 0 will not affect LIS length
    // LIS doesn't starts from 1, then x = 0 will affect LIS length
    //
    // If LIS ends with 1 then iterating from back we change change 1 to 0.
    // Assume that LIS ends with 1, then the number of zeros after 1 is bigger or equal than the number of ones.
    // This means that similar LIS can be obtain from zeros.
    // If $ones > $zeros it means that we can change LIS containing ones to LIS containing zeros.
    for ($i = strlen($binaryString)-1; $i >= 0; $i--) {
        if ($binaryString[$i] == '0') {
            $zeros++;
        }
        else {
            $ones++;
            if ($ones > $zeros) {
                $binaryString[$i] = '0';
                $ones--;
            }
        }
    }
    return $binaryString;
}

function main() {
    $binaryString = readline();
    echo kirksFunction($binaryString);
}

main();
?>


You are given three integers $$$n$$$, $$$l$$$, and $$$r$$$. You need to construct an array $$$a_1,a_2,\dots,a_n$$$ ($$$l\le a_i\le r$$$) such that $$$\gcd(i,a_i)$$$ are all distinct or report there's no solution.Here $$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line contains three integers $$$n$$$, $$$l$$$, $$$r$$$ ($$$1 \le n \le 10^5$$$, $$$1\le l\le r\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, if there is no solution, print "NO" (without quotes). You can print letters in any case (upper or lower). Otherwise, print "YES" (without quotes). In the next line, print $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ — the array you construct. If there are multiple solutions, you may output any.

<?php

$t = (integer) readline();
while($t--)
{
    $line = readline();
    $line = explode(' ', $line);
    $n = (integer) $line[0];
    $l = (integer) $line[1];
    $r = (integer) $line[2];
    $flag = 1;
    $ans = [];
    for($i = 1;$i <= $n;$i++)
    {
        $x = ($l % $i == 0 ? $l : $l + ($i - ($l % $i)));
        $x = ($i >= $l ? $i : $x);
        if($x > $r)
        {
            $flag = 0;
            break;
        }
        array_push($ans, $x);
    }
    if(!$flag)
    {
        echo "NO\n";
    }
    else
    {
        echo "YES\n";
        foreach($ans as $c)
        {
            echo $c . ' ';
        }
        echo "\n";
    }
}

<?php

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

function readInt() {
  return (int) read() ;
}

function readStrs() {
  return explode(" ",read()) ;
}

function readIntArray() {
  return array_map('intval', readStrs());
}

function readAsArray() {
  return str_split(read()) ;
}

function printLine($arg) {
  if($arg === false) {
    print_r("false") ;
  }
  elseif($arg === true) {
    print_r("true") ;
  }
  else {
    print_r($arg) ;
  }
  print_r("\n") ;
}

$t = readInt() ;

while($t--) {
	$ans = [] ;
	[$n,$l,$h] = readIntArray() ;
	for ($i=1; $i <= $n; $i++) {
		$temp = (int) ($h / $i) ;
		// printLine($temp);
		$ele = $temp*$i ;
		if($h >= $ele && $l <= $ele) $ans[] = $ele ;
	}
	// printLine($ans);
	if(count($ans) < $n) {
		printLine("NO") ;
	}
	else {
		printLine("YES");
		printLine(implode(' ', $ans));
	}
}

<?php

$t = (integer) readline();
while($t--)
{
    $line = readline();
    $line = explode(' ', $line);
    $n = (integer) $line[0];
    $l = (integer) $line[1];
    $r = (integer) $line[2];
    $flag = 1;
    $ans = [];
    for($i = 1;$i <= $n;$i++)
    {
        $x = ($l % $i == 0 ? $l : $l + ($l % $i));
        $x = ($i >= $l ? $i : $x);
        if($x > $r)
        {
            $flag = 0;
            break;
        }
        array_push($ans, $x);
    }
    if(!$flag)
    {
        echo "NO\n";
    }
    else
    {
        echo "YES\n";
        foreach($ans as $c)
        {
            echo $c . ' ';
        }
        echo "\n";
    }
}

<?php

$t = (integer) readline();
while($t--)
{
    $line = readline();
    $line = explode(' ', $line);
    $n = (integer) $line[0];
    $l = (integer) $line[1];
    $r = (integer) $line[2];
    if($r - $l + 1 < $n)
    {
        echo "NO\n";
        continue;
    }
    echo "YES\n";
    $mod = 0;
    if($l <= $n)
    {
        $mod = $n;
    }
    else
    {
        if($l % $n == 0)
        {
            $mod = $l;
        }
        else
        {
            $mod = (((integer)($l / $n)) + 1) * $n;
        }
    }
    for($i = $mod + ($n - ($mod - $l + 1));$i > $mod;$i--)
    {
        echo $i . ' ';
    }
    while($l <= $mod)
    {
        echo $l . ' ';
        $l++;
    }
    echo "\n";
}

<?php

$t = (integer) readline();
while($t--)
{
    $line = readline();
    $line = explode(' ', $line);
    $n = (integer) $line[0];
    $l = (integer) $line[1];
    $r = (integer) $line[2];
    if($r - $l + 1 < $n)
    {
        echo "NO\n";
        continue;
    }
    echo "YES\n";
    while($n--)
    {
        echo $l . ' ';
        $l++;
    }
    echo "\n";
}


Recently n students from city S moved to city P to attend a programming camp.They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea.i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it).

The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every  condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1.

For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea.

NoteThe example contains 2 tests:  During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second.  During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea.


Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops. Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output one integer — the number of pairs of bishops which attack each other.

NoteIn the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.

<?php

function nOfPairs($v)
{
	return ($v * ($v - 1)) / 2;
}

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

$pd = array( );
$md = array( );

for($cnt = 0; $cnt < $n; $cnt++)
{
	fscanf(STDIN, "%d%d", $x, $y);
	$pd[$x - $y]++;
	$md[$x - ($n - $y)]++;
}

foreach ($pd as $k => $v) {
	$sum += nOfPairs($v);
}

foreach ($md as $k => $v) {
	$sum += nOfPairs($v);
}

echo $sum;

?>

<?php
$n = (int)fgets(STDIN);
$fwdDiagCount = $backDiagCount = [];

for ($i = 0; $i < $n; $i++) {
  list ($x, $y) = explode(" ", trim(fgets(STDIN)));
  @$fwdDiagCount[$y - $x]++;
  @$backDiagCount[$x + $y]++;
}

$pairCount = 0;

foreach ($fwdDiagCount as $count) {
  if ($count > 1) {
    $pairCount += $count * ($count - 1) / 2;
  }
}

foreach ($backDiagCount as $count) {
  if ($count > 1) {
    $pairCount += $count * ($count - 1) / 2;
  }
}

echo "$pairCount\n";


Once again, Boris needs the help of Anton in creating a task. This time Anton needs to solve the following problem:There are two arrays of integers $$$a$$$ and $$$b$$$ of length $$$n$$$. It turned out that array $$$a$$$ contains only elements from the set $$$\{-1, 0, 1\}$$$.Anton can perform the following sequence of operations any number of times:   Choose any pair of indexes $$$(i, j)$$$ such that $$$1 \le i < j \le n$$$. It is possible to choose the same pair $$$(i, j)$$$ more than once.   Add $$$a_i$$$ to $$$a_j$$$. In other words, $$$j$$$-th element of the array becomes equal to $$$a_i + a_j$$$. For example, if you are given array $$$[1, -1, 0]$$$, you can transform it only to $$$[1, -1, -1]$$$, $$$[1, 0, 0]$$$ and $$$[1, -1, 1]$$$ by one operation.Anton wants to predict if it is possible to apply some number (zero or more) of these operations to the array $$$a$$$ so that it becomes equal to array $$$b$$$. Can you help him?

Each test contains multiple test cases.  The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$)  — the length of arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-1 \le a_i \le 1$$$)  — elements of array $$$a$$$. There can be duplicates among elements. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$-10^9 \le b_i \le 10^9$$$)  — elements of array $$$b$$$. There can be duplicates among elements. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case, output one line containing "YES" if it's possible to make arrays $$$a$$$ and $$$b$$$ equal by performing the described operations, or "NO" if it's impossible. You can print each letter in any case (upper or lower).

NoteIn the first test-case we can choose $$$(i, j)=(2, 3)$$$ twice and after that choose $$$(i, j)=(1, 2)$$$ twice too. These operations will transform $$$[1, -1, 0] \to [1, -1, -2] \to [1, 1, -2]$$$In the second test case we can't make equal numbers on the second position.In the third test case we can choose $$$(i, j)=(1, 2)$$$ $$$41$$$ times. The same about the fourth test case.In the last lest case, it is impossible to make array $$$a$$$ equal to the array $$$b$$$.

<?php
    define("MAXN", 1024) ;
    define("GAP_VAL", 1e-7) ;

    function gap($n)
    {
        return $n < GAP_VAL ;
    }

    $t = intval(trim(fgets(STDIN))) ;
    while($t --)
    {
        $n = intval(trim(fgets(STDIN))) ;

        $arr_a = explode(" ", trim(fgets(STDIN))) ;
        $arr_b = explode(" ", trim(fgets(STDIN))) ;

        $f = array(
            array_fill(0, $n, false),  // 1
            array_fill(0, $n, false)   // -1
        ) ;

        for($i = 0; $i < $n; $i ++)
        {
            $arr_a[$i] = intval($arr_a[$i]) ;
            $arr_b[$i] = intval($arr_b[$i]) ;

            if($i > 0)
            {
                $f[0][$i] = $f[0][$i - 1] ;
                $f[1][$i] = $f[1][$i - 1] ;

                if($arr_a[$i - 1] == 1) $f[0][$i] = true ;
                else if($arr_a[$i - 1] == -1) $f[1][$i] = true ;
            }
        }

        if($arr_a[0] != $arr_b[0]) echo "NO\n" ;
        else
        {
            for($i = $n - 1; $i >= 1; $i --)
            {
                if($arr_b[$i] > $arr_a[$i] && !$f[0][$i]) break ;
                if($arr_b[$i] < $arr_a[$i] && !$f[1][$i]) break ;
            }

            echo $i >= 1 ? "NO\n" : "YES\n" ;
        }
    }
?>

<?php
    $j = trim(fgets(STDIN));
    for($l = 0; $l < $j; $l++){
        $c = trim(fgets(STDIN));
        $a = explode(" ", trim(fgets(STDIN)));
        $b = explode(" ", trim(fgets(STDIN)));
        $bup = false;
        $bdown = false;

        foreach($a as $k => $v){

            if($bup && $bdown){
                echo "YES\n";
                continue 2;
            }
            if($v < $b[$k] && $bup == false){
                echo "NO\n";
                continue 2;
            }
            if($v > $b[$k] && $bdown == false){
                echo "NO\n";
                continue 2;
            }
            if($v == -1){
                $bdown = true;
            }
            if($v == 1){
                $bup = true;
            }
        }
        echo "YES\n";
    }

?>


Let's define $$$S(x)$$$ to be the sum of digits of number $$$x$$$ written in decimal system. For example, $$$S(5) = 5$$$, $$$S(10) = 1$$$, $$$S(322) = 7$$$.We will call an integer $$$x$$$ interesting if $$$S(x + 1) < S(x)$$$. In each test you will be given one integer $$$n$$$. Your task is to calculate the number of integers $$$x$$$ such that $$$1 \le x \le n$$$ and $$$x$$$ is interesting.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — number of test cases. Then $$$t$$$ lines follow, the $$$i$$$-th line contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) for the $$$i$$$-th test case.

Print $$$t$$$ integers, the $$$i$$$-th should be the answer for the $$$i$$$-th test case.

NoteThe first interesting number is equal to $$$9$$$.

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

$n = trim(fgets($file));

for ($i = 0; $i < $n; $i++) {
  $a = trim(fgets($file));

  if ($a < 9) { echo '0'.PHP_EOL; }
  else {
    if ($a == 9) { echo '1'.PHP_EOL; }
    else {
      $ans = floor($a / 10);
      if ($a[strlen($a) - 1] == 9) { $ans += 1; }
      echo $ans.PHP_EOL;
    }
  }
}

?>


You are given a positive integer $$$x$$$. Check whether the number $$$x$$$ is representable as the sum of the cubes of two positive integers.Formally, you need to check if there are two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b$$$) such that $$$a^3+b^3=x$$$.For example, if $$$x = 35$$$, then the numbers $$$a=2$$$ and $$$b=3$$$ are suitable ($$$2^3+3^3=8+27=35$$$). If $$$x=4$$$, then no pair of numbers $$$a$$$ and $$$b$$$ is suitable.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case contains one integer $$$x$$$ ($$$1 \le x \le 10^{12}$$$). Please note, that the input for some test cases won't fit into $$$32$$$-bit integer type, so you should use at least $$$64$$$-bit integer type in your programming language.

For each test case, output on a separate line:    "YES" if $$$x$$$ is representable as the sum of the cubes of two positive integers.  "NO" otherwise.  You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

NoteThe number $$$1$$$ is not representable as the sum of two cubes.The number $$$2$$$ is represented as $$$1^3+1^3$$$.The number $$$4$$$ is not representable as the sum of two cubes.The number $$$34$$$ is not representable as the sum of two cubes.The number $$$35$$$ is represented as $$$2^3+3^3$$$.The number $$$16$$$ is represented as $$$2^3+2^3$$$.The number $$$703657519796$$$ is represented as $$$5779^3+7993^3$$$.

<?php

$times = (int) trim(fgets(STDIN));
for ($idx = 0; $idx < $times; ++$idx) {
    $ni[] = trim(fgets(STDIN));
}
for ($idx = 0; $idx < $times; ++$idx) {
    $n = &$ni[$idx];
    $a = ceil(pow($n / 2, 1 / 3));
    for ($i = 1; $i <= $a; ++$i) {
        $b = $n - $i * $i * $i;
        if ($b < $i * $i * $i) {
            break;
        }
        $f = floor(pow($b, 1 / 3));
        for ($j = $f;; ++$j) {
            $j3 = $j * $j * $j;
            if ($b == $j3) {
                echo 'YES'.PHP_EOL;
                continue 3;
            } elseif ($b < $j3) {
                break;
            }
        }
    }
    echo 'NO'.PHP_EOL;
}

<?php

$times = (int) trim(fgets(STDIN));
for ($idx = 0; $idx < $times; ++$idx) {
    $ni[] = (int) trim(fgets(STDIN));
}
for ($idx = 0; $idx < $times; ++$idx) {
    $n = &$ni[$idx];
    $a = ceil(pow($n / 2, 1 / 3));
    for ($i = 1; $i <= $a; ++$i) {
        $b = $n - $i * $i * $i;
        if ($b < $i * $i * $i) {
            break;
        }
        $f = floor(pow($b, 1 / 3));
        for ($j = $f;; ++$j) {
            $j3 = $j * $j * $j;
            if ($b == $j3) {
                echo 'YES'.PHP_EOL;
                continue 3;
            } elseif ($b < $j3) {
                break;
            }
        }
    }
    echo 'NO'.PHP_EOL;
}


Petya has a rectangular Board of size $$$n \times m$$$. Initially, $$$k$$$ chips are placed on the board, $$$i$$$-th chip is located in the cell at the intersection of $$$sx_i$$$-th row and $$$sy_i$$$-th column.In one action, Petya can move all the chips to the left, right, down or up by $$$1$$$ cell.If the chip was in the $$$(x, y)$$$ cell, then after the operation:   left, its coordinates will be $$$(x, y - 1)$$$;  right, its coordinates will be $$$(x, y + 1)$$$;  down, its coordinates will be $$$(x + 1, y)$$$;  up, its coordinates will be $$$(x - 1, y)$$$. If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.Note that several chips can be located in the same cell.For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.Since Petya does not have a lot of free time, he is ready to do no more than $$$2nm$$$ actions.You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in $$$2nm$$$ actions.

The first line contains three integers $$$n, m, k$$$ ($$$1 \le n, m, k \le 200$$$) — the number of rows and columns of the board and the number of chips, respectively. The next $$$k$$$ lines contains two integers each $$$sx_i, sy_i$$$ ($$$ 1 \le sx_i \le n, 1 \le sy_i \le m$$$) — the starting position of the $$$i$$$-th chip. The next $$$k$$$ lines contains two integers each $$$fx_i, fy_i$$$ ($$$ 1 \le fx_i \le n, 1 \le fy_i \le m$$$) — the position that the $$$i$$$-chip should visit at least once.

In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once. In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters $$$L, R, D, U$$$ respectively. If the required sequence does not exist, print -1 in the single line.


The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it.The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.

The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.

Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.

NoteIn the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: .

<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d %d %d %d", $x, $y, $x0, $y0);
$cmd = trim(fgets($handle));
fclose($handle);
//echo $cmd, "\n";
//echo strlen($command), "\n";

$visited = array_fill(1, $x, array_fill(1, $y, false));
$t = $x * $y;

$visited[$x0][$y0] = true;
//echo $x0, " ", $y0, "\n";
echo "1 ";
$t--;

for($i = 0; $i < strlen($cmd); $i++) {
	//echo $cmd[$i], "\n";
	switch($cmd[$i]) {
		case "U":
			$x0 = ($x0 > 1) ? $x0 - 1 : $x0;
			break;
		case"D":
			$x0 = ($x0 < $x ) ? $x0 + 1 : $x0;
			break;
		case"L":
			$y0 = ($y0 > 1) ? $y0 - 1 : $y0;
			break;
		case "R":
			$y0 = ($y0 < $y ) ? $y0 + 1 : $y0;
			break;
	}
	//echo $x0, " ", $y0, "\n";
	if($i == strlen($cmd) - 1) { // last step
		echo $t, "\n";
	} else {
		if($visited[$x0][$y0]) {
			echo "0 ";
		} else {
			echo "1 ";
			$t --;
		}
	}
	$visited[$x0][$y0] = true;
}
?>

<?php
fscanf(STDIN, "%d %d %d %d", $x, $y, $x0, $y0);
$temp = str_split(trim(fgets(STDIN)));
$len = count($temp);
$ans = array_fill(0, $len + 1, 0);
$visit = array_fill(1, $x * $y, 0);
$step = 0;
$ans[0] = 1;
$visit[($x0-1)*$y+$y0] = 1;
foreach ($temp as $ch) {
    $step ++;
    switch ($ch) {
        case 'U':
            if ($x0 > 1) $x0 --;
            break;
        case 'R':
            if ($y0 < $y) $y0 ++;
            break;
        case 'D':
            if ($x0 < $x) $x0 ++;
            break;
        case 'L':
            if ($y0 > 1) $y0 --;
    }
    $tmp = ($x0 - 1) * $y + $y0;
    if ($visit[$tmp]==0) {
        $visit[$tmp] = 1;
        $ans[$step] = 1;
    }
}
$ans[$len] += $x * $y - array_sum($visit);
for ($i = 0;$i <= $len;$i ++) {
    if($i > 0) echo " ";
    echo $ans[$i];
}
echo "\n";
?>

<?php
fscanf(STDIN, "%d %d %d %d", $x, $y, $x0, $y0);
$temp = str_split(fgets(STDIN));
$len = count($temp);
$ans = array_fill(0, $len, 0);
$visit = array_fill(1, $x * $y, 0);
$step = 0;
$ans[0] = 1;
$visit[($x0-1)*$y+$y0] = 1;
foreach ($temp as $ch) {
    $step ++;
    switch ($ch) {
        case 'U':
            if ($x0 > 1) $x0 --;
            break;
        case 'R':
            if ($y0 < $y) $y0 ++;
            break;
        case 'D':
            if ($x0 < $x) $x0 ++;
            break;
        case 'L':
            if ($y0 > 1) $y0 --;
    }
    $tmp = ($x0 - 1) * $y + $y0;
    if ($visit[$tmp]==0) {
        $visit[$tmp] = 1;
        $ans[$step] = 1;
    }
}
$ans[$len - 1] += $x * $y - array_sum($visit);
for ($i = 0;$i < $len;$i ++) {
    if($i > 0) echo " ";
    echo $ans[$i];
}
echo "\n";
?>


You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for all i (l + 2 ≤ i ≤ r).Let's define len([l, r]) = r - l + 1, len([l, r]) is the length of the segment [l, r]. Segment [l1, r1], is longer than segment [l2, r2], if len([l1, r1]) > len([l2, r2]).Your task is to find a good segment of the maximum length in array a. Note that a segment of length 1 or 2 is always good.

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains integers: a1, a2, ..., an (0 ≤ ai ≤ 109).

Print the length of the longest good segment in array a.

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}
$input = explode("\r\n", $input);
$input[1]=explode(" ", $input[1]);
$n=$input[0];
$max=$res=2;
for($i=2;$i<$n;$i++) {
    if ($input[1][$i-2]+$input[1][$i-1] == $input[1][$i]) $res++;
    else {
        if ($res > $max) {
            $max=$res;
        }
        $res=2;
    }
}
if ($res > $max) {
    $max=$res;
}
echo $max<count($input[1])?$max:count($input[1]);
?>

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}
$input = explode("\r\n", $input);
$input[1]=explode(" ", $input[1]);
$n=$input[0];
$max=$res=2;
for($i=2;$i<=$n;$i++) {
    if ($input[1][$i-2]+$input[1][$i-1] == $input[1][$i]) $res++;
    else {
        if ($res > $max) {
            $max=$res;
        }
        $res=2;
    }
}
echo $max;
?>

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}
$input = explode("\r\n", $input);
$input[1]=explode(" ", $input[1]);
$n=$input[0];
$max=$res=2;
for($i=2;$i<=$n;$i++) {
    if ($input[1][$i-2]+$input[1][$i-1] == $input[1][$i]) $res++;
    else {
        if ($res > $max) {
            $max=$res;
        }
        $res=2;
    }
}

echo $max<count($input[1])?$max:count($input[1]);
?>


Sayaka Saeki is a member of the student council, which has $$$n$$$ other members (excluding Sayaka). The $$$i$$$-th member has a height of $$$a_i$$$ millimeters.It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.A pair of two consecutive members $$$u$$$ and $$$v$$$ on a line is considered photogenic if their average height is an integer, i.e. $$$\frac{a_u + a_v}{2}$$$ is an integer.Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 500$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$)  — the number of other council members. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$)  — the heights of each of the other members in millimeters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.

For each test case, output on one line $$$n$$$ integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.

NoteIn the first test case, there is one photogenic pair: $$$(1, 1)$$$ is photogenic, as $$$\frac{1+1}{2}=1$$$ is integer, while $$$(1, 2)$$$ isn't, as $$$\frac{1+2}{2}=1.5$$$ isn't integer.In the second test case, both pairs are photogenic.

<?php
class IO {
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
}

$n = IO::str();
$res = array();
while ($n--) {
    $t = IO::str();
    $arr = IO::arr();
    $chet = array();
    $nchet = array();

    foreach ($arr as $ar) {
        if ($ar % 2 == 0) {
            $chet[] = $ar;
        } else {
            $nchet[] = $ar;
        }
    }

    $res[] = array_merge($chet, $nchet);
}

foreach ($res as $r) {
    foreach ($r as $z) {
        echo $z . ' ';
    }
    echo "\n";
}

<?php
class IO {
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
}

$iQueryCount = 1;
$iQueryCount = IO::str();

while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    $n = IO::str();
    $a = IO::arr();
    $b = $c = [];
    $count = 0;
    foreach ($a as $v) {
        if ($v % 2 == 0) {
            $c[$count] = $v;
        } else {
            $b[$count] = $v;
        }
        $count++;
    }
    $res = array_merge($b + $c);
    IO::line(implode(' ', $res));

}

?>


You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$).

For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise.

NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.

<?php
    $j = trim(fgets(STDIN));
    for($i = 0; $i < $j; $i++){
        $n = trim(fgets(STDIN));
        $a = explode(' ', trim(fgets(STDIN)));
        $bo = false;
        foreach($a as $k => $v){
            for($x = $k+2; $x < $n; $x++){
                if($a[$x] == $v){
                    $bo = true;
                    break 2;
                }
            }
        }

        if($bo){
            echo "YES\n";
        } else {
            echo "NO\n";
        }

    }

<?php

	function find($a) {
		for ($i = 0; $i < count($a) - 2; $i++) {
			$ch = $a[$i];
			for ($j = $i + 2; $j < count($a); $j++) {
				if ($a[$j] == $ch)
					return true;
			}
		}
		return false;
	}

	$t = trim(fgets(STDIN));

	$ans = '';

	for ($o = 0; $o < $t; $o++) {
		$n = trim(fgets(STDIN));
		$a = explode(" ", trim(fgets(STDIN)));

		$ans .= find($a) ? "YES\n" : "NO\n";

	}
	print $ans;
?>

<?php

	function find($a) {
		for ($i = 0; $i < count($a)/2; $i++) {
			$ch = $a[$i];
			for ($j = count($a) - 1; $j >= count($a)/2; $j--) {
				if ($a[$j] == $ch && abs($i - $j) > 1 )
					return true;
			}
		}
		return false;
	}

	$t = trim(fgets(STDIN));

	$ans = '';

	for ($o = 0; $o < $t; $o++) {
		$n = trim(fgets(STDIN));
		$a = explode(" ", trim(fgets(STDIN)));

		$ans .= find($a) ? "YES\n" : "NO\n";

	}
	print $ans;
?>


Recently, Tokitsukaze found an interesting game. Tokitsukaze had $$$n$$$ items at the beginning of this game. However, she thought there were too many items, so now she wants to discard $$$m$$$ ($$$1 \le m \le n$$$) special items of them.These $$$n$$$ items are marked with indices from $$$1$$$ to $$$n$$$. In the beginning, the item with index $$$i$$$ is placed on the $$$i$$$-th position. Items are divided into several pages orderly, such that each page contains exactly $$$k$$$ positions and the last positions on the last page may be left empty.Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded.    Consider the first example from the statement: $$$n=10$$$, $$$m=4$$$, $$$k=5$$$, $$$p=[3, 5, 7, 10]$$$. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices $$$3$$$ and $$$5$$$. After, the first page remains to be special. It contains $$$[1, 2, 4, 6, 7]$$$, Tokitsukaze discards the special item with index $$$7$$$. After, the second page is special (since it is the first page containing a special item). It contains $$$[9, 10]$$$, Tokitsukaze discards the special item with index $$$10$$$. Tokitsukaze wants to know the number of operations she would do in total.

The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le n \le 10^{18}$$$, $$$1 \le m \le 10^5$$$, $$$1 \le m, k \le n$$$) — the number of items, the number of special items to be discarded and the number of positions in each page. The second line contains $$$m$$$ distinct integers $$$p_1, p_2, \ldots, p_m$$$ ($$$1 \le p_1 < p_2 < \ldots < p_m \le n$$$) — the indices of special items which should be discarded.

Print a single integer — the number of operations that Tokitsukaze would do in total.

NoteFor the first example:  In the first operation, Tokitsukaze would focus on the first page $$$[1, 2, 3, 4, 5]$$$ and discard items with indices $$$3$$$ and $$$5$$$;  In the second operation, Tokitsukaze would focus on the first page $$$[1, 2, 4, 6, 7]$$$ and discard item with index $$$7$$$;  In the third operation, Tokitsukaze would focus on the second page $$$[9, 10]$$$ and discard item with index $$$10$$$. For the second example, Tokitsukaze would focus on the second page $$$[6, 7, 8, 9, 10]$$$ and discard all special items at once.

<?php
#1190A
$start = microtime(1);
[$n, $m, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$result = $off = 0;
$good = -1;
foreach ($arr as $i => $p) {
    if (bcdiv(bcsub($p, $off), $k) != $good) {
        $off = $i + 1;
        $good = bcdiv(bcsub($p, $off), $k);
        $result++;
    }
}
echo $result;

<?php
#1190A
$start = microtime(1);
[$n, $m, $k] = fscanf(STDIN, '%u %u %u');
$arr = explode(' ', trim(fgets(STDIN)));
$result = $off = 0;
$good = -1;
foreach ($arr as $i => $p) {
    if (bcdiv(bcsub($p, $off), $k) != $good) {
        $off = $i + 1;
        $good = bcdiv(bcsub($p, $off), $k);
        $result++;
    }
}
echo $result;


Given a positive integer $$$n$$$. Find three distinct positive integers $$$a$$$, $$$b$$$, $$$c$$$ such that $$$a + b + c = n$$$ and $$$\operatorname{gcd}(a, b) = c$$$, where $$$\operatorname{gcd}(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Description of the test cases follows. The first and only line of each test case contains a single integer $$$n$$$ ($$$10 \le n \le 10^9$$$).

For each test case, output three distinct positive integers $$$a$$$, $$$b$$$, $$$c$$$ satisfying the requirements. If there are multiple solutions, you can print any. We can show that an answer always exists.

NoteIn the first test case, $$$6 + 9 + 3 = 18$$$ and $$$\operatorname{gcd}(6, 9) = 3$$$.In the second test case, $$$21 + 39 + 3 = 63$$$ and $$$\operatorname{gcd}(21, 39) = 3$$$.In the third test case, $$$29 + 43 + 1 = 73$$$ and $$$\operatorname{gcd}(29, 43) = 1$$$.

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

function gcd($a,$b) {
  return ($a % $b) ? gcd($b,$a % $b) : $b;
}

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  $n = trim(fgets($file));

  $c = 1;
  $a = floor(($n - $c) / 2);
  $b = $n - $a - $c;

  while (gcd($a, $b) != 1) {
   $a++;
   $b--;
  }
  echo $a.' '.$b.' '.$c.PHP_EOL;
}

?>


A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $$$1$$$ to $$$9$$$ (inclusive) are round.For example, the following numbers are round: $$$4000$$$, $$$1$$$, $$$9$$$, $$$800$$$, $$$90$$$. The following numbers are not round: $$$110$$$, $$$707$$$, $$$222$$$, $$$1001$$$.You are given a positive integer $$$n$$$ ($$$1 \le n \le 10^4$$$). Represent the number $$$n$$$ as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number $$$n$$$ as a sum of the least number of terms, each of which is a round number.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing an integer $$$n$$$ ($$$1 \le n \le 10^4$$$).

Print $$$t$$$ answers to the test cases. Each answer must begin with an integer $$$k$$$ — the minimum number of summands. Next, $$$k$$$ terms must follow, each of which is a round number, and their sum is $$$n$$$. The terms can be printed in any order. If there are several answers, print any of them.

<?php

	//$input = fopen("input.txt", "r");
	//fscanf($input, "%d\n", $t);

	fscanf(STDIN, "%d\n", $t);
	while($t--){
		fscanf(STDIN, "%d\n", $n);
		$arr = array();
		$mul = 1;
		while($n){
            if($n%10)   $arr[] = $mul * ($n%10);
            $mul *= 10;
            $n /= 10;
		}
		echo count($arr) . "\n";
		foreach ($arr as $num)  echo "$num ";
		echo "\n";
	}

<?php

$t = readline();
for ($i = 0; $i < $t; ++$i) {
    $n = readline();
    $len = strlen($n);
    $result = [];
    for ($j = 0; $j < $len; ++$j) {
        if ($n[$j] == 0) {
            continue;
        }
        $result[] = $n[$j] . str_repeat('0', $len - $j - 1);
    }
    echo count($result) . "\n";
    echo implode(' ', $result) . "\n";
}

<?php

$t = readline();
for ($i = 0; $i < $t; ++$i) {
    $n = readline();
    $len = strlen($n);
    $result = [];
    for ($j = 0; $j < $len; ++$j) {
        if ($n[$j] == 0) {
            continue;
        }
        $result[] = $n[$j] . str_repeat('0', $len - $j - 1);
    }
    echo implode(' ', $result) . "\n";
}


Nikephoros and Polycarpus play rock-paper-scissors. The loser gets pinched (not too severely!).Let us remind you the rules of this game. Rock-paper-scissors is played by two players. In each round the players choose one of three items independently from each other. They show the items with their hands: a rock, scissors or paper. The winner is determined by the following rules: the rock beats the scissors, the scissors beat the paper and the paper beats the rock. If the players choose the same item, the round finishes with a draw.Nikephoros and Polycarpus have played n rounds. In each round the winner gave the loser a friendly pinch and the loser ended up with a fresh and new red spot on his body. If the round finished in a draw, the players did nothing and just played on.Nikephoros turned out to have worked out the following strategy: before the game began, he chose some sequence of items A = (a1, a2, ..., am), and then he cyclically showed the items from this sequence, starting from the first one. Cyclically means that Nikephoros shows signs in the following order: a1, a2, ..., am, a1, a2, ..., am, a1, ... and so on. Polycarpus had a similar strategy, only he had his own sequence of items B = (b1, b2, ..., bk).Determine the number of red spots on both players after they've played n rounds of the game. You can consider that when the game began, the boys had no red spots on them.

The first line contains integer n (1 ≤ n ≤ 2·109) — the number of the game's rounds. The second line contains sequence A as a string of m characters and the third line contains sequence B as a string of k characters (1 ≤ m, k ≤ 1000). The given lines only contain characters "R", "S" and "P". Character "R" stands for the rock, character "S" represents the scissors and "P" represents the paper.

Print two space-separated integers: the numbers of red spots Nikephoros and Polycarpus have.

NoteIn the first sample the game went like this:  R - R. Draw.  P - S. Nikephoros loses.  S - P. Polycarpus loses.  R - P. Nikephoros loses.  P - R. Polycarpus loses.  S - S. Draw.  R - P. Nikephoros loses. Thus, in total Nikephoros has 3 losses (and 3 red spots), and Polycarpus only has 2.

<?php

error_reporting(0);
ob_end_clean();

solve();

function winner($A, $B){
	if ($A === $B) return 0;

	if ($A === 'R' && $B === 'S') return 1;
	elseif ($A === 'S' && $B === 'P') return 1;
	elseif ($A === 'P' && $B === 'R') return 1;
	else return -1;
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$N = $Scanner->Scan();
	$A = $Scanner->Scan();
	$B = $Scanner->Scan();
	$Alen = strlen($A);
	$Blen = strlen($B);
	$A_char = str_split($A);
	$B_char = str_split($B);
	$Acount = 0;
	$Bcount = 0;
	$cycle = $Alen * $Blen;
	$n = (int)($N / $cycle);
	$m = $N % $cycle;
	for ($i = 0; $i < min($cycle, $N); $i++) {
		$temp = winner($A_char[$i % $Alen], $B_char[$i % $Blen]);
		if ($temp === -1) $Acount++;
		elseif ($temp === 1) $Bcount++;

		if ($N > $cycle) {
			if ($i === $m - 1) {
				$Ac = $Acount;
				$Bc = $Bcount;
			}
		}
	}

	if ($N > $cycle) {
		$Acount = $Acount * $n + $Ac;
		$Bcount = $Bcount * $n + $Bc;
	}

	print($Acount. " ". $Bcount);

	$Scanner->Close();
}

class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);
		$contents = str_replace(array("\r\n", "\r", "\n"), " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	public function Scan(){
		return $this->Scanners[$this->Id++];
	}

	public function Close(){
		fclose($this->Stream);
	}
}

?>

<?php

function gcf($a, $b) {
  return ( $b == 0 ) ? ($a):( gcf($b, $a % $b) );
}
function lcm($a, $b) {
  return ( $a / gcf($a,$b) ) * $b;
}

function _main() {

$n = intval(fgets(STDIN));
$line1 = trim(fgets(STDIN));
$line2 = trim(fgets(STDIN));

$line1_len = strlen($line1);
$line2_len = strlen($line2);

$lcm_len = lcm($line1_len, $line2_len);
// print "lcm: $lcm_len\n";

$res = array(1 => 0, 2 => 0);
for($i=0;$i<min($n,$lcm_len);$i++) {
  if ($val = _cmp($line1[$i % $line1_len], $line2[$i % $line2_len])) {
    $res[$val]++;
  }
  // print " = $val\n";
  // sleep(1);
}

if ($n > $lcm_len) {
  $k = intval($n / $lcm_len);
  $res[1] *= $k;
  $res[2] *= $k;

  $tail_n = $n%$lcm_len;
  // print "tail_n: $tail_n | K = $k\n";
  for($i=0;$i<$tail_n;$i++) {
    if ($val = _cmp($line1[$i % $line1_len], $line2[$i % $line2_len])) {
      $res[$val]++;
    }
    // print " = $val\n";
    // sleep(1);
  }
}

return $res;
}

function _cmp($a, $b) {
// print "cmp $a <> $b";
  if ($a == $b) return 0;
  $w = array('R' => 0,'S' => 1,'P' => 2);

  return ($w[$a] + 1) % 3 == $w[$b]
    ? 1
    : 2;
}

function _test_cmp() {
  $a = 'RRRSSSPPP';
  $b = 'RSPRSPRSP';
  for($i=0;$i<strlen($a);$i++) {
    $res = _cmp($a[$i], $b[$i]);
    // print "_cmp($a[$i], $b[$i]) = $res\n";
  }
}

// _test_cmp();

$res = _main();
print $res[2] .' '. $res[1] ."\n";

<?php

error_reporting(0);
ob_end_clean();

solve();

function winner($A, $B){
	if ($A === $B) return 0;

	if ($A === 'R' && $B === 'S') return 1;
	elseif ($A === 'S' && $B === 'P') return 1;
	elseif ($A === 'P' && $B === 'R') return 1;
	else return -1;
}

function solve(){
	$Scanner = new Scanner("php://stdin");
	//$Scanner = new Scanner("input.txt");

	$N = $Scanner->Scan();
	$A = $Scanner->Scan();
	$B = $Scanner->Scan();
	$Alen = strlen($A);
	$Blen = strlen($B);
	$A_char = str_split($A);
	$B_char = str_split($B);
	$Acount = 0;
	$Bcount = 0;
	$cycle = $Alen * $Blen;
	$n = (int)($N / $cycle);
	$m = $N % $cycle;
	for ($i = 0; $i < min($cycle, $N); $i++) {
		$temp = winner($A_char[$i % $Alen], $B_char[$i % $Blen]);
		if ($temp === -1) $Acount++;
		elseif ($temp === 1) $Bcount++;

		if ($N > $cycle) {
			if ($i === $m - 1) {
				$Ac = $Acount;
				$Bc = $Bcount;
			}
		}
	}

	if ($N > $cycle) {
		$Acount = $Acount * $n + $Ac;
		$Bcount *= $Bcount * $n + $Bc;
	}

	print($Acount. " ". $Bcount);

	$Scanner->Close();
}

class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);
		$contents = str_replace(array("\r\n", "\r", "\n"), " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	public function Scan(){
		return $this->Scanners[$this->Id++];
	}

	public function Close(){
		fclose($this->Stream);
	}
}

?>

<?php

error_reporting(0);
ob_end_clean();

solve();

function winner($A, $B){
	if ($A === $B) return 0;

	if ($A === 'R' && $B === 'S') return 1;
	elseif ($A === 'S' && $B === 'P') return 1;
	elseif ($A === 'P' && $B === 'R') return 1;
	else return -1;
}

function solve(){
	//$Scanner = new Scanner("php://stdin");
	$Scanner = new Scanner("input.txt");

	$N = $Scanner->Scan();
	$A = $Scanner->Scan();
	$B = $Scanner->Scan();
	$Alen = strlen($A);
	$Blen = strlen($B);
	$A_char = str_split($A);
	$B_char = str_split($B);
	$Acount = 0;
	$Bcount = 0;
	for ($i = 0; $i < $N; $i++) {
		$temp = winner($A_char[$i % $Alen], $B_char[$i % $Blen]);
		if ($temp === -1) $Acount++;
		elseif ($temp === 1) $Bcount++;
	}

	print($Acount. " ". $Bcount);

	$Scanner->Close();
}

class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);
		$contents = str_replace(array("\r\n", "\r", "\n"), " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	public function Scan(){
		return $this->Scanners[$this->Id++];
	}

	public function Close(){
		fclose($this->Stream);
	}
}

?>

<?php

error_reporting(0);
ob_end_clean();

solve();

function winner($A, $B){
	if ($A === 'R' && $B === 'S') return 1;
	elseif ($A === 'R' && $B === 'P') return -1;
	elseif ($A === 'S' && $B === 'R') return -1;
	elseif ($A === 'S' && $B === 'P') return 1;
	elseif ($A === 'P' && $B === 'R') return 1;
	elseif ($A === 'P' && $B === 'S') return -1;
	else return 0;
}

function solve(){
	//$Scanner = new Scanner("php://stdin");
	$Scanner = new Scanner("input.txt");

	$N = $Scanner->Scan();
	$A = $Scanner->Scan();
	$B = $Scanner->Scan();
	$Alen = strlen($A);
	$Blen = strlen($B);

	$newA = $A;
	if ($N > $Alen) {
		$n = $N / $Alen + 1;
		for ($i = 2; $i <= $n; $i++) {
			$newA .= $A;
		}
	}

	$newB = $B;
	if ($N > $Blen) {
		$n = $N / $Blen + 1;
		for ($i = 2; $i <= $n; $i++) {
			$newB .= $B;
		}
	}

	$A_char = str_split($newA);
	$B_char = str_split($newB);
	$Acount = 0;
	$Bcount = 0;
	for ($i = 0; $i < $N; $i++) {
		$temp = winner($A_char[$i], $B_char[$i]);
		if ($temp === -1) $Acount++;
		elseif ($temp === 1) $Bcount++;
	}

	print($Acount. " ". $Bcount);

	$Scanner->Close();
}

class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);
		$contents = str_replace(array("\r\n", "\r", "\n"), " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	public function Scan(){
		return $this->Scanners[$this->Id++];
	}

	public function Close(){
		fclose($this->Stream);
	}
}

?>


Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.

The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105).

Print n integers forming the permutation. If there are multiple answers, print any of them.

NoteBy |x| we denote the absolute value of number x.

<?php
$line = explode(" ", trim(fgets(STDIN)));
$length = $line[0];
$count = $line[1];
$res = array();
for ($i=0; $i<$length; $i++) {
	$res[] = $i+1;
}
if ($count != 1) {
	$counter = 1;
	while ($count>0) {
		if ($counter%2 == 0) {
			$res[$counter] = $res[$counter-1] - $count;
		} else {
			$res[$counter] = $res[$counter-1] + $count;
		}
		$count--;
		$counter++;
	}
}
echo implode(" ", $res);
?>

<?php
$input_data = explode(" ", trim(fgets(STDIN)));
$input_n = $input_data[0];
$input_k = $input_data[1];
//$input_n = 7;
//$input_k = 2;

$result = array();
for ($i=0; $i<$input_n; $i++) {
	$result[] = $i+1;
}
if ($input_k != 1) {
	$i = 1;
	while ($input_k>0) {
		if ($i%2 == 0) {
			$result[$i] = $result[$i-1] - $input_k;
		} else {
			$result[$i] = $result[$i-1] + $input_k;
		}
		$input_k--;
		$i++;
	}
}
echo implode(" ", $result);
?>