import fileinput NEW_LINE import sys NEW_LINE import math NEW_LINE def read_cases ( ) : NEW_LINE INDENT fh = fileinput . input ( ) NEW_LINE T = int ( fh . readline ( ) . strip ( ) ) NEW_LINE cases = [ ] NEW_LINE for t in range ( T ) : NEW_LINE INDENT case = { } NEW_LINE case [ "1" ] = { } NEW_LINE case [ "1" ] [ " r " ] = map ( int , fh . readline ( ) . strip ( ) . split ( " ▁ " ) ) [ 0 ] NEW_LINE case [ "1" ] [ " d " ] = [ ] NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT case [ "1" ] [ " d " ] += [ map ( int , fh . readline ( ) . strip ( ) . split ( " ▁ " ) ) ] NEW_LINE DEDENT case [ "2" ] = { } NEW_LINE case [ "2" ] [ " r " ] = map ( int , fh . readline ( ) . strip ( ) . split ( " ▁ " ) ) [ 0 ] NEW_LINE case [ "2" ] [ " d " ] = [ ] NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT case [ "2" ] [ " d " ] += [ map ( int , fh . readline ( ) . strip ( ) . split ( " ▁ " ) ) ] NEW_LINE DEDENT cases += [ case ] NEW_LINE DEDENT if fh . readline ( ) . strip ( ) != " " : NEW_LINE INDENT raise Exception NEW_LINE DEDENT return cases NEW_LINE DEDENT def process_case ( case ) : NEW_LINE INDENT t1 = case [ "1" ] [ " d " ] [ case [ "1" ] [ " r " ] - 1 ] NEW_LINE t2 = case [ "2" ] [ " d " ] [ case [ "2" ] [ " r " ] - 1 ] NEW_LINE p = set ( t1 ) & set ( t2 ) NEW_LINE if len ( p ) == 1 : NEW_LINE INDENT return list ( p ) [ 0 ] NEW_LINE DEDENT if len ( p ) : NEW_LINE INDENT return " Bad ▁ magician ! " NEW_LINE DEDENT return " Volunteer ▁ cheated ! " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT cases = read_cases ( ) NEW_LINE for i , case in enumerate ( cases [ : ] ) : NEW_LINE INDENT result = process_case ( case ) NEW_LINE print " Case ▁ # % s : " % ( i + 1 , ) , result NEW_LINE DEDENT DEDENT


import java . util . * ; import java . io . * ; public class A { public static void main ( String [ ] args ) throws Exception { Scanner in = new Scanner ( new File ( " A - small - attempt0 . in " ) ) ; PrintWriter out = new PrintWriter ( new FileWriter ( new File ( " A - small . out " ) ) ) ; int t = in . nextInt ( ) ; for ( int x = 0 ; x < t ; x ++ ) { int row1 = in . nextInt ( ) - 1 ; HashSet < Integer > cards = new HashSet < Integer > ( ) ; for ( int y = 0 ; y < 4 ; y ++ ) { for ( int z = 0 ; z < 4 ; z ++ ) { int card = in . nextInt ( ) ; if ( y == row1 ) { cards . add ( card ) ; } } } int row2 = in . nextInt ( ) - 1 ; int chosen = - 1 ; int count = 0 ; for ( int a = 0 ; a < 4 ; a ++ ) { for ( int b = 0 ; b < 4 ; b ++ ) { int card = in . nextInt ( ) ; if ( a == row2 && cards . contains ( card ) ) { chosen = card ; count ++ ; } } } out . print ( " Case ▁ # " + ( x + 1 ) + " : ▁ " ) ; if ( count == 1 ) { out . println ( chosen ) ; } else if ( count == 0 ) { out . println ( " Volunteer ▁ cheated ! " ) ; } else { out . println ( " Bad ▁ magician ! " ) ; } } out . close ( ) ; } }

from collections import * NEW_LINE from sys import argv , stdin , stdout , stderr NEW_LINE Case = namedtuple ( ' Case ' , ' selection , ▁ array ' ) NEW_LINE def readcase ( f ) : NEW_LINE INDENT selection = [ int ( next ( f ) ) ] NEW_LINE array = [ [ readints ( f ) for _ in range ( 4 ) ] ] NEW_LINE selection . append ( int ( next ( f ) ) ) NEW_LINE array . append ( [ readints ( f ) for _ in range ( 4 ) ] ) NEW_LINE return Case ( selection , array ) NEW_LINE DEDENT def solvecase ( case ) : NEW_LINE INDENT sets = [ set ( arr [ sel - 1 ] ) for sel , arr in zip ( * case ) ] NEW_LINE possible = sets [ 0 ] & sets [ 1 ] NEW_LINE if not possible : NEW_LINE INDENT return " Volunteer ▁ cheated ! " NEW_LINE DEDENT elif len ( possible ) > 1 : NEW_LINE INDENT return ' Bad ▁ magician ! ' NEW_LINE DEDENT else : NEW_LINE INDENT return str ( possible . pop ( ) ) NEW_LINE DEDENT DEDENT def readints ( f ) : NEW_LINE INDENT return list ( map ( int , next ( f ) . split ( ' ▁ ' ) ) ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT with open ( argv [ 1 ] ) as f , open ( argv [ 2 ] , ' w ' ) as out : NEW_LINE INDENT cases = int ( next ( f ) ) NEW_LINE for case in range ( 1 , cases + 1 ) : NEW_LINE INDENT print ( ' Case ▁ # % d : ▁ % s ' % ( case , solvecase ( readcase ( f ) ) ) , file = out ) NEW_LINE DEDENT DEDENT DEDENT main ( ) NEW_LINE


import java . util . Scanner ; public class A { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int cases = in . nextInt ( ) ; for ( int i = 1 ; i <= cases ; i ++ ) { int poss = 0 ; int ans = - 1 ; int a = in . nextInt ( ) - 1 ; int [ ] [ ] first = new int [ 4 ] [ 4 ] ; int [ ] [ ] second = new int [ 4 ] [ 4 ] ; for ( int j = 0 ; j < 4 ; j ++ ) { for ( int k = 0 ; k < 4 ; k ++ ) { first [ j ] [ k ] = in . nextInt ( ) ; } } int b = in . nextInt ( ) - 1 ; for ( int j = 0 ; j < 4 ; j ++ ) { for ( int k = 0 ; k < 4 ; k ++ ) { second [ j ] [ k ] = in . nextInt ( ) ; } } for ( int j = 0 ; j < 4 ; j ++ ) { for ( int k = 0 ; k < 4 ; k ++ ) { if ( first [ a ] [ k ] == second [ b ] [ j ] ) { ans = first [ a ] [ k ] ; poss ++ ; } } } if ( poss > 1 ) { System . out . printf ( " Case ▁ # % d : ▁ Bad ▁ magician ! \n " , i ) ; } else if ( poss == 1 ) { System . out . printf ( " Case ▁ # % d : ▁ % d \n " , i , ans ) ; } else { System . out . printf ( " Case ▁ # % d : ▁ Volunteer ▁ cheated ! \n " , i ) ; } } } }

import sys NEW_LINE def main ( ) : NEW_LINE INDENT f = open ( sys . argv [ 1 ] ) NEW_LINE T = int ( f . readline ( ) . strip ( ) ) NEW_LINE for i in range ( 1 , T + 1 ) : NEW_LINE INDENT row1 = getRow ( f ) NEW_LINE row2 = getRow ( f ) NEW_LINE cards = list ( row1 & row2 ) NEW_LINE if len ( cards ) == 1 : NEW_LINE INDENT print " Case ▁ # % d : ▁ % s " % ( i , cards [ 0 ] ) NEW_LINE DEDENT elif len ( cards ) > 1 : NEW_LINE INDENT print " Case ▁ # % d : ▁ Bad ▁ magician ! " % i NEW_LINE DEDENT else : NEW_LINE INDENT print " Case ▁ # % d : ▁ Volunteer ▁ cheated ! " % i NEW_LINE DEDENT DEDENT DEDENT def getRow ( f ) : NEW_LINE INDENT n = int ( f . readline ( ) . strip ( ) ) NEW_LINE row = [ ] NEW_LINE for i in range ( 1 , 5 ) : NEW_LINE INDENT l = f . readline ( ) NEW_LINE if n == i : NEW_LINE INDENT row = set ( l . strip ( ) . split ( ) ) NEW_LINE DEDENT DEDENT return row NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import math NEW_LINE import sys NEW_LINE def findsol ( C1 , C2 ) : NEW_LINE INDENT a = set ( C1 ) . intersection ( set ( C2 ) ) NEW_LINE if len ( a ) == 0 : NEW_LINE INDENT b = ' Volunteer ▁ cheated ! ' NEW_LINE DEDENT if len ( a ) == 1 : NEW_LINE INDENT b = str ( a . pop ( ) ) NEW_LINE DEDENT if len ( a ) > 1 : NEW_LINE INDENT b = ' Bad ▁ magician ! ' NEW_LINE DEDENT return b NEW_LINE DEDENT def convertnums ( s ) : NEW_LINE INDENT a = [ ] NEW_LINE ii = 0 NEW_LINE for jj in range ( len ( s ) ) : NEW_LINE INDENT if s [ jj ] == ' ▁ ' : NEW_LINE INDENT if ( ii < jj ) : NEW_LINE INDENT a . append ( int ( s [ ii : jj ] ) ) NEW_LINE ii = jj + 1 NEW_LINE DEDENT DEDENT DEDENT a . append ( int ( s [ ii : jj ] ) ) NEW_LINE return a NEW_LINE DEDENT fidi = open ( ' A - small - attempt0 . in ' , ' r ' ) NEW_LINE fido = open ( ' a . out ' , ' w ' ) NEW_LINE T = fidi . readline ( ) NEW_LINE T = int ( T ) NEW_LINE for ii in range ( 1 , T + 1 ) : NEW_LINE INDENT R1 = fidi . readline ( ) NEW_LINE R1 = int ( R1 ) NEW_LINE for jj in range ( 4 ) : NEW_LINE INDENT C = fidi . readline ( ) NEW_LINE if ( jj == R1 - 1 ) : NEW_LINE INDENT C1 = convertnums ( C ) NEW_LINE DEDENT DEDENT R2 = fidi . readline ( ) NEW_LINE R2 = int ( R2 ) NEW_LINE for jj in range ( 4 ) : NEW_LINE INDENT C = fidi . readline ( ) NEW_LINE if ( jj == R2 - 1 ) : NEW_LINE INDENT C2 = convertnums ( C ) NEW_LINE DEDENT DEDENT a = findsol ( C1 , C2 ) NEW_LINE fido . write ( ' Case ▁ # ' + str ( ii ) + ' : ▁ ' + str ( a ) + ' \n ' ) NEW_LINE print ( ' Case ▁ # ' , str ( ii ) , ' : ▁ ' , str ( a ) ) NEW_LINE DEDENT fidi . close ( ) NEW_LINE fido . close ( ) NEW_LINE


from collections import deque , defaultdict NEW_LINE E = 0.0 NEW_LINE def main ( ) : NEW_LINE INDENT f = open ( ' A . in ' , ' r ' ) NEW_LINE fout = open ( ' A . out ' , ' w ' ) NEW_LINE T = int ( f . readline ( ) ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT print ' Case ▁ # ' + str ( t ) + ' : ' , NEW_LINE r1 = int ( f . readline ( ) ) NEW_LINE s1 = getSet ( f , r1 ) NEW_LINE r2 = int ( f . readline ( ) ) NEW_LINE s2 = getSet ( f , r2 ) NEW_LINE res = s1 & s2 NEW_LINE if ( len ( res ) == 1 ) : NEW_LINE INDENT print list ( res ) [ 0 ] NEW_LINE DEDENT elif ( len ( res ) > 1 ) : NEW_LINE INDENT print ' Bad ▁ magician ! ' NEW_LINE DEDENT else : NEW_LINE INDENT print ' Volunteer ▁ cheated ! ' NEW_LINE DEDENT DEDENT DEDENT def getSet ( f , r ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 1 , 4 + 1 ) : NEW_LINE INDENT cards = f . readline ( ) . split ( ) NEW_LINE if i == r : NEW_LINE INDENT for card in cards : NEW_LINE INDENT s . add ( card ) NEW_LINE DEDENT DEDENT DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import java . io . BufferedWriter ; import java . io . FileReader ; import java . io . FileWriter ; import java . io . IOException ; import java . util . Scanner ; public class MagicTrick { public void magic ( ) throws IOException { Scanner sc = new Scanner ( new FileReader ( " jam . in " ) ) ; BufferedWriter bw = new BufferedWriter ( new FileWriter ( " jam . out " ) ) ; int cases ; cases = sc . nextInt ( ) ; for ( int z = 1 ; z <= cases ; z ++ ) { int [ ] [ ] map1 = new int [ 4 ] [ 4 ] ; int [ ] [ ] map2 = new int [ 4 ] [ 4 ] ; int i , j ; int r1 , r2 ; r1 = sc . nextInt ( ) - 1 ; for ( i = 0 ; i < 4 ; i ++ ) for ( j = 0 ; j < 4 ; j ++ ) map1 [ i ] [ j ] = sc . nextInt ( ) ; r2 = sc . nextInt ( ) - 1 ; for ( i = 0 ; i < 4 ; i ++ ) for ( j = 0 ; j < 4 ; j ++ ) map2 [ i ] [ j ] = sc . nextInt ( ) ; int ans = 0 ; int sameNum = 0 ; for ( i = 0 ; i < 4 ; i ++ ) for ( j = 0 ; j < 4 ; j ++ ) if ( map1 [ r1 ] [ i ] == map2 [ r2 ] [ j ] ) { sameNum ++ ; ans = map1 [ r1 ] [ i ] ; } bw . write ( " Case ▁ # " + z + " : ▁ " ) ; if ( sameNum == 0 ) bw . write ( " Volunteer ▁ cheated ! " ) ; else if ( sameNum == 1 ) bw . write ( " " + ans ) ; else bw . write ( " Bad ▁ magician ! " ) ; bw . write ( " \n " ) ; } bw . close ( ) ; sc . close ( ) ; } }


package con2014Q ; import java . io . * ; import java . util . * ; public class A { private static final String fileName = " results / con2014Q / " + A . class . getSimpleName ( ) . toLowerCase ( ) ; private static final String inputFileName = fileName + " . in " ; private static final String outputFileName = fileName + " . out " ; private static Scanner in ; private static PrintWriter out ; static final String BAD = " Bad ▁ magician ! " , CHEAT = " Volunteer ▁ cheated ! " ; private void solve ( ) { int R1 = in . nextInt ( ) ; int possible = 0 ; for ( int i = 1 ; i <= 4 ; i ++ ) { for ( int j = 1 ; j <= 4 ; j ++ ) { int n = in . nextInt ( ) ; if ( i == R1 ) { possible |= ( 1 << n ) ; } } } { R1 = in . nextInt ( ) ; int possible2 = 0 ; for ( int i = 1 ; i <= 4 ; i ++ ) { for ( int j = 1 ; j <= 4 ; j ++ ) { int n = in . nextInt ( ) ; if ( i == R1 ) { possible2 |= ( 1 << n ) ; } } } possible &= possible2 ; } if ( possible == 0 ) { out . println ( CHEAT ) ; } else { int res = - 1 ; for ( int i = 0 ; i <= 16 ; i ++ ) { if ( ( possible & ( 1 << i ) ) == 0 ) continue ; if ( res != - 1 ) { out . println ( BAD ) ; return ; } res = i ; } out . println ( res ) ; } } public static void main ( String [ ] args ) throws IOException { long start = System . currentTimeMillis ( ) ; in = new Scanner ( new FileReader ( inputFileName ) ) ; out = new PrintWriter ( outputFileName ) ; int tests = in . nextInt ( ) ; for ( int t = 1 ; t <= tests ; t ++ ) { out . print ( " Case ▁ # " + t + " : ▁ " ) ; new A ( ) . solve ( ) ; System . out . println ( " Case ▁ # " + t + " : ▁ solved " ) ; } in . close ( ) ; out . close ( ) ; long stop = System . currentTimeMillis ( ) ; System . out . println ( stop - start + " ▁ ms " ) ; } }


import java . io . FileInputStream ; import java . io . FileOutputStream ; import java . io . PrintStream ; import java . util . Scanner ; public class ProblemA { public static void main ( String [ ] args ) throws Exception { System . setIn ( new FileInputStream ( " A - small - attempt0 . in " ) ) ; System . setOut ( new PrintStream ( new FileOutputStream ( " A - small - attempt0 . out " ) ) ) ; Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int caseNum = 1 ; caseNum <= T ; caseNum ++ ) { int [ ] counts = new int [ 17 ] ; for ( int turn = 0 ; turn < 2 ; turn ++ ) { int row = in . nextInt ( ) - 1 ; for ( int r = 0 ; r < 4 ; r ++ ) { for ( int c = 0 ; c < 4 ; c ++ ) { int n = in . nextInt ( ) ; if ( r == row ) { counts [ n ] ++ ; } } } } int res = - 2 ; for ( int i = 1 ; i <= 16 ; i ++ ) { if ( counts [ i ] == 2 ) { if ( res == - 2 ) { res = i ; } else { res = - 1 ; } } } if ( res > 0 ) { System . out . printf ( " Case ▁ # % d : ▁ % d % n " , caseNum , res ) ; } else { System . out . printf ( " Case ▁ # % d : ▁ % s % n " , caseNum , ( res == - 2 ) ? " Volunteer ▁ cheated ! " : " Bad ▁ magician ! " ) ; } } } }


import java . util . Scanner ; public class A { static Scanner in = new Scanner ( System . in ) ; void solve ( ) { int N = in . nextInt ( ) ; int [ ] s = new int [ N ] ; int X = 0 ; for ( int i = 0 ; i < N ; ++ i ) { s [ i ] = in . nextInt ( ) ; X += s [ i ] ; } for ( int i = 0 ; i < N ; ++ i ) { double a = 0.0 ; double b = 1.0 ; for ( int q = 0 ; q < 100 ; ++ q ) { double c = ( a + b ) / 2 ; double val = s [ i ] + c * X ; double used = c * X ; for ( int j = 0 ; j < N ; ++ j ) { if ( i == j ) continue ; if ( s [ j ] >= val ) continue ; used += val - s [ j ] ; } if ( used >= X ) { b = c ; } else { a = c ; } } System . out . print ( String . format ( " ▁ % .6f " , 100 * a ) . replace ( " , " , " . " ) ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int T = in . nextInt ( ) ; for ( int i = 1 ; i <= T ; ++ i ) { System . out . print ( " Case ▁ # " + i + " : " ) ; new A ( ) . solve ( ) ; } } }

import sys NEW_LINE import bisect NEW_LINE f = open ( sys . argv [ 1 ] , ' r ' ) NEW_LINE T = int ( f . readline ( ) ) NEW_LINE for case in range ( 0 , T ) : NEW_LINE INDENT line = [ int ( x ) for x in f . readline ( ) . split ( ) ] NEW_LINE ( N , s ) = line [ 0 ] , line [ 1 : ] NEW_LINE total_points = sum ( s ) NEW_LINE temp = s [ : ] NEW_LINE temp . sort ( ) NEW_LINE while total_points > 0 : NEW_LINE INDENT val = temp . pop ( 0 ) NEW_LINE count = 1 NEW_LINE while temp and val == temp [ 0 ] : NEW_LINE INDENT temp . pop ( 0 ) NEW_LINE count += 1 NEW_LINE DEDENT if total_points >= count : NEW_LINE INDENT val += 1 NEW_LINE total_points -= count NEW_LINE DEDENT else : NEW_LINE INDENT val += float ( total_points ) / count NEW_LINE total_points = 0 NEW_LINE DEDENT for i in range ( 0 , count ) : NEW_LINE INDENT bisect . insort ( temp , val ) NEW_LINE DEDENT DEDENT needed = temp [ 0 ] NEW_LINE total_points = sum ( s ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT s [ i ] = max ( 0 , 100 * float ( needed - s [ i ] ) / total_points ) NEW_LINE DEDENT formatted_values = " ▁ " . join ( [ " % 6f " % value for value in s ] ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( case + 1 , formatted_values ) NEW_LINE DEDENT


import sys , math , time NEW_LINE filename = " B " NEW_LINE inputFilename = filename + " - large . in " NEW_LINE outputFilename = filename + " . txt " NEW_LINE inputFile = open ( inputFilename , " r " ) NEW_LINE outputFile = open ( outputFilename , " w " ) NEW_LINE startTime = time . time ( ) NEW_LINE testcases = int ( inputFile . readline ( ) ) NEW_LINE for testcase in range ( testcases ) : NEW_LINE INDENT outputStringHeader = " Case ▁ # " + str ( testcase + 1 ) + " : ▁ " NEW_LINE outputFile . write ( outputStringHeader ) NEW_LINE print " Case ▁ # " , str ( testcase + 1 ) + " : ▁ " NEW_LINE inputInts = map ( int , inputFile . readline ( ) . split ( ) ) NEW_LINE N = inputInts [ 0 ] NEW_LINE scores = inputInts [ 1 : ] NEW_LINE solutions = [ None ] * len ( scores ) NEW_LINE X = sum ( scores ) NEW_LINE avgP = 1.0 / float ( len ( scores ) ) NEW_LINE avgS = float ( X ) / float ( len ( scores ) ) NEW_LINE prunedScores = [ ] NEW_LINE prunedCount = 0 NEW_LINE for i in range ( len ( scores ) ) : NEW_LINE INDENT if ( avgP + ( avgS - scores [ i ] ) * ( 1.0 / float ( X ) ) ) >= 0 : NEW_LINE INDENT prunedScores . append ( scores [ i ] ) NEW_LINE solutions [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT prunedScores . append ( - 1 ) NEW_LINE prunedCount += 1 NEW_LINE DEDENT DEDENT avgP = 1.0 / float ( len ( prunedScores ) - prunedCount ) NEW_LINE avgS = float ( sum ( prunedScores ) + prunedCount ) / float ( len ( prunedScores ) - prunedCount ) NEW_LINE for i in range ( len ( prunedScores ) ) : NEW_LINE INDENT if prunedScores [ i ] != - 1 : NEW_LINE INDENT solutions [ i ] = ( 100 * ( avgP + ( avgS - prunedScores [ i ] ) * ( 1.0 / float ( X ) ) ) ) NEW_LINE DEDENT DEDENT for solution in solutions : NEW_LINE INDENT if solution < 0 : NEW_LINE INDENT solution = 0.0 NEW_LINE DEDENT outputFile . write ( " % 1.8f ▁ " % solution ) NEW_LINE DEDENT outputFile . write ( " \n " ) NEW_LINE DEDENT outputFile . close ( ) NEW_LINE inputFile . close ( ) NEW_LINE print time . time ( ) - startTime NEW_LINE


EPSILON = 0.00000001 NEW_LINE def solve ( N , * votes ) : NEW_LINE INDENT X = sum ( votes ) NEW_LINE def score ( ( i , w ) ) : NEW_LINE INDENT return votes [ i ] + X * w NEW_LINE DEDENT def rebalance ( weights , rem = 1 ) : NEW_LINE INDENT if rem <= EPSILON : NEW_LINE INDENT return weights NEW_LINE DEDENT sorted_pairs = sorted ( enumerate ( weights ) , key = score ) NEW_LINE scores = map ( score , sorted_pairs ) NEW_LINE t = float ( " inf " ) NEW_LINE imax = N NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if scores [ i + 1 ] - scores [ i ] > EPSILON : NEW_LINE INDENT t = scores [ i + 1 ] NEW_LINE imax = i + 1 NEW_LINE break NEW_LINE DEDENT DEDENT if imax * ( t - scores [ 0 ] ) > ( rem * X ) : NEW_LINE INDENT ret = weights [ : ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if i < imax : NEW_LINE INDENT ret [ sorted_pairs [ i ] [ 0 ] ] += rem / float ( imax ) NEW_LINE DEDENT DEDENT return ret NEW_LINE DEDENT new_weights = weights [ : ] NEW_LINE new_rem = rem NEW_LINE w = ( t - scores [ 0 ] ) / float ( X ) NEW_LINE for i in range ( imax ) : NEW_LINE INDENT new_weights [ sorted_pairs [ i ] [ 0 ] ] += w NEW_LINE new_rem -= w NEW_LINE DEDENT return rebalance ( new_weights , new_rem ) NEW_LINE DEDENT return ' ▁ ' . join ( map ( lambda a : str ( 100 * a ) , rebalance ( [ 0 ] * N ) ) ) NEW_LINE DEDENT T = int ( raw_input ( ) ) NEW_LINE for i in range ( T ) : NEW_LINE INDENT print " Case ▁ # % i : ▁ % s " % ( i + 1 , solve ( * ( int ( s ) for s in raw_input ( ) . split ( ' ▁ ' ) ) ) ) NEW_LINE DEDENT


import sys NEW_LINE import string NEW_LINE from itertools import combinations NEW_LINE def lowest ( line ) : NEW_LINE INDENT X = sum ( line ) NEW_LINE srted = sorted ( line ) NEW_LINE out = [ 0 ] * len ( line ) NEW_LINE ptsRemaining = X NEW_LINE linecopy = line NEW_LINE best = 0 NEW_LINE for n in srted : NEW_LINE INDENT if sum ( [ max ( n - l , 0 ) for l in line ] ) <= X : NEW_LINE INDENT best = n NEW_LINE DEDENT DEDENT lower = [ best - l for l in line if best - l >= 0 ] NEW_LINE diff = sum ( lower ) NEW_LINE ptsRemaining = X - diff NEW_LINE distPts = ptsRemaining / float ( len ( lower ) ) NEW_LINE linecopy = list ( line ) NEW_LINE for i in range ( len ( linecopy ) ) : NEW_LINE INDENT if linecopy [ i ] < best + distPts : NEW_LINE INDENT linecopy [ i ] = best + distPts NEW_LINE DEDENT DEDENT for i in range ( len ( linecopy ) ) : NEW_LINE INDENT if linecopy [ i ] > line [ i ] : NEW_LINE INDENT out [ i ] = 100 * ( linecopy [ i ] - line [ i ] ) / float ( X ) NEW_LINE DEDENT DEDENT return out NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT infile = open ( sys . argv [ 1 ] ) NEW_LINE lines = [ ] NEW_LINE output = [ ] NEW_LINE infile . readline ( ) NEW_LINE for line in infile : NEW_LINE INDENT lines . append ( [ int ( p ) for p in line . split ( ) [ 1 : ] ] ) NEW_LINE DEDENT caseNum = 1 NEW_LINE for line in lines : NEW_LINE INDENT print caseNum NEW_LINE output . append ( " Case ▁ # " + str ( caseNum ) + " : ▁ " + ' ▁ ' . join ( [ str ( l ) for l in lowest ( line ) ] ) ) NEW_LINE caseNum += 1 NEW_LINE DEDENT out = open ( ' out . out ' , ' w ' ) NEW_LINE for line in output : NEW_LINE INDENT out . write ( line + ' \n ' ) NEW_LINE DEDENT DEDENT


def readline_ints ( ) : NEW_LINE INDENT return [ int ( num ) for num in fin . readline ( ) . strip ( ) . split ( ) ] NEW_LINE DEDENT from collections import Counter NEW_LINE def tideline ( scores , X ) : NEW_LINE INDENT cc = Counter ( scores ) NEW_LINE floating = 0 NEW_LINE for score in range ( max ( scores ) + 1 ) : NEW_LINE INDENT floating += cc [ score ] NEW_LINE if floating > X : NEW_LINE INDENT break NEW_LINE DEDENT X -= floating NEW_LINE DEDENT else : NEW_LINE INDENT score += 1 NEW_LINE DEDENT return score + X / floating NEW_LINE DEDENT def find_min_vote_proportions ( scores ) : NEW_LINE INDENT X = sum ( scores ) NEW_LINE points_limit = tideline ( scores , X ) NEW_LINE print ( points_limit ) NEW_LINE proportions_needed = [ ] NEW_LINE for s in scores : NEW_LINE INDENT if s >= points_limit : NEW_LINE INDENT proportions_needed . append ( 0.0 ) NEW_LINE DEDENT else : NEW_LINE INDENT points_needed = points_limit - s NEW_LINE proportions_needed . append ( 100 * points_needed / X ) NEW_LINE DEDENT DEDENT return proportions_needed NEW_LINE DEDENT fname = " A - large " NEW_LINE with open ( fname + " . in " , " r " ) as fin , open ( fname + " . out " , " w " ) as fout : NEW_LINE INDENT numcases = readline_ints ( ) [ 0 ] NEW_LINE print ( numcases , " cases " ) NEW_LINE for caseno in range ( 1 , numcases + 1 ) : NEW_LINE INDENT N , * scores = readline_ints ( ) NEW_LINE result = find_min_vote_proportions ( scores ) NEW_LINE result_str = " ▁ " . join ( " % f " % p for p in result ) NEW_LINE outstr = " Case ▁ # % d : ▁ % s " % ( caseno , result_str ) NEW_LINE fout . write ( outstr + " \n " ) NEW_LINE print ( outstr ) NEW_LINE DEDENT DEDENT


import java . io . * ; import java . util . * ; public class A implements Runnable { private String IFILE = " A - large . in " ; private Scanner in ; private PrintWriter out ; public void Run ( ) throws IOException { in = new Scanner ( new File ( IFILE ) ) ; out = new PrintWriter ( " output . txt " ) ; int ntest = in . nextInt ( ) ; for ( int test = 1 ; test <= ntest ; test ++ ) { out . print ( " Case ▁ # " + test + " : ▁ " ) ; int n = in . nextInt ( ) ; int [ ] mas = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) mas [ i ] = in . nextInt ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += mas [ i ] ; double [ ] f = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) f [ i ] = ( mas [ i ] + 0.0 ) / sum ; for ( int i = 0 ; i < n ; i ++ ) { double min = 0.0 ; double max = 1.0 ; while ( max - min > 1e-14 ) { double v = ( min + max ) / 2.0 ; double cv = f [ i ] + v ; double q = 0.0 ; for ( int j = 0 ; j < n ; j ++ ) if ( i != j ) q += Math . max ( cv - f [ j ] , 0 ) ; if ( q < 1.0 - v ) min = v ; else max = v ; } out . print ( " ▁ " + 100.0 * max ) ; } out . println ( ) ; } in . close ( ) ; out . close ( ) ; } public void run ( ) { try { Run ( ) ; } catch ( IOException e ) { } } public static void main ( String [ ] args ) throws IOException { new A ( ) . Run ( ) ; } }


package round1 ; import java . io . FileInputStream ; import java . io . FileNotFoundException ; import java . io . FileOutputStream ; public class A { public static void main ( String [ ] args ) throws FileNotFoundException { Kattio io ; io = new Kattio ( new FileInputStream ( " src / round1 / A - large - 0 . in " ) , new FileOutputStream ( " src / round1 / A - large - 0 . out " ) ) ; int cases = io . getInt ( ) ; for ( int i = 1 ; i <= cases ; i ++ ) { io . print ( " Case ▁ # " + i + " : ▁ " ) ; new A ( ) . solve ( io ) ; } io . close ( ) ; } private void solve ( Kattio io ) { int n = io . getInt ( ) , sum = 0 ; int s [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { s [ i ] = io . getInt ( ) ; sum += s [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { double lo = 0.0 , hi = 1.0 ; for ( int j = 0 ; j < 200 ; j ++ ) { double x = ( lo + hi ) / 2 ; double isc = s [ i ] + sum * x ; double left = 1 - x ; for ( int k = 0 ; k < n ; k ++ ) { if ( k == i || s [ k ] > isc ) continue ; double y = ( isc - s [ k ] ) / sum ; left -= y ; } if ( left < 0 ) { hi = x ; } else { lo = x ; } } if ( i > 0 ) { io . print ( ' ▁ ' ) ; } io . print ( lo * 100.0 ) ; } io . println ( ) ; } }


import static java . lang . Math . max ; import java . io . * ; import java . util . * ; public class SafetyInNumbers { public static void main ( String [ ] args ) throws Throwable { Scanner in = new Scanner ( new File ( " safety . in " ) ) ; PrintWriter out = new PrintWriter ( " safety . out " ) ; int testCount = Integer . parseInt ( in . nextLine ( ) . trim ( ) ) ; for ( int i = 0 ; i < testCount ; i ++ ) { out . print ( " Case ▁ # " + ( i + 1 ) + " : " ) ; solve ( in , out ) ; } out . close ( ) ; } static void solve ( Scanner in , PrintWriter out ) { int n = in . nextInt ( ) ; double [ ] answer = new double [ n ] ; int sum = 0 ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = in . nextInt ( ) ; sum += a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { double l = 0 ; double r = 1 ; for ( int j = 0 ; j < 100 ; j ++ ) { double m = ( l + r ) * .5 ; double currentMan = a [ i ] + m * sum ; double all = m ; for ( int k = 0 ; k < n ; k ++ ) { if ( k == i ) { continue ; } all += max ( 0 , ( currentMan - a [ k ] ) / sum ) ; } if ( all > 1 ) { r = m ; } else { l = m ; } } answer [ i ] = ( l + r ) * 50 ; } for ( double e : answer ) { out . print ( " ▁ " + e ) ; } out . println ( ) ; } }


import java . io . * ; import java . text . DecimalFormat ; import java . util . * ; import static java . lang . Math . * ; import static java . util . Arrays . * ; public class A { public static void main ( String [ ] args ) throws FileNotFoundException { in = new Scanner ( new InputStreamReader ( new FileInputStream ( " a _ in . txt " ) ) ) ; out = new PrintStream ( new BufferedOutputStream ( new FileOutputStream ( " a _ out . txt " ) ) ) ; int n = in . nextInt ( ) ; in . nextLine ( ) ; int t = 1 ; while ( n -- > 0 ) new A ( ) . solveProblem ( t ++ ) ; out . close ( ) ; } static Scanner in = new Scanner ( new InputStreamReader ( System . in ) ) ; static PrintStream out = new PrintStream ( new BufferedOutputStream ( System . out ) ) ; public void solveProblem ( int nr ) { out . print ( " Case ▁ # " + nr + " : ▁ " ) ; int n = in . nextInt ( ) ; double [ ] p = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) p [ i ] = in . nextInt ( ) ; double som = 0 ; for ( double pp : p ) som += ( double ) pp ; for ( int i = 0 ; i < n ; i ++ ) { double lo = 0.0 ; double hi = 1. ; for ( int l = 0 ; l < 500 ; l ++ ) { double mid = ( lo + hi ) / 2.0 ; double eind = p [ i ] + mid * som ; double tot = mid ; for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; if ( p [ j ] >= eind - eps ) continue ; tot += ( eind - p [ j ] ) / som ; } if ( tot > 1.0 ) hi = mid ; else lo = mid ; } double ou = ( 100.0 * lo ) ; if ( ou < eps ) ou = 0.0 ; out . print ( ou ) ; if ( i == n - 1 ) out . println ( ) ; else out . print ( " ▁ " ) ; } System . err . println ( " Case ▁ # " + nr + " ▁ solved " ) ; } double eps = 1e-9 ; }


import java . io . File ; import java . io . FileNotFoundException ; import java . io . PrintStream ; import java . util . Scanner ; public class RollerCoasterScheduling { public static void main ( String [ ] args ) throws FileNotFoundException { Scanner cin = new Scanner ( new File ( " B - large . in " ) ) ; PrintStream cout = new PrintStream ( " B - large . out " ) ; int _case = 0 ; for ( int T = cin . nextInt ( ) ; T > 0 ; T -- ) { _case ++ ; int N = cin . nextInt ( ) ; int C = cin . nextInt ( ) ; int M = cin . nextInt ( ) ; int [ ] [ ] a = new int [ C + 1 ] [ N + 1 ] ; for ( int i = 0 ; i < M ; i ++ ) { int p = cin . nextInt ( ) ; int b = cin . nextInt ( ) ; a [ b ] [ p ] ++ ; } int totalTrains = 0 ; for ( int i = 1 ; i <= C ; i ++ ) { int sum = 0 ; for ( int j = 1 ; j <= N ; j ++ ) sum += a [ i ] [ j ] ; totalTrains = Math . max ( totalTrains , sum ) ; } int prefixColSum = 0 ; for ( int j = 1 ; j <= N ; j ++ ) { for ( int i = 1 ; i <= C ; i ++ ) prefixColSum += a [ i ] [ j ] ; totalTrains = Math . max ( totalTrains , ( prefixColSum + j - 1 ) / j ) ; } int promotion = 0 ; for ( int j = N ; j >= 1 ; j -- ) { int sum = 0 ; for ( int i = 1 ; i <= C ; i ++ ) sum += a [ i ] [ j ] ; int extra = Math . max ( 0 , sum - totalTrains ) ; promotion += extra ; } String ans = " " + totalTrains + " ▁ " + promotion ; cout . printf ( " Case ▁ # % d : ▁ % s % n " , _case , ans ) ; } cin . close ( ) ; cout . close ( ) ; } }

import numpy as np NEW_LINE import math NEW_LINE inname = " input . txt " NEW_LINE outname = " output . txt " NEW_LINE with open ( inname , ' r ' ) as f : NEW_LINE INDENT cases = int ( f . readline ( ) ) NEW_LINE for tc in range ( 1 , cases + 1 ) : NEW_LINE INDENT line = f . readline ( ) . strip ( ) . split ( ' ▁ ' ) NEW_LINE N = int ( line [ 0 ] ) NEW_LINE C = int ( line [ 1 ] ) NEW_LINE M = int ( line [ 2 ] ) NEW_LINE Cnum = [ 0 ] * C NEW_LINE Nnum = [ 0 ] * N NEW_LINE for i in range ( M ) : NEW_LINE INDENT line = f . readline ( ) . strip ( ) . split ( ' ▁ ' ) NEW_LINE p = int ( line [ 0 ] ) NEW_LINE b = int ( line [ 1 ] ) NEW_LINE Nnum [ p - 1 ] += 1 NEW_LINE Cnum [ b - 1 ] += 1 NEW_LINE DEDENT Nsum = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT Nsum [ i + 1 ] = Nsum [ i ] + Nnum [ i ] NEW_LINE DEDENT y = 0 NEW_LINE for i in range ( C ) : NEW_LINE INDENT if Cnum [ i ] > y : NEW_LINE INDENT y = Cnum [ i ] NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT a = math . ceil ( Nsum [ i + 1 ] / ( i + 1 ) ) NEW_LINE if a > y : NEW_LINE INDENT y = a NEW_LINE DEDENT DEDENT z = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if Nnum [ i ] > y : NEW_LINE INDENT z += Nnum [ i ] - y NEW_LINE DEDENT DEDENT print ( " Case ▁ # % d : ▁ % d ▁ % d " % ( tc , y , z ) ) NEW_LINE DEDENT DEDENT


def optimalResult ( N , C , positionsByPerson ) : NEW_LINE INDENT fewestRides = 0 NEW_LINE for position in xrange ( 1 , N + 1 ) : NEW_LINE INDENT totalRides = 0 NEW_LINE for B in positionsByPerson : NEW_LINE INDENT rides = 0 NEW_LINE for P in positionsByPerson [ B ] : NEW_LINE INDENT if P <= position : NEW_LINE INDENT rides += 1 NEW_LINE DEDENT DEDENT fewestRides = max ( rides , fewestRides ) NEW_LINE totalRides += rides NEW_LINE DEDENT fewestRides = max ( fewestRides , ( totalRides + position - 1 ) / position ) NEW_LINE DEDENT upgrades = 0 NEW_LINE for position in xrange ( 1 , N + 1 ) : NEW_LINE INDENT frequency = 0 NEW_LINE for B in positionsByPerson : NEW_LINE INDENT for P in positionsByPerson [ B ] : NEW_LINE INDENT if P == position : NEW_LINE INDENT frequency += 1 NEW_LINE DEDENT DEDENT DEDENT upgrades += max ( 0 , frequency - fewestRides ) NEW_LINE DEDENT return ' ▁ ' . join ( map ( str , [ fewestRides , upgrades ] ) ) NEW_LINE DEDENT with open ( ' . . / inputs / B - large . in ' ) as infile : NEW_LINE INDENT with open ( ' . . / outputs / B - large . out ' , ' wb ' ) as outfile : NEW_LINE INDENT cases = int ( infile . readline ( ) ) NEW_LINE for i in xrange ( cases ) : NEW_LINE INDENT [ N , C , M ] = map ( int , infile . readline ( ) . split ( ' ▁ ' ) ) NEW_LINE positionsByPerson = { } NEW_LINE for _ in xrange ( M ) : NEW_LINE INDENT [ P , B ] = map ( int , infile . readline ( ) . split ( ' ▁ ' ) ) NEW_LINE positionsByPerson [ B ] = positionsByPerson . get ( B , [ ] ) + [ P ] NEW_LINE DEDENT for B in positionsByPerson : NEW_LINE INDENT positionsByPerson [ B ] = sorted ( positionsByPerson [ B ] ) NEW_LINE DEDENT outfile . write ( ' Case ▁ # ' + str ( i + 1 ) + ' : ▁ ' ) NEW_LINE outfile . write ( optimalResult ( N , C , positionsByPerson ) ) NEW_LINE outfile . write ( ' \n ' ) NEW_LINE DEDENT DEDENT DEDENT


from collections import defaultdict NEW_LINE def solve ( N , C , M , A ) : NEW_LINE INDENT num_bought = defaultdict ( int ) NEW_LINE for p , b in A : NEW_LINE INDENT num_bought [ b ] += 1 NEW_LINE DEDENT tickets = defaultdict ( list ) NEW_LINE for p , b in A : NEW_LINE INDENT tickets [ p ] . append ( b ) NEW_LINE DEDENT for k , v in tickets . iteritems ( ) : NEW_LINE INDENT v . sort ( key = lambda x : num_bought [ x ] ) NEW_LINE DEDENT R = max ( num_bought . values ( ) ) NEW_LINE tickets_sold = len ( A ) NEW_LINE for num_rides in xrange ( R , tickets_sold + 1 ) : NEW_LINE INDENT promos = 0 NEW_LINE awaiting_seat = 0 NEW_LINE for seat in xrange ( N , 0 , - 1 ) : NEW_LINE INDENT new = len ( tickets [ seat ] ) NEW_LINE if awaiting_seat + new <= num_rides : NEW_LINE INDENT awaiting_seat = 0 NEW_LINE DEDENT else : NEW_LINE INDENT if new <= num_rides : NEW_LINE INDENT rem = num_rides - new NEW_LINE awaiting_seat -= rem NEW_LINE awaiting_seat = max ( awaiting_seat , 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT awaiting_seat += ( new - num_rides ) NEW_LINE promos += new - num_rides NEW_LINE DEDENT DEDENT DEDENT if not awaiting_seat : NEW_LINE INDENT return " { } ▁ { } " . format ( num_rides , promos ) NEW_LINE DEDENT DEDENT DEDENT def main ( ) : NEW_LINE INDENT with open ( ' in . txt ' , ' r ' ) as fi , open ( ' out . txt ' , ' w ' ) as fo : NEW_LINE INDENT rr = lambda : fi . readline ( ) . strip ( ) NEW_LINE rrM = lambda : map ( int , rr ( ) . split ( ) ) NEW_LINE for tc in xrange ( 1 , 1 + int ( rr ( ) ) ) : NEW_LINE INDENT N , C , M = rrM ( ) NEW_LINE A = [ rrM ( ) for _ in xrange ( M ) ] NEW_LINE zeta = solve ( N , C , M , A ) NEW_LINE outstr = " Case ▁ # % i : ▁ " % tc + str ( zeta ) NEW_LINE print outstr NEW_LINE fo . write ( outstr + ' \n ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : main ( ) NEW_LINE


import numpy as np NEW_LINE LINES_PARAM = 2 NEW_LINE INPUT_FILE_NAME = ' B - large . in ' NEW_LINE OUTPUT_FILE_NAME = ' B - large . out ' NEW_LINE def do_case ( parsed ) : NEW_LINE INDENT n = parsed [ 0 ] [ 0 ] NEW_LINE c = parsed [ 0 ] [ 1 ] NEW_LINE seat = [ 0 for i in range ( n ) ] NEW_LINE rider = [ 0 for i in range ( c ) ] NEW_LINE for l in parsed [ 1 : ] : NEW_LINE INDENT rider [ l [ 1 ] - 1 ] += 1 NEW_LINE seat [ l [ 0 ] - 1 ] += 1 NEW_LINE DEDENT s = np . cumsum ( seat ) NEW_LINE m = max ( rider ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m = max ( m , ( s [ i ] + i ) // ( i + 1 ) ) NEW_LINE DEDENT pr = sum ( [ max ( 0 , se - m ) for se in seat ] ) NEW_LINE return str ( m ) + ' ▁ ' + str ( pr ) NEW_LINE DEDENT def do_parse ( input ) : NEW_LINE INDENT return [ [ int ( num ) for num in line . rstrip ( ) . split ( " ▁ " ) ] for line in input ] NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT input_f = open ( INPUT_FILE_NAME , ' r ' ) NEW_LINE output = [ ] NEW_LINE num_of_test_cases = int ( input_f . readline ( ) , 10 ) NEW_LINE temp = input_f . readlines ( ) NEW_LINE index = 0 NEW_LINE for test_case in range ( num_of_test_cases ) : NEW_LINE INDENT lines = int ( temp [ index ] . rstrip ( ) . split ( " ▁ " ) [ LINES_PARAM ] ) NEW_LINE parsed_input = do_parse ( temp [ index : index + lines + 1 ] ) NEW_LINE index = index + 1 + lines NEW_LINE output . append ( ' Case ▁ # ' + str ( test_case + 1 ) + ' : ▁ ' + do_case ( parsed_input ) ) NEW_LINE DEDENT output_f = open ( OUTPUT_FILE_NAME , ' w ' ) NEW_LINE output_f . write ( ' \n ' . join ( output ) ) NEW_LINE input_f . close ( ) NEW_LINE output_f . close ( ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


numInputs = int ( input ( ) ) NEW_LINE for i in range ( numInputs ) : NEW_LINE INDENT N , C , M = [ int ( num ) for num in input ( ) . split ( " ▁ " ) ] NEW_LINE custSeats = [ { } , { } ] NEW_LINE seatsBought = set ( ) NEW_LINE for _ in range ( M ) : NEW_LINE INDENT seatNum , custNum = [ int ( num ) for num in input ( ) . split ( " ▁ " ) ] NEW_LINE seatsBought . add ( seatNum ) NEW_LINE if seatNum in custSeats [ custNum - 1 ] . keys ( ) : NEW_LINE INDENT custSeats [ custNum - 1 ] [ seatNum ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT custSeats [ custNum - 1 ] [ seatNum ] = 1 NEW_LINE DEDENT DEDENT numTicketsBought = [ 0 , 0 ] NEW_LINE for custNum in range ( 2 ) : NEW_LINE INDENT for seatNum in custSeats [ custNum ] . keys ( ) : NEW_LINE INDENT numTicketsBought [ custNum ] += custSeats [ custNum ] [ seatNum ] NEW_LINE DEDENT DEDENT larger = max ( numTicketsBought ) NEW_LINE smaller = min ( numTicketsBought ) NEW_LINE numRides = larger NEW_LINE numPromotions = 0 NEW_LINE for seatNum in seatsBought : NEW_LINE INDENT if seatNum not in custSeats [ 0 ] . keys ( ) or seatNum not in custSeats [ 1 ] . keys ( ) : NEW_LINE INDENT continue NEW_LINE DEDENT if custSeats [ 0 ] [ seatNum ] + custSeats [ 1 ] [ seatNum ] <= larger : NEW_LINE INDENT continue NEW_LINE DEDENT if seatNum == 1 : NEW_LINE INDENT numRides = custSeats [ 0 ] [ seatNum ] + custSeats [ 1 ] [ seatNum ] NEW_LINE DEDENT else : NEW_LINE INDENT numPromotions += custSeats [ 0 ] [ seatNum ] + custSeats [ 1 ] [ seatNum ] - larger NEW_LINE DEDENT DEDENT print ( " Case ▁ # " + str ( i + 1 ) + " : ▁ " + str ( numRides ) + " ▁ " + str ( numPromotions ) ) NEW_LINE DEDENT


import java . util . * ; public class cjB { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int cas = sc . nextInt ( ) ; for ( int cass = 1 ; cass <= cas ; cass ++ ) { System . out . print ( " Case ▁ # " + cass + " : ▁ " ) ; int seats = sc . nextInt ( ) ; int customers = sc . nextInt ( ) ; int tickets = sc . nextInt ( ) ; int [ ] perCust = new int [ customers ] ; int [ ] perSeat = new int [ seats ] ; int maxPerCust = 0 ; for ( int ttt = 0 ; ttt < tickets ; ttt ++ ) { int ssss = sc . nextInt ( ) - 1 ; int cccc = sc . nextInt ( ) - 1 ; perCust [ cccc ] ++ ; perSeat [ ssss ] ++ ; if ( perCust [ cccc ] > maxPerCust ) { maxPerCust = perCust [ cccc ] ; } } int tt = tickets ; int minCoast = 0 ; while ( tt > 0 ) { minCoast ++ ; tt -= seats ; } if ( minCoast < maxPerCust ) minCoast = maxPerCust ; int tot = 0 ; for ( int qw = 0 ; qw < seats ; qw ++ ) { tot += perSeat [ qw ] ; while ( ( minCoast * ( qw + 1 ) < tot ) ) { minCoast ++ ; } } int promo = 0 ; for ( int qq = 0 ; qq < seats ; qq ++ ) { if ( perSeat [ qq ] > minCoast ) { promo += ( perSeat [ qq ] - minCoast ) ; } } System . out . println ( minCoast + " ▁ " + promo ) ; } } }


package round2 ; import java . util . Scanner ; public class B { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int cases = sc . nextInt ( ) ; for ( int caze = 1 ; caze <= cases ; caze ++ ) { int N = sc . nextInt ( ) ; int C = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; int [ ] seats = new int [ N ] ; int [ ] clients = new int [ C ] ; for ( int i = 0 ; i < M ; i ++ ) { int pos = sc . nextInt ( ) - 1 ; int client = sc . nextInt ( ) - 1 ; seats [ pos ] ++ ; clients [ client ] ++ ; } int ansA = 0 ; for ( int i = 0 ; i < clients . length ; i ++ ) { int client = clients [ i ] ; ansA = Math . max ( ansA , client ) ; } int acum = 0 ; for ( int i = 0 ; i < seats . length ; i ++ ) { int seat = seats [ i ] ; acum += seat ; ansA = Math . max ( ansA , ( acum + i ) / ( i + 1 ) ) ; } int ansB = 0 ; for ( int i = 0 ; i < seats . length ; i ++ ) { int seat = seats [ i ] ; if ( seat > ansA ) ansB += seat - ansA ; } System . out . println ( " Case ▁ # " + caze + " : ▁ " + ansA + " ▁ " + ansB ) ; } } }


import java . util . * ; class B { public static void main ( String [ ] arg ) { Scanner sc = new Scanner ( System . in ) ; int TT = sc . nextInt ( ) ; for ( int ii = 1 ; ii <= TT ; ++ ii ) { int N = sc . nextInt ( ) ; int C = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; int [ ] COUNT = new int [ C + 1 ] ; int [ ] coast = new int [ N + 1 ] ; for ( int i = 0 ; i < M ; ++ i ) { int pos = sc . nextInt ( ) ; int buyer = sc . nextInt ( ) ; COUNT [ buyer ] ++ ; coast [ pos ] ++ ; } int min = 0 ; for ( int i = 0 ; i < COUNT . length ; ++ i ) { if ( COUNT [ i ] > min ) min = COUNT [ i ] ; } min = Math . max ( min , coast [ 1 ] ) ; int max = M ; while ( min < max ) { int piv = ( min + max ) / 2 ; int promotions = canDo ( coast , piv ) ; if ( promotions >= 0 ) { if ( max == piv ) System . err . println ( " whoops " ) ; max = piv ; } else { min = piv + 1 ; } } if ( min != max ) System . err . println ( " never " ) ; int ans = canDo ( coast , max ) ; System . out . printf ( " Case ▁ # % d : ▁ % d ▁ % d \n " , ii , max , ans ) ; } } public static int canDo ( int [ ] coast , int R ) { int slots_available = 0 ; int promotions = 0 ; for ( int i = 1 ; i < coast . length ; ++ i ) { if ( coast [ i ] > R ) { if ( coast [ i ] - R > slots_available ) { return - 1 ; } int p = ( coast [ i ] - R ) ; slots_available -= p ; promotions += p ; } else if ( coast [ i ] == R ) { } else { slots_available += R - coast [ i ] ; } } return promotions ; } }


import java . io . * ; import java . util . * ; public class Googlerese { public static void main ( String [ ] args ) throws Exception { BufferedReader in = new BufferedReader ( new InputStreamReader ( System . in ) ) ; String dict = " yhesocvxduiglbkrztnwjpfmaq " ; int T = Integer . parseInt ( in . readLine ( ) ) ; for ( int i = 0 ; i < T ; i ++ ) { String s = in . readLine ( ) ; String ans = " " ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { if ( s . charAt ( j ) < 97 || s . charAt ( j ) > 122 ) { ans += s . charAt ( j ) ; } else { ans += dict . charAt ( s . charAt ( j ) - 97 ) ; } } System . out . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + ans ) ; } } }


import sys NEW_LINE def makemap ( ) : NEW_LINE INDENT ALPHA = ' abcdefghijklmnopqrstuvwxyz ' NEW_LINE M = { ' y ' : ' a ' , ' e ' : ' o ' , ' q ' : ' z ' , } NEW_LINE G = ''' ejp ▁ mysljylc ▁ kd ▁ kxveddknmc ▁ re ▁ jsicpdrysi STRNEWLINE rbcpc ▁ ypc ▁ rtcsra ▁ dkh ▁ wyfrepkym ▁ veddknkmkrkcd STRNEWLINE de ▁ kr ▁ kd ▁ eoya ▁ kw ▁ aej ▁ tysr ▁ re ▁ ujdr ▁ lkgc ▁ jv ''' NEW_LINE E = ''' our ▁ language ▁ is ▁ impossible ▁ to ▁ understand STRNEWLINE there ▁ are ▁ twenty ▁ six ▁ factorial ▁ possibilities STRNEWLINE so ▁ it ▁ is ▁ okay ▁ if ▁ you ▁ want ▁ to ▁ just ▁ give ▁ up ''' NEW_LINE assert ( len ( G ) == len ( E ) ) NEW_LINE for i in range ( len ( G ) ) : NEW_LINE INDENT g = G [ i ] NEW_LINE e = E [ i ] NEW_LINE if g in ALPHA : NEW_LINE INDENT assert ( e in ALPHA ) NEW_LINE if g not in M : NEW_LINE INDENT M [ g ] = e NEW_LINE DEDENT else : NEW_LINE INDENT assert ( M [ g ] == e ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT assert ( e not in ALPHA ) NEW_LINE DEDENT DEDENT assert ( len ( M ) == 25 or len ( M ) == 26 ) NEW_LINE assert ( len ( M ) == len ( set ( M . values ( ) ) ) ) NEW_LINE if len ( M ) == 25 : NEW_LINE INDENT g = ' ' . join ( c for c in ALPHA if c not in M ) NEW_LINE e = ' ' . join ( c for c in ALPHA if c not in M . values ( ) ) NEW_LINE M [ g ] = e NEW_LINE DEDENT assert ( len ( M ) == len ( set ( M . values ( ) ) ) ) NEW_LINE assert ( set ( M ) == set ( ALPHA ) ) NEW_LINE return M NEW_LINE DEDENT def trans ( s , M ) : NEW_LINE INDENT return ' ' . join ( M . get ( c , c ) for c in s ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT M = makemap ( ) NEW_LINE T = int ( sys . stdin . readline ( ) . strip ( ) ) NEW_LINE for i in range ( T ) : NEW_LINE INDENT s = sys . stdin . readline ( ) NEW_LINE s = trans ( s , M ) NEW_LINE sys . stdout . write ( ' Case ▁ # { } : ▁ ' . format ( i + 1 ) ) NEW_LINE sys . stdout . write ( s ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


dictionary = { ' a ' : ' y ' , ' b ' : ' h ' , ' c ' : ' e ' , ' d ' : ' s ' , ' e ' : ' o ' , ' f ' : ' c ' , ' g ' : ' v ' , ' h ' : ' x ' , ' i ' : ' d ' , ' j ' : ' u ' , ' k ' : ' i ' , ' l ' : ' g ' , ' m ' : ' l ' , ' n ' : ' b ' , ' o ' : ' k ' , ' p ' : ' r ' , ' q ' : ' z ' , ' r ' : ' t ' , ' s ' : ' n ' , ' t ' : ' w ' , ' u ' : ' j ' , ' v ' : ' p ' , ' w ' : ' f ' , ' x ' : ' m ' , ' y ' : ' a ' , ' z ' : ' q ' } NEW_LINE def process_file ( file ) : NEW_LINE INDENT fsock = open ( file ) NEW_LINE text = fsock . read ( ) NEW_LINE fsock . close ( ) NEW_LINE lines = text . split ( ' \n ' ) NEW_LINE return lines NEW_LINE DEDENT def process_lines ( lines ) : NEW_LINE INDENT ans = [ ] NEW_LINE first = True NEW_LINE for line in lines : NEW_LINE INDENT if first == True : NEW_LINE INDENT first = False NEW_LINE DEDENT else : NEW_LINE INDENT trans = ' ' NEW_LINE for char in line : NEW_LINE INDENT if char in dictionary : NEW_LINE INDENT trans += dictionary [ char ] NEW_LINE DEDENT else : NEW_LINE INDENT trans += char NEW_LINE DEDENT DEDENT if trans != ' ' : NEW_LINE INDENT ans . append ( trans ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT import sys NEW_LINE filename = sys . argv [ 1 ] NEW_LINE lines = process_file ( filename ) NEW_LINE res = process_lines ( lines ) NEW_LINE c = 0 NEW_LINE for line in res : NEW_LINE INDENT c += 1 NEW_LINE print " Case ▁ # % d : ▁ % s " % ( c , line ) NEW_LINE DEDENT DEDENT


import numpy as N NEW_LINE from string import translate , maketrans NEW_LINE case = 1 NEW_LINE problem = " A " NEW_LINE practice = False NEW_LINE if practice : NEW_LINE INDENT practice = " - practice " NEW_LINE DEDENT else : NEW_LINE INDENT practice = " - attempt0" NEW_LINE DEDENT if case == 0 : NEW_LINE INDENT infile = open ( " % s - % s % s . in " % ( problem , " sample " , practice ) , ' r ' ) NEW_LINE outfile = open ( " % s - % s % s . out " % ( problem , " sample " , practice ) , ' w ' ) NEW_LINE DEDENT elif case == 1 : NEW_LINE INDENT infile = open ( " % s - % s % s . in " % ( problem , " small " , practice ) , ' r ' ) NEW_LINE outfile = open ( " % s - % s % s . out " % ( problem , " small " , practice ) , ' w ' ) NEW_LINE DEDENT elif case == 2 : NEW_LINE INDENT infile = open ( " % s - % s % s . in " % ( problem , " large " , practice ) , ' r ' ) NEW_LINE outfile = open ( " % s - % s % s . out " % ( problem , " large " , practice ) , ' w ' ) NEW_LINE DEDENT else : NEW_LINE INDENT raise ValueError , ' Invalid ▁ case ' NEW_LINE DEDENT alpha = ' abcdefghijklmnopqrstuvwxyz ▁ ' NEW_LINE trans = ' ynficwlbkuomxsevzpdrjgthaq ▁ ' NEW_LINE table = maketrans ( trans , alpha ) NEW_LINE cases = int ( infile . readline ( ) . strip ( ' \n ' ) ) NEW_LINE for i in range ( cases ) : NEW_LINE INDENT instr = infile . readline ( ) . strip ( ' \n ' ) NEW_LINE output = translate ( instr , table ) NEW_LINE outfile . write ( ' Case ▁ # % i : ▁ % s \n ' % ( i + 1 , output ) ) NEW_LINE DEDENT infile . close ( ) NEW_LINE outfile . close ( ) NEW_LINE


translator = { ' y ' : ' a ' , ' n ' : ' b ' , ' f ' : ' c ' , ' i ' : ' d ' , ' c ' : ' e ' , ' w ' : ' f ' , ' l ' : ' g ' , ' b ' : ' h ' , ' k ' : ' i ' , ' u ' : ' j ' , ' o ' : ' k ' , ' m ' : ' l ' , ' x ' : ' m ' , ' s ' : ' n ' , ' e ' : ' o ' , ' v ' : ' p ' , ' z ' : ' q ' , ' p ' : ' r ' , ' d ' : ' s ' , ' r ' : ' t ' , ' j ' : ' u ' , ' g ' : ' v ' , ' t ' : ' w ' , ' h ' : ' x ' , ' a ' : ' y ' , ' q ' : ' z ' } NEW_LINE with open ( ' A - small - attempt0 . in ' , ' r ' ) as fin : NEW_LINE INDENT with open ( ' output . txt ' , ' w ' ) as fout : NEW_LINE INDENT numcases = int ( fin . readline ( ) ) NEW_LINE for i in range ( 1 , numcases + 1 ) : NEW_LINE INDENT fout . write ( " Case ▁ # " ) NEW_LINE fout . write ( str ( i ) ) NEW_LINE fout . write ( " : ▁ " ) NEW_LINE line = fin . readline ( ) NEW_LINE for k in line : NEW_LINE INDENT if k in translator : NEW_LINE INDENT fout . write ( translator [ k ] ) NEW_LINE DEDENT elif k . isupper ( ) and k . lower ( ) in translator : NEW_LINE INDENT print " Hit ▁ upper " NEW_LINE fout . write ( translator [ k . lower ( ) ] . upper ( ) ) NEW_LINE DEDENT else : NEW_LINE INDENT fout . write ( k ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT


package j2012qualifier ; import java . io . File ; import java . io . FileNotFoundException ; import java . io . PrintWriter ; import java . util . HashMap ; import java . util . Map ; import java . util . Scanner ; public class A { public static String inputDirectory = " src / j2012qualifier / " ; public static String inputFile = " A . in " ; public static String outputFile = " A . out " ; public static PrintWriter output ; public static void main ( String [ ] args ) throws FileNotFoundException { Scanner s = new Scanner ( new File ( inputDirectory + inputFile ) ) ; output = new PrintWriter ( new File ( inputDirectory + outputFile ) ) ; Map < Character , Character > codeMap = new HashMap < Character , Character > ( ) ; String source = " qzejp ▁ mysljylc ▁ kd ▁ kxveddknmc ▁ re ▁ jsicpdrysi ▁ rbcpc ▁ ypc ▁ rtcsra ▁ dkh ▁ wyfrepkym ▁ veddknkmkrkcd ▁ de ▁ kr ▁ kd ▁ eoya ▁ kw ▁ aej ▁ tysr ▁ re ▁ ujdr ▁ lkgc ▁ jv " ; String result = " zqour ▁ language ▁ is ▁ impossible ▁ to ▁ understand ▁ there ▁ are ▁ twenty ▁ six ▁ factorial ▁ possibilities ▁ so ▁ it ▁ is ▁ okay ▁ if ▁ you ▁ want ▁ to ▁ just ▁ give ▁ up " ; for ( int i = 0 ; i < source . length ( ) ; i ++ ) { codeMap . put ( source . charAt ( i ) , result . charAt ( i ) ) ; } int cases = s . nextInt ( ) ; s . nextLine ( ) ; for ( int Case = 1 ; Case <= cases ; Case ++ ) { String code = s . nextLine ( ) ; char [ ] uncoded = new char [ code . length ( ) ] ; for ( int i = 0 ; i < uncoded . length ; i ++ ) { uncoded [ i ] = codeMap . get ( code . charAt ( i ) ) ; } String answer = String . valueOf ( uncoded ) ; out ( " Case ▁ # " + Case + " : ▁ " + answer ) ; } output . flush ( ) ; } public static void out ( String s ) { output . println ( s ) ; } }


package google . codejam2012 . qualification ; import java . io . BufferedReader ; import java . io . InputStreamReader ; public class SpeakingInTongues { private static String TRANSLATER = " yhesocvxduiglbkrztnwjpfmaq " ; private static char translate ( char ch ) { if ( ' a ' <= ch & ch <= ' z ' ) return TRANSLATER . charAt ( ch - ' a ' ) ; else return ch ; } private static String translate ( String s ) { int l = s . length ( ) ; StringBuilder translated = new StringBuilder ( l ) ; for ( int i = 0 ; i < l ; i ++ ) { translated . append ( translate ( s . charAt ( i ) ) ) ; } return translated . toString ( ) ; } public static void main ( String [ ] args ) { try { BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) , 64 << 10 ) ; int testsNumber = Integer . parseInt ( br . readLine ( ) . trim ( ) ) ; for ( int test = 1 ; test <= testsNumber ; test ++ ) { System . out . println ( " Case ▁ # " + test + " : " + translate ( br . readLine ( ) ) ) ; } } catch ( Exception e ) { System . err . println ( " Error : " + e . getMessage ( ) ) ; } } }


import java . util . Scanner ; import java . io . PrintStream ; import java . io . OutputStream ; import java . io . IOException ; import java . io . FileOutputStream ; import java . util . Arrays ; import java . io . PrintWriter ; import java . io . FileInputStream ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream ; try { inputStream = new FileInputStream ( " gcj1 . in " ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } OutputStream outputStream ; try { outputStream = new FileOutputStream ( " gcj1 . out " ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } Scanner in = new Scanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; GCJ1 solver = new GCJ1 ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } } class GCJ1 { public void solve ( int testNumber , Scanner in , PrintWriter out ) { char [ ] map = { ' y ' , ' h ' , ' e ' , ' s ' , ' o ' , ' c ' , ' v ' , ' x ' , ' d ' , ' u ' , ' i ' , ' g ' , ' l ' , ' b ' , ' k ' , ' r ' , ' z ' , ' t ' , ' n ' , ' w ' , ' j ' , ' p ' , ' f ' , ' m ' , ' a ' , ' q ' } ; int cases = in . nextInt ( ) ; in . nextLine ( ) ; for ( int i = 0 ; i < cases ; i ++ ) { String input = in . nextLine ( ) ; char [ ] output = new char [ input . length ( ) ] ; for ( int j = 0 ; j < output . length ; j ++ ) { if ( input . charAt ( j ) == ' ▁ ' ) output [ j ] = ' ▁ ' ; else output [ j ] = map [ input . charAt ( j ) - ' a ' ] ; } out . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + new String ( output ) ) ; } } }


import java . util . * ; import java . io . * ; import java . math . * ; import java . awt . * ; import static java . lang . Math . * ; import static java . lang . Integer . parseInt ; import static java . lang . Double . parseDouble ; import static java . lang . Long . parseLong ; import static java . lang . System . * ; import static java . util . Arrays . * ; import static java . util . Collection . * ; public class A { static char [ ] Cipher = new char [ 256 ] ; public static void train ( ) { String in = " ejpmysljylckdkxveddknmcrejsicpdrysirbcpcypcrtcsradkhwyfrepkymveddknkmkrkcddekrkdeoyakwaejtysrreujdrlkgcjvyeq " , out = " ourlanguageisimpossibletounderstandtherearetwentysixfactorialpossibilitiessoitisokayifyouwanttojustgiveupaoz " ; fill ( Cipher , ( char ) 0 ) ; for ( int i = 0 ; i < in . length ( ) ; ++ i ) Cipher [ in . charAt ( i ) ] = out . charAt ( i ) ; for ( char i = ' a ' ; i <= ' z ' ; ++ i ) { if ( Cipher [ i ] == 0 ) { char j , k ; for ( j = ' a ' ; j <= ' z ' ; ++ j ) { for ( k = ' a ' ; k <= ' z ' ; ++ k ) if ( Cipher [ k ] == j ) break ; if ( k > ' z ' ) break ; } Cipher [ i ] = j ; } } for ( int i = ' a ' ; i <= ' z ' ; ++ i ) if ( Cipher [ i ] != 0 ) Cipher [ i - ' a ' + ' A ' ] = ( char ) ( i + ( ' A ' - ' a ' ) ) ; } public static void main ( String [ ] args ) throws IOException { train ( ) ; BufferedReader br = new BufferedReader ( new InputStreamReader ( in ) ) ; int T = parseInt ( br . readLine ( ) ) ; for ( int t = 0 ; t ++ < T ; ) { out . print ( " Case ▁ # " + t + " : ▁ " ) ; for ( char c : br . readLine ( ) . toCharArray ( ) ) out . print ( Cipher [ c ] == 0 ? c : Cipher [ c ] ) ; out . println ( ) ; } } }


import java . util . * ; public class C { private static Scanner in = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int C = in . nextInt ( ) ; for ( int thisCase = 1 ; thisCase <= C ; thisCase ++ ) { System . out . printf ( " Case ▁ # % d : ▁ % d % n " , thisCase , largestCircle ( ) ) ; } } private static int largestCircle ( ) { int N = in . nextInt ( ) ; int bff [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { bff [ i ] = in . nextInt ( ) - 1 ; } int status [ ] = new int [ N ] ; int chainLength [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { status [ i ] = - 2 ; chainLength [ i ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { if ( bff [ bff [ i ] ] == i ) { status [ i ] = - 1 ; } } int maxLoopSize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( status [ i ] != - 1 ) { int current = bff [ i ] ; status [ i ] = i ; int steps = 1 ; boolean done = false ; while ( ! done ) { if ( current == i ) { if ( steps > maxLoopSize ) { maxLoopSize = steps ; } done = true ; } else if ( status [ current ] == i ) { done = true ; } else if ( status [ current ] == - 1 ) { if ( steps > chainLength [ current ] ) { chainLength [ current ] = steps ; } done = true ; } else { steps ++ ; status [ current ] = i ; current = bff [ current ] ; } } } } int frankenCircle = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( status [ i ] == - 1 ) { frankenCircle += ( chainLength [ i ] + 1 ) ; } } return Math . max ( frankenCircle , maxLoopSize ) ; } }

def chain ( b , x ) : NEW_LINE INDENT sz = 1 NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT if b [ i ] == x and b [ x ] != i : NEW_LINE INDENT sz = max ( sz , 1 + chain ( b , i ) ) NEW_LINE DEDENT DEDENT return sz NEW_LINE DEDENT def solve ( b ) : NEW_LINE INDENT mx = 0 NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT seen = [ 0 ] * len ( b ) NEW_LINE seen [ i ] = 1 NEW_LINE cur = i NEW_LINE while seen [ b [ cur ] ] == 0 : NEW_LINE INDENT seen [ b [ cur ] ] = seen [ cur ] + 1 NEW_LINE cur = b [ cur ] NEW_LINE DEDENT mx = max ( seen [ cur ] - seen [ b [ cur ] ] + 1 , mx ) NEW_LINE DEDENT c = 0 NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT if b [ b [ i ] ] == i : NEW_LINE INDENT c += chain ( b , i ) + chain ( b , b [ i ] ) NEW_LINE DEDENT DEDENT return max ( c / 2 , mx ) NEW_LINE DEDENT T_max = int ( raw_input ( ) ) NEW_LINE for T in range ( T_max ) : NEW_LINE INDENT N = int ( raw_input ( ) ) NEW_LINE bffs = map ( int , raw_input ( ) . split ( " ▁ " ) ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT bffs [ i ] -= 1 NEW_LINE DEDENT out = " Case ▁ # " + str ( T + 1 ) + " : ▁ " + str ( solve ( bffs ) ) NEW_LINE print out NEW_LINE DEDENT


task = ' C ' NEW_LINE type = 2 NEW_LINE if type == 0 : NEW_LINE INDENT inp = open ( ' sample . in ' , ' r ' ) NEW_LINE DEDENT elif type == 1 : NEW_LINE INDENT inp = open ( ' % s - small . in ' % ( task , ) ) NEW_LINE DEDENT else : NEW_LINE INDENT inp = open ( ' % s - large . in ' % ( task ) , ) NEW_LINE DEDENT outp = open ( ' % s . out ' % ( task , ) , ' w ' ) NEW_LINE T = int ( inp . readline ( ) [ : - 1 ] ) NEW_LINE for t in range ( T ) : NEW_LINE INDENT N = int ( inp . readline ( ) [ : - 1 ] ) NEW_LINE edges = [ - 1 for i in range ( N ) ] NEW_LINE revedges = [ list ( ) for i in range ( N ) ] NEW_LINE st = inp . readline ( ) [ : - 1 ] . split ( ) NEW_LINE def dfs ( v ) : NEW_LINE INDENT anses = [ 0 ] NEW_LINE for v2 in revedges [ v ] : NEW_LINE INDENT anses . append ( dfs ( v2 ) + 1 ) NEW_LINE DEDENT return max ( anses ) NEW_LINE DEDENT for j , a in zip ( range ( N ) , st ) : NEW_LINE INDENT a = int ( a ) - 1 NEW_LINE edges [ j ] = a NEW_LINE revedges [ a ] . append ( j ) NEW_LINE DEDENT pairs = set ( ) NEW_LINE loncyc = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT used = [ - 1 for i in range ( N ) ] NEW_LINE cur = i NEW_LINE curs = 0 NEW_LINE while used [ cur ] == - 1 : NEW_LINE INDENT used [ cur ] = curs NEW_LINE curs += 1 NEW_LINE cur = edges [ cur ] NEW_LINE DEDENT if curs - used [ cur ] == 2 : NEW_LINE INDENT pairs . add ( cur ) NEW_LINE DEDENT loncyc = max ( loncyc , curs - used [ cur ] ) NEW_LINE DEDENT pairtokens_sum = 0 NEW_LINE while len ( pairs ) > 0 : NEW_LINE INDENT cur1 = pairs . pop ( ) NEW_LINE cur2 = edges [ cur1 ] NEW_LINE revedges [ cur1 ] . remove ( cur2 ) NEW_LINE revedges [ cur2 ] . remove ( cur1 ) NEW_LINE pairs . remove ( cur2 ) NEW_LINE curs = 2 + dfs ( cur1 ) + dfs ( cur2 ) NEW_LINE pairtokens_sum += curs NEW_LINE DEDENT ans = max ( pairtokens_sum , loncyc ) NEW_LINE outp . write ( " Case ▁ # % s : ▁ % s \n " % ( t + 1 , ans ) ) NEW_LINE DEDENT


import sys NEW_LINE sys . stdin = open ( " data . txt " ) NEW_LINE sys . stdout = open ( " out . txt " , " w " ) NEW_LINE input = sys . stdin . readline NEW_LINE sys . setrecursionlimit ( 10000 ) NEW_LINE g = [ ] NEW_LINE friend = [ ] NEW_LINE def dfs ( u , n ) : NEW_LINE INDENT v = u NEW_LINE for i in range ( 1 , n + 2 ) : NEW_LINE INDENT v = friend [ v ] NEW_LINE if u == v : return i NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT def depth ( u , bad ) : NEW_LINE INDENT out = 0 NEW_LINE for v in g [ u ] : NEW_LINE INDENT if v != bad : NEW_LINE INDENT out = max ( out , depth ( v , bad ) ) NEW_LINE DEDENT DEDENT return out + 1 NEW_LINE DEDENT def getpairs ( ) : NEW_LINE INDENT out = 0 NEW_LINE for i in range ( len ( g ) ) : NEW_LINE INDENT if friend [ friend [ i ] ] == i : NEW_LINE INDENT out += depth ( i , friend [ i ] ) + depth ( friend [ i ] , i ) NEW_LINE DEDENT DEDENT return out // 2 NEW_LINE DEDENT for c in range ( int ( input ( ) ) ) : NEW_LINE INDENT n = int ( input ( ) ) NEW_LINE friend = list ( map ( lambda s : int ( s ) - 1 , input ( ) . split ( ) ) ) NEW_LINE ans = 0 NEW_LINE g = [ [ ] for _ in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT g [ friend [ i ] ] . append ( i ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT ans = max ( ans , dfs ( i , n ) ) NEW_LINE DEDENT ans = max ( ans , getpairs ( ) ) NEW_LINE print ( " Case ▁ # % s : ▁ % s " % ( c + 1 , ans ) ) NEW_LINE DEDENT


import sys NEW_LINE fin = open ( sys . argv [ 1 ] , " r " ) NEW_LINE fout = open ( " p3 . out " , " w " ) NEW_LINE T = int ( fin . readline ( ) ) NEW_LINE for tt in xrange ( T ) : NEW_LINE INDENT N = int ( fin . readline ( ) ) NEW_LINE out = map ( int , fin . readline ( ) . split ( ) ) NEW_LINE out = map ( lambda x : x - 1 , out ) NEW_LINE ans = 0 NEW_LINE best = [ 0 for j in xrange ( N ) ] NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT cur = i NEW_LINE vis = [ 0 for j in xrange ( N ) ] NEW_LINE vis [ cur ] = 1 NEW_LINE while vis [ out [ cur ] ] == 0 : NEW_LINE INDENT vis [ out [ cur ] ] = 1 NEW_LINE cur = out [ cur ] NEW_LINE DEDENT if out [ cur ] == i : NEW_LINE INDENT ans = max ( ans , sum ( vis ) ) NEW_LINE DEDENT if out [ out [ cur ] ] == cur : NEW_LINE INDENT best [ cur ] = max ( best [ cur ] , sum ( vis ) ) NEW_LINE DEDENT DEDENT tot = 0 NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT if out [ out [ i ] ] == i : NEW_LINE INDENT tot += best [ i ] + best [ out [ i ] ] - 2 NEW_LINE DEDENT DEDENT tot /= 2 NEW_LINE ans = max ( ans , tot ) NEW_LINE fout . write ( " Case ▁ # " + str ( tt + 1 ) + " : ▁ " + str ( ans ) + " \n " ) NEW_LINE DEDENT


def longestdfs ( inedges , start , seen ) : NEW_LINE INDENT seen . add ( start ) NEW_LINE if not inedges [ start ] : NEW_LINE INDENT return 1 NEW_LINE DEDENT candidates = [ ] NEW_LINE for inedge in inedges [ start ] : NEW_LINE INDENT candidates . append ( longestdfs ( inedges , inedge , seen ) ) NEW_LINE DEDENT return max ( candidates ) + 1 NEW_LINE DEDENT cases = int ( input ( ) ) NEW_LINE for case in range ( cases ) : NEW_LINE INDENT n = int ( input ( ) ) NEW_LINE toedges = [ int ( x ) - 1 for x in input ( ) . split ( ) ] NEW_LINE inedges = [ [ ] for x in toedges ] NEW_LINE for i , to in enumerate ( toedges ) : NEW_LINE INDENT inedges [ to ] . append ( i ) NEW_LINE DEDENT mutuals = [ ] NEW_LINE for i , to in enumerate ( toedges ) : NEW_LINE INDENT if toedges [ to ] == i and to > i : NEW_LINE INDENT mutuals . append ( ( to , i ) ) NEW_LINE inedges [ to ] . remove ( i ) NEW_LINE inedges [ i ] . remove ( to ) NEW_LINE DEDENT DEDENT mdepths = [ ] NEW_LINE seen = set ( ) NEW_LINE for a , b in mutuals : NEW_LINE INDENT mdepths . append ( longestdfs ( inedges , a , seen ) + longestdfs ( inedges , b , seen ) ) NEW_LINE DEDENT cyclemax = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if i in seen : NEW_LINE INDENT continue NEW_LINE DEDENT seen . add ( i ) NEW_LINE length = 1 NEW_LINE tempi = toedges [ i ] NEW_LINE path = [ i , tempi ] NEW_LINE while tempi != i : NEW_LINE INDENT if tempi in seen : NEW_LINE INDENT length -= path . index ( tempi ) NEW_LINE break NEW_LINE DEDENT seen . add ( tempi ) NEW_LINE length += 1 NEW_LINE tempi = toedges [ tempi ] NEW_LINE path . append ( tempi ) NEW_LINE DEDENT cyclemax = max ( length , cyclemax ) NEW_LINE DEDENT ans = max ( sum ( mdepths ) , cyclemax ) NEW_LINE print ( " Case ▁ # { } : ▁ { } " . format ( case + 1 , ans ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; public class C { int solveOne ( Scanner in ) { int n = in . nextInt ( ) ; int f [ ] = new int [ 1 + n ] ; for ( int i = 1 ; i <= n ; i ++ ) { f [ i ] = in . nextInt ( ) ; } boolean inPair [ ] = new boolean [ 1 + n ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( f [ f [ i ] ] == i ) { inPair [ i ] = true ; } } int chainLen [ ] = new int [ 1 + n ] ; boolean used [ ] = new boolean [ 1 + n ] ; int maxCircleLen = 0 ; for ( int first = 1 ; first <= n ; first ++ ) { if ( inPair [ first ] ) { continue ; } Arrays . fill ( used , false ) ; used [ first ] = true ; int cur = first ; int len = 1 ; while ( true ) { int next = f [ cur ] ; if ( inPair [ next ] ) { chainLen [ next ] = Math . max ( chainLen [ next ] , len ) ; break ; } if ( next == first ) { maxCircleLen = Math . max ( maxCircleLen , len ) ; break ; } if ( used [ next ] ) { break ; } used [ next ] = true ; len ++ ; cur = next ; } } int joinLen = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( inPair [ i ] ) { joinLen += 1 + chainLen [ i ] ; } } return Math . max ( maxCircleLen , joinLen ) ; } void solve ( Scanner in , PrintWriter out ) { int nTests = in . nextInt ( ) ; for ( int iTest = 1 ; iTest <= nTests ; iTest ++ ) { out . printf ( " Case ▁ # % d : ▁ % d % n " , iTest , solveOne ( in ) ) ; } } void run ( ) { try ( Scanner in = new Scanner ( System . in ) ; PrintWriter out = new PrintWriter ( System . out ) ; ) { solve ( in , out ) ; } } public static void main ( String args [ ] ) { new C ( ) . run ( ) ; } }


package lab6zp ; import java . util . ArrayList ; import java . util . Collections ; import java . util . Scanner ; public class Lab6ZP { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int n = Integer . parseInt ( in . next ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { boolean is = false ; int m = Integer . parseInt ( in . next ( ) ) ; int [ ] k = new int [ m ] ; int [ ] h = new int [ m ] ; int min = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { h [ j ] = 0 ; k [ j ] = Integer . parseInt ( in . next ( ) ) - 1 ; } System . out . print ( " Case ▁ # " + ( i + 1 ) + " : ▁ " ) ; int fullcounter = 0 ; ArrayList < Integer > q = new ArrayList < > ( ) ; for ( int j = 0 ; j < m ; j ++ ) { int p = j ; for ( int z = 0 ; z < m + 2 ; z ++ ) { if ( k [ k [ p ] ] == p ) { if ( z > h [ p ] || ! q . contains ( p ) ) { fullcounter -= h [ p ] ; fullcounter += z ; if ( ! q . contains ( p ) ) { q . add ( p ) ; fullcounter ++ ; } h [ p ] = z ; } if ( min < fullcounter ) { min = fullcounter ; } z = m + 1 ; continue ; } p = k [ p ] ; if ( z > 0 && p == k [ j ] ) { if ( min < z ) { min = z ; } z = m + 1 ; } } } System . out . println ( min ) ; } } }


package Round1A2016 ; import java . lang . reflect . Array ; import java . util . Arrays ; import java . util . Scanner ; public class C { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int T = sc . nextInt ( ) ; for ( int testCase = 1 ; testCase <= T ; ++ testCase ) { final int n = sc . nextInt ( ) ; int [ ] bff = new int [ n ] ; boolean [ ] hasIncoming = new boolean [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { bff [ i ] = sc . nextInt ( ) - 1 ; hasIncoming [ bff [ i ] ] = true ; } int [ ] loop = new int [ n ] ; int maxPeriod = 0 ; for ( int i = 0 ; i < n ; ++ i ) { boolean [ ] visited = new boolean [ n ] ; int cur = 0 ; int [ ] index = new int [ n ] ; int j = i ; while ( ! visited [ j ] ) { visited [ j ] = true ; index [ j ] = cur ++ ; j = bff [ j ] ; } final int period = cur - index [ j ] ; for ( int k = 0 ; k < period ; ++ k ) { loop [ j ] = period ; j = bff [ j ] ; } maxPeriod = Math . max ( maxPeriod , period ) ; } int [ ] maxChain = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( hasIncoming [ i ] ) continue ; int j = i ; int chain = 0 ; while ( loop [ j ] == 0 ) { ++ chain ; j = bff [ j ] ; } maxChain [ j ] = Math . max ( maxChain [ j ] , chain ) ; } int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( loop [ i ] == 2 ) { sum += 1 + maxChain [ i ] ; } } System . out . printf ( " Case ▁ # % d : ▁ % d \n " , testCase , Math . max ( sum , maxPeriod ) ) ; } } }


import java . util . * ; class C { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int tcc = sc . nextInt ( ) ; for ( int tc = 1 ; tc <= tcc ; ++ tc ) { int n = sc . nextInt ( ) ; Kid [ ] kids = new Kid [ n ] ; for ( int i = 0 ; i < n ; ++ i ) kids [ i ] = new Kid ( i + 1 ) ; for ( int i = 0 ; i < n ; ++ i ) kids [ i ] . bff = kids [ sc . nextInt ( ) - 1 ] ; int maxCycle = 0 ; for ( Kid first : kids ) { Set < Kid > set = new HashSet < Kid > ( ) ; Kid k = first ; for ( ; ; ) { if ( set . contains ( k ) ) { if ( k == first ) { first . cycle = set . size ( ) ; maxCycle = Math . max ( maxCycle , first . cycle ) ; } break ; } set . add ( k ) ; k = k . bff ; } } for ( Kid first : kids ) { Set < Kid > set = new HashSet < Kid > ( ) ; Kid k = first ; for ( ; ; ) { if ( set . contains ( k ) ) { if ( k . cycle == 2 ) { k . brings = Math . max ( k . brings , set . size ( ) - 1 ) ; } break ; } set . add ( k ) ; k = k . bff ; } } int brings = 0 ; for ( Kid k : kids ) if ( k . cycle == 2 ) brings += k . brings ; System . out . printf ( " Case ▁ # % d : ▁ % d % n " , tc , Math . max ( maxCycle , brings ) ) ; } } } class Kid { public Kid bff ; public int index ; public int cycle = 0 ; public int brings = 0 ; public Kid ( int index ) { this . index = index ; } public String toString ( ) { return this . index + " ( " + this . cycle + " , " + this . brings + " ) " ; } }


import java . io . * ; import java . util . * ; public class Goro { public static void main ( String [ ] args ) { try { BufferedReader br = new BufferedReader ( new FileReader ( " D - large . in " ) ) ; int T = Integer . parseInt ( br . readLine ( ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( " output " ) ) ; for ( int I = 1 ; I <= T ; I ++ ) { int N = Integer . parseInt ( br . readLine ( ) ) ; StringTokenizer st = new StringTokenizer ( br . readLine ( ) , " ▁ " , false ) ; int ret = N ; for ( int i = 0 ; i < N ; i ++ ) { int k = Integer . parseInt ( st . nextToken ( ) ) ; if ( k == i + 1 ) ret -- ; } pw . println ( " Case ▁ # " + I + " : ▁ " + ret + " . 000000" ) ; } br . close ( ) ; pw . flush ( ) ; pw . close ( ) ; } catch ( IOException ie ) { ie . printStackTrace ( ) ; } } }

import sys NEW_LINE def c ( ) : NEW_LINE INDENT t = int ( sys . stdin . readline ( ) ) NEW_LINE for i in xrange ( t ) : NEW_LINE INDENT n = int ( sys . stdin . readline ( ) ) NEW_LINE arr = map ( int , sys . stdin . readline ( ) . strip ( ) . split ( ) ) NEW_LINE arr = [ x - 1 for x in arr ] NEW_LINE assert len ( arr ) == n NEW_LINE count = 0 NEW_LINE seen = [ False for _ in xrange ( n ) ] NEW_LINE for ix , x in enumerate ( arr ) : NEW_LINE INDENT if x == ix : NEW_LINE INDENT continue NEW_LINE DEDENT while not seen [ x ] : NEW_LINE INDENT seen [ x ] = True NEW_LINE x = arr [ x ] NEW_LINE count += 1 NEW_LINE DEDENT DEDENT print " Case ▁ # % d : ▁ % d " % ( i + 1 , count ) NEW_LINE DEDENT DEDENT c ( ) NEW_LINE


def get_permutation ( L1 , L2 ) : NEW_LINE INDENT if sorted ( L1 ) != sorted ( L2 ) : NEW_LINE INDENT raise ValueError ( " L2 ▁ must ▁ be ▁ permutation ▁ of ▁ L1 ▁ ( % s , ▁ % s ) " % ( L1 , L2 ) ) NEW_LINE DEDENT permutation = map ( dict ( ( v , i ) for i , v in enumerate ( L1 ) ) . get , L2 ) NEW_LINE assert [ L1 [ p ] for p in permutation ] == L2 NEW_LINE return permutation NEW_LINE DEDENT def get_cycles ( permutation ) : NEW_LINE INDENT nswaps = 0 NEW_LINE seen = set ( ) NEW_LINE cycles = [ ] NEW_LINE for i in xrange ( len ( permutation ) ) : NEW_LINE INDENT if i not in seen : NEW_LINE INDENT j = i NEW_LINE cycles . append ( [ i + 1 ] ) NEW_LINE while permutation [ j ] != i : NEW_LINE INDENT j = permutation [ j ] NEW_LINE seen . add ( j ) NEW_LINE nswaps += 1 NEW_LINE cycles [ - 1 ] . append ( j + 1 ) NEW_LINE DEDENT DEDENT DEDENT return cycles NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT f = open ( " input . txt " ) NEW_LINE testlen = int ( f . readline ( ) . strip ( ) ) NEW_LINE tests = [ ] NEW_LINE i = 0 NEW_LINE for line in f : NEW_LINE INDENT if i % 2 : NEW_LINE INDENT tests . append ( line . strip ( ) ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT tests = tests [ : testlen ] NEW_LINE for test , i in zip ( tests , range ( testlen ) ) : NEW_LINE INDENT print " Case ▁ # % d : ▁ % s " % ( i + 1 , solve ( parse ( test ) ) ) NEW_LINE DEDENT DEDENT def parse ( test ) : NEW_LINE INDENT return map ( int , test . split ( " ▁ " ) ) NEW_LINE DEDENT def solve ( test ) : NEW_LINE INDENT perm = get_permutation ( test , sorted ( test ) ) NEW_LINE cycles = get_cycles ( perm ) NEW_LINE v = 0 NEW_LINE for cy in cycles : NEW_LINE INDENT if len ( cy ) > 1 : NEW_LINE INDENT v += len ( cy ) NEW_LINE DEDENT DEDENT return " % d . 000000" % ( v ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import sys NEW_LINE def process ( num , fin , fout ) : NEW_LINE INDENT n = int ( fin . readline ( ) ) NEW_LINE data = [ int ( x ) for x in fin . readline ( ) . split ( ) ] NEW_LINE unmatched = 0 NEW_LINE for i in xrange ( len ( data ) ) : NEW_LINE INDENT if data [ i ] != i + 1 : NEW_LINE INDENT unmatched += 1 NEW_LINE DEDENT DEDENT fout . write ( str ( unmatched ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT if len ( sys . argv ) != 3 : NEW_LINE INDENT print " Please ▁ indicate ▁ input ▁ and ▁ output " NEW_LINE exit ( 0 ) NEW_LINE DEDENT fin = open ( sys . argv [ 1 ] , ' r ' ) NEW_LINE fout = open ( sys . argv [ 2 ] , ' w ' ) NEW_LINE times = int ( fin . readline ( ) ) NEW_LINE for i in xrange ( times ) : NEW_LINE INDENT fout . write ( " Case ▁ # % d : ▁ " % ( i + 1 ) ) NEW_LINE process ( i , fin , fout ) NEW_LINE fout . write ( " \n " ) NEW_LINE DEDENT fin . close ( ) NEW_LINE fout . close ( ) NEW_LINE DEDENT


infile = " D - large . in " NEW_LINE debug = False NEW_LINE def find_subsets ( L ) : NEW_LINE INDENT used = [ False ] * len ( L ) NEW_LINE subsets = [ ] NEW_LINE for ix in xrange ( len ( L ) ) : NEW_LINE INDENT if used [ ix ] : NEW_LINE INDENT continue NEW_LINE DEDENT used [ ix ] = True NEW_LINE ix_subset = [ ix ] NEW_LINE next_ix = L [ ix ] - 1 NEW_LINE while next_ix != ix : NEW_LINE INDENT ix_subset . append ( next_ix ) NEW_LINE used [ next_ix ] = True NEW_LINE next_ix = L [ next_ix ] - 1 NEW_LINE DEDENT subsets . append ( ix_subset ) NEW_LINE DEDENT assert False not in used NEW_LINE assert sum ( map ( len , subsets ) ) == len ( L ) NEW_LINE return subsets NEW_LINE DEDENT def how_many ( set_size ) : NEW_LINE INDENT if set_size == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return set_size NEW_LINE DEDENT DEDENT def solve ( nstr , data ) : NEW_LINE INDENT N = int ( nstr ) NEW_LINE data = map ( int , data . split ( ) ) NEW_LINE assert len ( data ) == N NEW_LINE subsets = find_subsets ( data ) NEW_LINE if debug : NEW_LINE INDENT print subsets NEW_LINE print map ( len , subsets ) NEW_LINE DEDENT avg = 0.0 NEW_LINE for subset in subsets : NEW_LINE INDENT avg += how_many ( len ( subset ) ) NEW_LINE DEDENT return " % .7f " % avg NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT i = file ( infile ) NEW_LINE N = int ( i . readline ( ) ) NEW_LINE for n in xrange ( N ) : NEW_LINE INDENT soln = solve ( i . readline ( ) , i . readline ( ) ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( n + 1 , soln ) NEW_LINE DEDENT DEDENT main ( ) NEW_LINE


data = [ l . strip ( ) for l in open ( " infile " , " r " ) . readlines ( ) ] NEW_LINE out = open ( " outfile " , " w " ) NEW_LINE ncases = int ( data . pop ( 0 ) ) NEW_LINE for case in range ( ncases ) : NEW_LINE INDENT numelements = int ( data . pop ( 0 ) ) NEW_LINE elements = [ int ( s ) for s in data . pop ( 0 ) . split ( ' ▁ ' ) ] NEW_LINE inwrongplace = 0 NEW_LINE for i in range ( len ( elements ) ) : NEW_LINE INDENT if elements [ i ] != i + 1 : NEW_LINE INDENT inwrongplace += 1 NEW_LINE DEDENT DEDENT out . write ( " Case ▁ # " + str ( case + 1 ) + " : ▁ " + str ( " % .6f " % inwrongplace ) + " \n " ) NEW_LINE DEDENT


import java . util . * ; import java . io . * ; public class Solution { public void doIt ( ) throws Exception { Scanner sc = new Scanner ( new FileReader ( " input . txt " ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( " output . txt " ) ) ; final int MAX = 1000 ; double [ ] invFact = new double [ MAX + 1 ] ; invFact [ 0 ] = 1.0 ; for ( int i = 1 ; i <= MAX ; i ++ ) invFact [ i ] = invFact [ i - 1 ] / ( double ) i ; double [ ] derang = new double [ MAX + 1 ] ; derang [ 0 ] = invFact [ 0 ] ; for ( int i = 1 ; i <= MAX ; i ++ ) derang [ i ] = derang [ i - 1 ] + ( i % 2 == 1 ? - 1 : 1 ) * invFact [ i ] ; double [ ] avgTime = new double [ MAX + 1 ] ; avgTime [ 0 ] = 0.0 ; for ( int i = 1 ; i <= MAX ; i ++ ) { double sum = 1.0 ; for ( int j = 1 ; j <= i ; j ++ ) sum += avgTime [ i - j ] * invFact [ j ] * derang [ i - j ] ; avgTime [ i ] = sum / ( 1 - invFact [ 0 ] * derang [ i ] ) ; } int caseCnt = sc . nextInt ( ) ; for ( int caseNum = 0 ; caseNum < caseCnt ; caseNum ++ ) { int N = sc . nextInt ( ) ; int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { int x = sc . nextInt ( ) ; if ( x != i ) ans ++ ; } pw . println ( " Case ▁ # " + ( caseNum + 1 ) + " : ▁ " + ans ) ; } pw . flush ( ) ; pw . close ( ) ; sc . close ( ) ; } public static void main ( String [ ] args ) throws Exception { new Solution ( ) . doIt ( ) ; } }


import java . util . * ; public class d { static double [ ] memo = new double [ 1010 ] ; static double [ ] [ ] memoo = new double [ 1010 ] [ 1010 ] ; public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int t = 1 ; t <= T ; t ++ ) { int n = in . nextInt ( ) ; int k = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( i != in . nextInt ( ) ) k ++ ; System . out . printf ( " Case ▁ # % d : ▁ % f % n " , t , ( double ) k ) ; } } public static double f ( int k , int j ) { if ( j < 0 || j > k ) return 0 ; if ( k == 0 && j == 0 ) return 1 ; if ( memoo [ k ] [ j ] != 0 ) return memoo [ k ] [ j ] ; double ans = f ( k - 1 , j - 1 ) + f ( k - 1 , j ) * ( k - 1 - j ) + f ( k - 1 , j + 1 ) * ( j + 1 ) ; ans /= k ; return memoo [ k ] [ j ] = ans ; } public static double go ( int k ) { if ( k == 0 ) return 0 ; if ( memo [ k ] != 0 ) return memo [ k ] ; double total = 1 ; for ( int i = 1 ; i <= k ; i ++ ) { total += f ( k , i ) * go ( k - i ) ; } total /= ( 1 - f ( k , 0 ) ) ; return memo [ k ] = total ; } }


import java . io . * ; import java . util . * ; import java . math . * ; public class Main implements Runnable { String file = " D - large " ; private void solve ( ) throws IOException { int tn = nextInt ( ) ; for ( int testN = 1 ; testN <= tn ; ++ testN ) { int n = nextInt ( ) ; int ans = n ; for ( int i = 0 ; i < n ; ++ i ) { if ( nextInt ( ) == i + 1 ) { -- ans ; } } out . printf ( " Case ▁ # % d : ▁ % d . 000000 \n " , testN , ans ) ; } } public static void main ( String [ ] args ) { new Thread ( new Main ( ) ) . start ( ) ; } StringTokenizer tok ; PrintWriter out ; BufferedReader br ; @ Override public void run ( ) { try { Locale . setDefault ( Locale . US ) ; out = new PrintWriter ( new FileWriter ( file + " . out " ) ) ; br = new BufferedReader ( new FileReader ( file + " . in " ) ) ; tok = new StringTokenizer ( " " ) ; while ( hasNext ( ) ) { solve ( ) ; } out . close ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; System . exit ( 1 ) ; } } boolean hasNext ( ) throws IOException { while ( ! tok . hasMoreElements ( ) ) { String line = br . readLine ( ) ; if ( line == null ) { return false ; } tok = new StringTokenizer ( line ) ; } return true ; } String next ( ) throws IOException { if ( hasNext ( ) ) { return tok . nextToken ( ) ; } throw new IOException ( " No ▁ more ▁ tokens " ) ; } int nextInt ( ) throws IOException { return Integer . parseInt ( next ( ) ) ; } long nextLong ( ) throws IOException { return Long . parseLong ( next ( ) ) ; } double nextDouble ( ) throws IOException { return Double . parseDouble ( next ( ) ) ; } String nextLine ( ) throws IOException { if ( hasNext ( ) ) { return tok . nextToken ( " \n " ) ; } throw new IOException ( " No ▁ more ▁ tokens " ) ; } }


import java . io . File ; import java . io . PrintWriter ; import java . util . Arrays ; import java . util . Scanner ; public class D { static double [ ] dpc = new double [ 1024 ] ; static double [ ] dpa = new double [ 1024 ] ; public static void main ( String [ ] args ) throws Exception { Scanner s = new Scanner ( new File ( " D . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( " D . out " ) ) ; dpc [ 1 ] = 0 ; dpc [ 2 ] = 2 ; dpa [ 1 ] = 0 ; dpa [ 2 ] = 1 ; for ( int i = 3 ; i <= 1000 ; i ++ ) { dpc [ i ] = 1 ; for ( int j = 1 ; j < i ; j ++ ) { dpc [ i ] += ( dpc [ j ] + dpa [ i - j ] ) / i ; } dpc [ i ] *= ( ( double ) i ) / ( i - 1 ) ; dpa [ i ] = dpc [ i ] - 1 ; } int T = s . nextInt ( ) ; int [ ] arr = new int [ 1024 ] ; boolean [ ] mark = new boolean [ 1024 ] ; for ( int tc = 1 ; tc <= T ; tc ++ ) { out . print ( " Case ▁ # " + tc + " : ▁ " ) ; int N = s . nextInt ( ) ; for ( int i = 1 ; i <= N ; i ++ ) arr [ i ] = s . nextInt ( ) ; double answer = 0 ; Arrays . fill ( mark , false ) ; for ( int i = 1 ; i <= N ; i ++ ) { int cycleLength = 0 ; int cur = i ; while ( ! mark [ cur ] ) { mark [ cur ] = true ; cur = arr [ cur ] ; cycleLength ++ ; } answer += dpc [ cycleLength ] ; } out . println ( answer ) ; } out . close ( ) ; System . out . println ( dpc [ 1000 ] ) ; } }


import java . util . * ; public class Smooth { static Scanner sc = new Scanner ( System . in ) ; static int [ ] [ ] memo = new int [ 100 ] [ 257 ] ; public static void main ( String [ ] args ) { int T = sc . nextInt ( ) ; for ( int i = 1 ; i <= T ; i ++ ) { System . out . print ( " Case ▁ # " + i + " : ▁ " ) ; solveCase ( ) ; } } static final int SPC = 256 ; static void solveCase ( ) { del = sc . nextInt ( ) ; ins = sc . nextInt ( ) ; maxDist = sc . nextInt ( ) ; n = sc . nextInt ( ) ; arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = sc . nextInt ( ) ; for ( int [ ] arr : memo ) Arrays . fill ( arr , - 1 ) ; System . out . println ( solve ( 0 , SPC ) ) ; } static int del , ins , maxDist , n ; static int [ ] arr ; static int solve ( int index , int prev ) { if ( index >= n ) return 0 ; if ( memo [ index ] [ prev ] == - 1 ) { int res = del + solve ( index + 1 , prev ) ; for ( int val = 0 ; val < SPC ; val ++ ) { res = Math . min ( res , Math . abs ( arr [ index ] - val ) + insCost ( val , prev ) + solve ( index + 1 , val ) ) ; } memo [ index ] [ prev ] = res ; } return memo [ index ] [ prev ] ; } static int insCost ( int cur , int prev ) { if ( prev == SPC || cur == prev ) return 0 ; if ( maxDist == 0 ) return 100000000 ; return ins * ( ( Math . abs ( cur - prev ) + maxDist - 1 ) / maxDist - 1 ) ; } }

import sys NEW_LINE import psyco NEW_LINE psyco . full ( ) NEW_LINE def dbg ( a ) : sys . stderr . write ( str ( a ) ) NEW_LINE def readint ( ) : return int ( raw_input ( ) ) NEW_LINE def readfloat ( ) : return float ( raw_input ( ) ) NEW_LINE def readarray ( foo ) : return [ foo ( x ) for x in raw_input ( ) . split ( ) ] NEW_LINE def doit ( p , last ) : NEW_LINE INDENT if ( p , last ) in mem : return mem [ ( p , last ) ] NEW_LINE if ( p == N ) : return 0 NEW_LINE res = doit ( p + 1 , last ) + D NEW_LINE for i in xrange ( 256 ) : NEW_LINE INDENT if last == - 1 : NEW_LINE INDENT res = min ( res , doit ( p + 1 , i ) + abs ( a [ p ] - i ) ) NEW_LINE continue NEW_LINE DEDENT diff = abs ( last - i ) NEW_LINE if M == 0 and diff > 0 : continue NEW_LINE insertcost = ( ( diff - 1 ) / M ) * I if ( diff > 0 and I > 0 ) else 0 NEW_LINE editcost = abs ( a [ p ] - i ) NEW_LINE res = min ( res , doit ( p + 1 , i ) + editcost + insertcost ) NEW_LINE DEDENT mem [ ( p , last ) ] = res NEW_LINE return res NEW_LINE DEDENT def run_test ( test ) : NEW_LINE INDENT global D , I , M , N , a , mem NEW_LINE ( D , I , M , N ) = readarray ( int ) NEW_LINE a = readarray ( int ) NEW_LINE mem = { } NEW_LINE res = doit ( 0 , - 1 ) NEW_LINE print " Case ▁ # % d : ▁ % d " % ( test + 1 , res ) NEW_LINE DEDENT for test in range ( readint ( ) ) : NEW_LINE INDENT dbg ( " Test ▁ % d \n " % ( test + 1 ) ) NEW_LINE run_test ( test ) NEW_LINE DEDENT


from __future__ import division NEW_LINE import collections NEW_LINE import itertools NEW_LINE import sys NEW_LINE class gcj : NEW_LINE INDENT IN = sys . stdin NEW_LINE number = 0 NEW_LINE @ classmethod NEW_LINE def case ( cls ) : NEW_LINE INDENT cls . number += 1 NEW_LINE return ' Case ▁ # % d : ' % cls . number NEW_LINE DEDENT @ classmethod NEW_LINE def line ( cls , type = str ) : NEW_LINE INDENT line = cls . IN . readline ( ) NEW_LINE return type ( line . strip ( ' \n ' ) ) NEW_LINE DEDENT @ classmethod NEW_LINE def splitline ( cls , type = str ) : NEW_LINE INDENT line = cls . IN . readline ( ) NEW_LINE return [ type ( x ) for x in line . split ( ) ] NEW_LINE DEDENT DEDENT def go ( ) : NEW_LINE INDENT t = gcj . line ( int ) NEW_LINE for _ in xrange ( t ) : NEW_LINE INDENT D , I , m , n = gcj . splitline ( int ) NEW_LINE data = gcj . splitline ( int ) NEW_LINE assert len ( data ) == n NEW_LINE print gcj . case ( ) , solve ( D , I , m , data ) NEW_LINE DEDENT DEDENT def solve ( D , I , m , data ) : NEW_LINE INDENT n = len ( data ) NEW_LINE tab = [ 0 ] * 256 NEW_LINE otab = [ 0 ] * 256 NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT otab , tab = tab , otab NEW_LINE for j in xrange ( 256 ) : NEW_LINE INDENT cur = otab [ j ] + D NEW_LINE base = abs ( data [ i ] - j ) NEW_LINE if cur > base : NEW_LINE INDENT for k in xrange ( 256 ) : NEW_LINE INDENT if m == 0 and k != j : NEW_LINE INDENT continue NEW_LINE DEDENT new = otab [ k ] NEW_LINE if k < j : NEW_LINE INDENT new += ( j - k - 1 ) // m * I NEW_LINE DEDENT elif k > j : NEW_LINE INDENT new += ( k - j - 1 ) // m * I NEW_LINE DEDENT if cur > base + new : NEW_LINE INDENT cur = base + new NEW_LINE DEDENT DEDENT DEDENT tab [ j ] = cur NEW_LINE DEDENT DEDENT return min ( tab [ i ] for i in xrange ( 256 ) ) NEW_LINE DEDENT go ( ) NEW_LINE


from sys import stdin NEW_LINE N = 256 NEW_LINE def main ( ) : NEW_LINE INDENT dd , ii , mm , n = map ( int , stdin . readline ( ) . split ( ) ) NEW_LINE A = map ( int , stdin . readline ( ) . split ( ) ) NEW_LINE x = [ 0 ] * N NEW_LINE for a in A : NEW_LINE INDENT y = [ ] NEW_LINE for v in x : NEW_LINE INDENT y . append ( v + dd ) NEW_LINE DEDENT for i in xrange ( N ) : NEW_LINE INDENT y [ i ] = min ( y [ i ] , min ( x [ max ( 0 , i - mm ) : min ( i + mm + 1 , N ) ] ) + abs ( i - a ) ) NEW_LINE DEDENT for i in xrange ( N ) : NEW_LINE INDENT for j in xrange ( i + 1 , min ( N , i + mm + 1 ) ) : NEW_LINE INDENT y [ j ] = min ( y [ j ] , y [ i ] + ii ) NEW_LINE DEDENT DEDENT for i in xrange ( N ) : NEW_LINE INDENT for j in xrange ( i + 1 , min ( N , i + mm + 1 ) ) : NEW_LINE INDENT y [ N - 1 - j ] = min ( y [ N - 1 - j ] , y [ N - 1 - i ] + ii ) NEW_LINE DEDENT DEDENT x = y NEW_LINE DEDENT return str ( min ( x ) ) NEW_LINE DEDENT tno = int ( stdin . readline ( ) ) NEW_LINE for i in xrange ( 0 , tno ) : NEW_LINE INDENT print " Case ▁ # % d : ▁ % s " % ( i + 1 , main ( ) ) NEW_LINE DEDENT


import os NEW_LINE import sys NEW_LINE def debug ( * x , ** a ) : NEW_LINE INDENT print " DEBUG : ▁ % s " % " ▁ " . join ( [ repr ( v ) for v in x ] + [ " % s = % s " % ( k , repr ( v ) ) for ( k , v ) in a . iteritems ( ) ] ) NEW_LINE sys . stdout . flush ( ) NEW_LINE DEDENT def read_ints ( ) : NEW_LINE INDENT return [ int ( x ) for x in raw_input ( ) . strip ( ) . split ( ) ] NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT [ N_CASES ] = read_ints ( ) NEW_LINE for case in xrange ( 1 , N_CASES + 1 ) : NEW_LINE INDENT [ D , I , M , N ] = read_ints ( ) NEW_LINE A = read_ints ( ) NEW_LINE minA = min ( A ) NEW_LINE A = [ x - minA for x in A ] NEW_LINE Z = max ( A ) + 1 NEW_LINE def insertions ( a , b ) : NEW_LINE INDENT if abs ( a - b ) <= M : NEW_LINE INDENT return 0 NEW_LINE DEDENT if M == 0 : NEW_LINE INDENT return 1000000 NEW_LINE DEDENT return ( abs ( a - b ) - 1 ) // M * I NEW_LINE DEDENT c = [ 0 ] * Z NEW_LINE for a in A : NEW_LINE INDENT cc = [ 0 ] * Z NEW_LINE for j in xrange ( Z ) : NEW_LINE INDENT cost = c [ j ] + D NEW_LINE for k in xrange ( Z ) : NEW_LINE INDENT if j == a : NEW_LINE INDENT cost = min ( cost , c [ k ] + insertions ( k , a ) ) NEW_LINE DEDENT else : NEW_LINE INDENT cost = min ( cost , c [ k ] + insertions ( k , a ) + insertions ( a , j ) + I ) NEW_LINE cost = min ( cost , c [ k ] + abs ( j - a ) + insertions ( k , j ) ) NEW_LINE DEDENT DEDENT cc [ j ] = cost NEW_LINE DEDENT c = cc NEW_LINE DEDENT print " Case ▁ # % d : ▁ % d " % ( case , min ( c ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


def read ( ) : NEW_LINE INDENT D , I , M , nVal = map ( int , raw_input ( ) . split ( ) ) NEW_LINE val = map ( int , raw_input ( ) . split ( ) ) NEW_LINE return D , I , M , val NEW_LINE DEDENT def rec ( lastV , idx , D , I , M , val , dp ) : NEW_LINE INDENT if idx == len ( val ) : return 0 NEW_LINE ret = dp [ lastV ] [ idx ] NEW_LINE if ret != - 1 : return ret NEW_LINE ret = 1 << 30 NEW_LINE if lastV != 256 and abs ( lastV - val [ idx ] ) > M and M != 0 : NEW_LINE INDENT absV = abs ( lastV - val [ idx ] ) NEW_LINE ret = min ( ret , rec ( val [ idx ] , idx + 1 , D , I , M , val , dp ) + max ( 0 , ( absV / M - ( absV % M == 0 ) ) * I ) ) NEW_LINE DEDENT ret = min ( ret , rec ( lastV , idx + 1 , D , I , M , val , dp ) + D ) NEW_LINE if lastV == 256 : NEW_LINE INDENT for i in range ( 0 , 256 ) : NEW_LINE INDENT ret = min ( ret , rec ( i , idx + 1 , D , I , M , val , dp ) + abs ( val [ idx ] - i ) ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 0 , 256 ) : NEW_LINE INDENT if abs ( lastV - i ) <= M : NEW_LINE INDENT ret = min ( ret , rec ( i , idx + 1 , D , I , M , val , dp ) + abs ( val [ idx ] - i ) ) NEW_LINE DEDENT DEDENT DEDENT dp [ lastV ] [ idx ] = ret NEW_LINE return ret NEW_LINE DEDENT def work ( cases , ( D , I , M , val ) ) : NEW_LINE INDENT dp = [ [ - 1 for j in range ( len ( val ) ) ] for i in range ( 257 ) ] NEW_LINE print " Case ▁ # % d : ▁ % d " % ( cases , rec ( 256 , 0 , D , I , M , val , dp ) ) NEW_LINE DEDENT for i in range ( int ( raw_input ( ) ) ) : NEW_LINE INDENT work ( i + 1 , read ( ) ) NEW_LINE DEDENT


import java . util . * ; import static java . lang . Math . * ; public class B { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int T = sc . nextInt ( ) ; for ( int t = 1 ; t <= T ; t ++ ) { int D = sc . nextInt ( ) ; int I = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; int N = sc . nextInt ( ) ; int [ ] A = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = sc . nextInt ( ) ; int [ ] DP = new int [ 256 ] ; Arrays . fill ( DP , 0 ) ; int [ ] CUR = new int [ 256 ] ; for ( int at = N - 1 ; at >= 0 ; at -- ) { for ( int i = 0 ; i < 1000 ; i ++ ) { int prev ; if ( i % 2 == 0 ) prev = A [ at ] + i / 2 ; else prev = A [ at ] - ( i + 1 ) / 2 ; if ( prev < 0 || prev >= 256 ) continue ; int cost = DP [ prev ] + D ; for ( int j = 0 ; j < 256 ; j ++ ) { if ( abs ( j - prev ) <= M || prev == - 1 ) cost = min ( cost , DP [ j ] + abs ( j - A [ at ] ) ) ; if ( abs ( j - A [ at ] ) < abs ( A [ at ] - prev ) && ( prev == - 1 || abs ( j - prev ) <= M ) ) cost = min ( cost , CUR [ j ] + I ) ; } CUR [ prev ] = cost ; } for ( int j = 0 ; j < 256 ; j ++ ) DP [ j ] = CUR [ j ] ; Arrays . fill ( CUR , 0 ) ; } int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < DP . length ; i ++ ) ans = min ( ans , DP [ i ] ) ; System . out . format ( " Case ▁ # % d : ▁ % d \n " , t , ans ) ; } } }


import java . util . * ; public class C { public static void main ( String args [ ] ) { ( new C ( ) ) . exec ( ) ; } void exec ( ) { Scanner cin = new Scanner ( System . in ) ; int t = cin . nextInt ( ) ; for ( int z = 0 ; z < t ; ++ z ) { int k = cin . nextInt ( ) ; int value [ ] = new int [ k ] ; boolean used [ ] = new boolean [ k ] ; int count = 0 ; int cur = 0 ; while ( count < k ) { for ( int i = 0 ; i < k ; ++ i ) { if ( used [ i ] ) { continue ; } if ( cur == count ) { value [ i ] = ++ count ; cur = 0 ; used [ i ] = true ; } else { ++ cur ; } } } System . out . print ( " Case ▁ # " + ( z + 1 ) + " : " ) ; int n = cin . nextInt ( ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( " ▁ " + value [ cin . nextInt ( ) - 1 ] ) ; } System . out . println ( ) ; } } }

import sys NEW_LINE def readdata ( ) : NEW_LINE INDENT def rl ( ) : NEW_LINE INDENT return sys . stdin . readline ( ) . strip ( ) . split ( ' ▁ ' ) NEW_LINE DEDENT n = int ( rl ( ) [ 0 ] ) NEW_LINE cases = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT K = int ( rl ( ) [ 0 ] ) NEW_LINE il = [ int ( x ) for x in rl ( ) ] NEW_LINE case = ( K , il [ 1 : il [ 0 ] + 1 ] ) NEW_LINE cases . append ( case ) NEW_LINE DEDENT return cases NEW_LINE DEDENT case_number = 1 NEW_LINE for case in readdata ( ) : NEW_LINE INDENT sc = case [ 0 ] NEW_LINE si = range ( sc ) NEW_LINE sv = [ 0 ] * sc NEW_LINE cv = 1 NEW_LINE ci = 0 NEW_LINE for z in xrange ( sc ) : NEW_LINE INDENT ci += z NEW_LINE ci = ci % len ( si ) NEW_LINE sv [ si [ ci ] ] = z + 1 NEW_LINE si . remove ( si [ ci ] ) NEW_LINE DEDENT print ' Case ▁ # ' + str ( case_number ) + ' : ' , ' ▁ ' . join ( [ str ( sv [ x - 1 ] ) for x in case [ 1 ] ] ) NEW_LINE case_number += 1 NEW_LINE DEDENT


inp_file = file ( " C - small . in " ) NEW_LINE out_file = file ( " C - small . out " , " w " ) NEW_LINE def solve ( line ) : NEW_LINE INDENT line = [ int ( c1 ) for c1 in line . split ( " ▁ " ) ] NEW_LINE card_num , indices = line [ : 2 ] NEW_LINE line = line [ 2 : ] NEW_LINE positions = range ( card_num ) NEW_LINE result = [ 0 for c1 in range ( card_num ) ] NEW_LINE c1 = 1 NEW_LINE c2 = 0 NEW_LINE while c1 < card_num : NEW_LINE INDENT result [ positions [ c2 ] ] = c1 NEW_LINE positions . pop ( c2 ) NEW_LINE c2 = ( c2 + c1 ) % len ( positions ) NEW_LINE c1 += 1 NEW_LINE DEDENT result [ result . index ( 0 ) ] = card_num NEW_LINE return " ▁ " . join ( [ str ( result [ c1 - 1 ] ) for c1 in line ] ) NEW_LINE DEDENT num = int ( inp_file . readline ( ) ) NEW_LINE for case in range ( num ) : NEW_LINE INDENT line = inp_file . readline ( ) [ : - 1 ] + " ▁ " + inp_file . readline ( ) [ : - 1 ] NEW_LINE out_file . write ( " Case ▁ # % s : ▁ " % ( case + 1 ) + solve ( line ) + " \n " ) NEW_LINE DEDENT inp_file . close ( ) NEW_LINE out_file . close ( ) NEW_LINE


import sys NEW_LINE filename = sys . argv [ 1 ] NEW_LINE inputfile = file ( filename , ' r ' ) NEW_LINE numcases = int ( inputfile . readline ( ) . strip ( ) ) NEW_LINE for case in range ( 1 , numcases + 1 ) : NEW_LINE INDENT K = int ( inputfile . readline ( ) . strip ( ) ) NEW_LINE data = map ( int , inputfile . readline ( ) . strip ( ) . split ( " ▁ " ) ) NEW_LINE n = data [ 0 ] NEW_LINE dis = data [ 1 : ] NEW_LINE cards = range ( K ) NEW_LINE remainingcards = range ( K ) NEW_LINE remainingcardpos = 0 NEW_LINE for cardvalue in range ( K ) : NEW_LINE INDENT remainingcardpos = ( cardvalue + remainingcardpos ) % len ( remainingcards ) NEW_LINE card = remainingcards [ remainingcardpos ] NEW_LINE cards [ card ] = cardvalue + 1 NEW_LINE del remainingcards [ remainingcardpos ] NEW_LINE DEDENT sys . stdout . write ( " Case ▁ # % d : " % case ) NEW_LINE for di in dis : NEW_LINE INDENT sys . stdout . write ( " ▁ % d " % cards [ di - 1 ] ) NEW_LINE DEDENT sys . stdout . write ( " \n " ) NEW_LINE DEDENT


import sys , itertools NEW_LINE data = filter ( None , map ( lambda x : x . strip ( ) , open ( sys . argv [ 1 ] ) . readlines ( ) ) ) NEW_LINE def pop_int ( data ) : NEW_LINE INDENT return int ( data . pop ( 0 ) ) NEW_LINE DEDENT def pop_ints ( data ) : NEW_LINE INDENT return map ( int , data . pop ( 0 ) . split ( ) ) NEW_LINE DEDENT def pop_rows ( data , num_rows ) : NEW_LINE INDENT result = data [ : num_rows ] NEW_LINE for i in range ( num_rows ) : NEW_LINE INDENT data . pop ( 0 ) NEW_LINE DEDENT return result NEW_LINE DEDENT def pop_case ( data ) : NEW_LINE INDENT num_cards = pop_int ( data ) NEW_LINE indices = pop_ints ( data ) [ 1 : ] NEW_LINE return num_cards , indices NEW_LINE DEDENT def make_deck ( num_cards ) : NEW_LINE INDENT deck = [ None for i in range ( num_cards ) ] NEW_LINE indices_open = range ( num_cards ) NEW_LINE last_ii = 0 NEW_LINE for i in range ( 1 , num_cards + 1 ) : NEW_LINE INDENT last_ii = index_index = ( i + last_ii - 1 ) % len ( indices_open ) NEW_LINE position = indices_open [ index_index ] NEW_LINE assert ( deck [ position ] is None ) NEW_LINE deck [ position ] = i NEW_LINE indices_open . remove ( position ) NEW_LINE DEDENT return deck NEW_LINE DEDENT def solve ( num_cards , indices ) : NEW_LINE INDENT solution = make_deck ( num_cards ) NEW_LINE answer = ' ▁ ' . join ( str ( solution [ i - 1 ] ) for i in indices ) NEW_LINE return answer NEW_LINE DEDENT num_cases = pop_int ( data ) NEW_LINE for case_num in range ( 1 , num_cases + 1 ) : NEW_LINE INDENT num_cards , indices = pop_case ( data ) NEW_LINE answer = solve ( num_cards , indices ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( case_num , answer ) NEW_LINE DEDENT


case = " small - attempt0" NEW_LINE input_file = " C - % s . in " % case NEW_LINE output_file = " C - % s . out " % case NEW_LINE fin = open ( input_file ) NEW_LINE fout = open ( output_file , " w " ) NEW_LINE ncase = int ( fin . readline ( ) . strip ( ) ) NEW_LINE for z in xrange ( 1 , ncase + 1 ) : NEW_LINE INDENT print >> fout , " Case ▁ # % d : " % z , NEW_LINE k = int ( fin . readline ( ) . strip ( ) ) NEW_LINE d = [ int ( x ) - 1 for x in fin . readline ( ) . strip ( ) . split ( ) [ 1 : ] ] NEW_LINE for dd in d : NEW_LINE INDENT n , p , r , i = k , dd , 0 , 1 NEW_LINE while p != r : NEW_LINE INDENT n -= 1 NEW_LINE if p > r : NEW_LINE INDENT p -= 1 NEW_LINE DEDENT r = ( r + i ) % n NEW_LINE i += 1 NEW_LINE DEDENT print >> fout , i , NEW_LINE DEDENT print >> fout NEW_LINE DEDENT fin . close ( ) NEW_LINE fout . close ( ) NEW_LINE


import java . io . * ; import java . util . * ; public class TaskC implements Runnable { private String IFILE = " C - large . in " ; private Scanner in ; private PrintWriter out ; public void Run ( ) throws IOException { in = new Scanner ( new File ( IFILE ) ) ; out = new PrintWriter ( " output . txt " ) ; int ntest = in . nextInt ( ) ; for ( int test = 1 ; test <= ntest ; test ++ ) { out . print ( " Case ▁ # " + test + " : " ) ; int n = in . nextInt ( ) ; int m = in . nextInt ( ) ; int [ ] mas = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { int v = in . nextInt ( ) - 1 ; int count = n ; for ( int j = 0 ; j < n ; j ++ ) { int pos = j % count ; if ( pos == v ) { out . print ( " ▁ " + ( j + 1 ) ) ; break ; } if ( pos > v ) { v += count - pos - 1 ; } else { v = v - pos - 1 ; } count -- ; } } out . println ( ) ; } in . close ( ) ; out . close ( ) ; } public void run ( ) { try { Run ( ) ; } catch ( IOException e ) { } } public static void main ( String [ ] args ) throws IOException { new TaskC ( ) . Run ( ) ; } }


package common ; import java . io . BufferedReader ; import java . io . File ; import java . io . FileReader ; import java . io . FileWriter ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . List ; public class Helper { public static String [ ] getAllFileContentAsArray ( String filename ) throws Exception { BufferedReader in = new BufferedReader ( new FileReader ( filename ) ) ; List < String > all = new ArrayList < String > ( ) ; String s = in . readLine ( ) ; while ( s != null ) { all . add ( s ) ; s = in . readLine ( ) ; } in . close ( ) ; return all . toArray ( new String [ 0 ] ) ; } public static void writeToFile ( String [ ] cont , String filename ) throws Exception { File file = new File ( filename ) ; file . delete ( ) ; FileWriter w = null ; try { w = new FileWriter ( file , true ) ; for ( String s : cont ) { w . write ( s + " \n " ) ; } } finally { if ( w != null ) { w . close ( ) ; } } } public static int [ ] getIntegersFromString ( String s ) throws Exception { List < Integer > a = new ArrayList < Integer > ( ) ; for ( String se : s . split ( " ▁ " ) ) { try { Integer x = Integer . parseInt ( se ) ; a . add ( x ) ; } catch ( Exception e ) { } } int [ ] ret = new int [ a . size ( ) ] ; for ( int i = 0 ; i < a . size ( ) ; ++ i ) { ret [ i ] = a . get ( i ) . intValue ( ) ; } return ret ; } private Helper ( ) { } }