Submission #502195


Source Code Expand

<?php

function is_prime($n, $k) {
    if ($n == 2)
        return true;
    if ($n < 2 || $n % 2 == 0)
        return false;

    $d = $n - 1;
    $s = 0;

    while ($d % 2 == 0) {
        $d /= 2;
        $s++;
    }

    for ($i = 0; $i < $k; $i++) {
        $a = rand(2, $n-1);

        $x = bcpowmod($a, $d, $n);
        if ($x == 1 || $x == $n-1)
            continue;

        for ($j = 1; $j < $s; $j++) {
            $x = bcmod(bcmul($x, $x), $n);
            if ($x == 1)
                return false;
            if ($x == $n-1)
                continue 2;
        }
        return false;
    }
    return true;
}

function pc_permute($items, $perms, $count) {
    if (empty($items) || 5-$count >= count($items)) {
        $return = array($perms);
    }  else {
        $return = array();
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            $return = array_merge($return, pc_permute($newitems, $newperms, $count));
        }
    }
    return $return;
}


while(true)
{
    $stdin = trim(fgets(STDIN));
    if ($stdin === '')
        break;

    $chars = array_unique(str_split($stdin));
    $perms = pc_permute(array(1,3,5,7,9), array(), count($chars));
    $failed = true;

    if(count($chars) > 5) {
        echo "-1\n";
        continue;
    } else {
        foreach($perms as $perm){
            $num = strtr($stdin, implode('', $chars), implode('', $perm));
            if(is_prime($num, 10)) {
                $failed = false;
                echo "$num\n";
                break;
            }
        }
    }

    if($failed)
        echo "-1\n";
}

Submission Info

Submission Time
Task D - 文字列と素数
User z_kro
Language PHP (PHP 5.5.21)
Score 50
Code Size 1826 Byte
Status AC
Exec Time 75 ms
Memory 4656 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 50 / 50
Status
AC × 3
AC × 25
Set Name Test Cases
Sample 00_sample_00, 00_sample_01, 00_sample_02
All 00_sample_00, 00_sample_01, 00_sample_02, 05_rand_00, 05_rand_01, 05_rand_02, 05_rand_03, 05_rand_04, 05_rand_05, 05_rand_06, 05_rand_07, 05_rand_08, 05_rand_09, 15_rand_00, 15_rand_01, 15_rand_02, 15_rand_03, 15_rand_04, 15_rand_05, 15_rand_06, 15_rand_07, 15_rand_08, 15_rand_09, 20_teuti_00, 20_teuti_01
Case Name Status Exec Time Memory
00_sample_00 AC 54 ms 4476 KiB
00_sample_01 AC 55 ms 4604 KiB
00_sample_02 AC 55 ms 4612 KiB
05_rand_00 AC 57 ms 4412 KiB
05_rand_01 AC 51 ms 4484 KiB
05_rand_02 AC 51 ms 4492 KiB
05_rand_03 AC 59 ms 4556 KiB
05_rand_04 AC 55 ms 4608 KiB
05_rand_05 AC 54 ms 4484 KiB
05_rand_06 AC 52 ms 4492 KiB
05_rand_07 AC 54 ms 4608 KiB
05_rand_08 AC 55 ms 4488 KiB
05_rand_09 AC 54 ms 4616 KiB
15_rand_00 AC 55 ms 4608 KiB
15_rand_01 AC 54 ms 4496 KiB
15_rand_02 AC 53 ms 4616 KiB
15_rand_03 AC 53 ms 4488 KiB
15_rand_04 AC 52 ms 4612 KiB
15_rand_05 AC 50 ms 4612 KiB
15_rand_06 AC 56 ms 4656 KiB
15_rand_07 AC 57 ms 4484 KiB
15_rand_08 AC 59 ms 4492 KiB
15_rand_09 AC 56 ms 4620 KiB
20_teuti_00 AC 75 ms 4616 KiB
20_teuti_01 AC 54 ms 4548 KiB