├── README.md ├── RS2D.java ├── aba12e.java ├── acode.java ├── acpc10a.java ├── acpc10d.java ├── acpc11b.java ├── acpc11c.java ├── addrev.java ├── adfruits.java ├── ae00.java ├── ae1b.java ├── ae2a.java ├── aggrcow.java ├── ags.java ├── alien.java ├── aliens.java ├── amr10g.java ├── amr11h.java ├── amr12d.java ├── anarc05b.java ├── anarc08h.java ├── anarc09b.java ├── anarc09g.java ├── army.java ├── arraysub.java ├── assembly.java ├── assign4.java ├── backpack.java ├── ballot.java ├── ballsag.java ├── barn.java ├── bcake.java ├── behappy.java ├── bishops.java ├── blinnet.java ├── boballs2.java ├── bomarble.java ├── bomedian.java ├── books1.java ├── boperish.java ├── bovaluel.java ├── bst.java ├── btcode_c.java ├── btcode_h.java ├── buglife.java ├── bureau.java ├── bytese1.java ├── bytese2.java ├── bytesm1.java ├── bytesm2.java ├── c1ljutnj.java ├── candy3.java ├── candydist.java ├── caprica.java ├── cardgame.java ├── cattleb.java ├── ccost.java ├── cerc07b.java ├── cerc07k.java ├── cf33c.java ├── clone.java ├── cloppair.java ├── cmpls.java ├── coins.java ├── cointoss.java ├── complete.java ├── contcity.java ├── cornet.java ├── countisl.java ├── courier.java ├── cowcar.java ├── cran02.java ├── cran04.java ├── crscntry.java ├── cstreet.java ├── ct25c.java ├── ctain.java ├── ctgame.java ├── darts.java ├── dcepc206.java ├── dcepca03.java ├── dfloor.java ├── dice.java ├── dingrp.java ├── disjpath.java ├── disquery.java ├── distance.java ├── divsum.java ├── donothin.java ├── dragoncu.java ├── dsubseq.java ├── edist.java ├── egypar.java ├── einst.java ├── eqbox.java ├── esjail.java ├── etf.java ├── exclsec.java ├── exfor.java ├── expedi.java ├── fact0.java ├── faketsp.java ├── fasion.java ├── fastflow.java ├── fctrl.java ├── fctrl2.java ├── fence1.java ├── fence3.java ├── fibosum.java ├── fisher.java ├── foss.java ├── fpolice.java ├── ftheelf.java ├── gaswars.java ├── gcj101ab.java ├── gcj101bb.java ├── gcj101c.java ├── gcpc11a.java ├── george.java ├── gergovia.java ├── ghosts.java ├── girlsnbs.java ├── glasnici.java ├── gny07a.java ├── gny07h.java ├── gnyr09f.java ├── gsm.java ├── gss1.java ├── gss5.java ├── guessthe.java ├── hackers.java ├── hamster1.java ├── hamster2.java ├── hangover.java ├── hchains.java ├── headshot.java ├── hellokit.java ├── herbicid.java ├── herding.java ├── hexboard.java ├── highways.java ├── hknap.java ├── horrible.java ├── hotels.java ├── hs10gmbl.java ├── hs10strs.java ├── hs10sums.java ├── ieeebgam.java ├── image.java ├── importa.java ├── incards.java ├── incdseq.java ├── incseq.java ├── intest.java ├── invcnt.java ├── iopc1205.java ├── itrix_b.java ├── itrix_d.java ├── jmpmnkey.java ├── jochef.java ├── johnny.java ├── julka.java ├── kgss.java ├── knjige.java ├── kompici.java ├── lastdig.java ├── lazycows.java ├── lineup.java ├── lisa.java ├── lite.java ├── m3tile.java ├── main75.java ├── main8_c.java ├── majstor.java ├── maketree.java ├── marbles.java ├── martian.java ├── matching.java ├── matgame.java ├── matsum.java ├── maxsumsq.java ├── mblast.java ├── mcircgame.java ├── mcl.java ├── mcoins.java ├── micemaze.java ├── mirrored.java ├── mlaserp.java ├── mminpaid.java ├── mmmgame.java ├── movmrbl.java ├── mpilot.java ├── mrect1.java ├── mtele.java ├── mtotalf.java ├── mtreecol.java ├── mtwalk.java ├── multq3.java ├── myq6.java ├── myq7.java ├── nagay1.java ├── naptime.java ├── nfactor.java ├── nicebtre.java ├── nkmars.java ├── novice62.java ├── novice63.java ├── nqueen.java ├── ntkm.java ├── ny10e.java ├── ololo.java ├── onp.java ├── opcpizza.java ├── osprob1.java ├── pairsort.java ├── parsums.java ├── party.java ├── patheads.java ├── pbcgame.java ├── pebble.java ├── permut2.java ├── pfdep.java ├── phonelst.java ├── picad.java ├── pie.java ├── pigbank.java ├── pir.java ├── plonk.java ├── polynom.java ├── pony1.java ├── pony3.java ├── portalun.java ├── pothole.java ├── powtow.java ├── ppath.java ├── prhyme.java ├── prime1.java ├── probor.java ├── prtynght.java ├── pruball.java ├── pt07x.java ├── pt07y.java ├── pt07z.java ├── quadarea.java ├── quality.java ├── rating.java ├── rdnwk.java ├── regpolyg.java ├── rent.java ├── resist.java ├── resn04.java ├── roads.java ├── robotgri.java ├── rock.java ├── rooks.java ├── rplb.java ├── rrsched.java ├── samer08a.java ├── samer08c.java ├── samer08f.java ├── samer08j.java ├── sbets.java ├── scities.java ├── scubadiv.java ├── segsqrss.java ├── sensornt.java ├── sflip.java ├── shop.java ├── signgame.java ├── skiing.java ├── soldier.java ├── sqrt2.java ├── stamps.java ├── stargate.java ├── stead.java ├── stone.java ├── stpar.java ├── sublex.java ├── sud.java ├── sudoku2.java ├── sumtrian.java ├── table5x5.java ├── tails.java ├── temperat.java ├── test.java ├── tetra.java ├── theatre.java ├── thrbl.java ├── threetw1.java ├── trafficn.java ├── transfers.java ├── transj.java ├── traverse.java ├── trex.java ├── tricentr.java ├── tricount.java ├── trip2.java ├── troops.java ├── trstage.java ├── trt.java ├── ttreat.java ├── twends.java ├── twodel.java ├── twodel2.java ├── vmili.java ├── wachovia.java ├── wingold.java ├── wonka1.java ├── workb.java ├── wrong.java ├── xoinc.java ├── ykh.java ├── youtube.java ├── zsum.java └── zuma.java /README.md: -------------------------------------------------------------------------------- 1 | SPOJ 2 | ==== 3 | 4 | Some selected solutions 5 | 6 | 7 | To see problem, put http://www.spoj.com/problems/FILENAME/ 8 | Make sure it's all caps, and don't include the .java extension 9 | 10 | Some files may actually have nothing of interest (i.e. it's just a template file). 11 | This just means I forgot to implement the problem... 12 | 13 | http://www.spoj.com/users/lg52931/ to see what problems are actually solved. 14 | Some may not be included (that probably means I implemented in C++, or I can't locate the file). 15 | -------------------------------------------------------------------------------- /acode.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class acode 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | 9 | public static void main(String[] args) throws IOException { 10 | in = new BufferedReader(new InputStreamReader(System.in)); 11 | out = new PrintWriter(System.out, true); 12 | String line = in.readLine(); 13 | while(line.charAt(0) != '0') { 14 | out.println(count(line.toCharArray())); 15 | line = in.readLine(); 16 | } 17 | } 18 | 19 | private static long count(char[] arr) { 20 | long[] dp = new long[arr.length+1]; 21 | dp[0] = 1; 22 | for(int i = 0; i < arr.length; i++) { 23 | if(arr[i] > '0' && arr[i] <= '9') dp[i+1] += dp[i]; 24 | if(i > 0) { 25 | int test = (arr[i-1]-'0')*10+(arr[i]-'0'); 26 | if(test >= 10 && test <= 26) dp[i+1] += dp[i-1]; 27 | } 28 | } 29 | return dp[arr.length]; 30 | } 31 | } -------------------------------------------------------------------------------- /acpc10a.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class acpc10a 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | while (true) { 13 | int a1 = in.nextInt (), a2 = in.nextInt (), a3 = in.nextInt (); 14 | if (a1 == 0 && a2 == 0 && a3 == 0) break; 15 | if (a3 - a2 == a2 - a1) { 16 | out.println ("AP " + (a3 + (a2 - a1))); 17 | } else { 18 | out.println ("GP " + (a3 * a2 / a1)); 19 | } 20 | } 21 | } 22 | } 23 | 24 | /** Faster input **/ 25 | class Reader { 26 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 27 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 28 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 29 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 30 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 31 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 32 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 33 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 34 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 35 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 36 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 37 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 38 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 39 | public void close()throws IOException{if(din==null) return;din.close();} 40 | } -------------------------------------------------------------------------------- /acpc10d.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class acpc10d 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int t = 0; 13 | while (true) {t++; 14 | int n = in.nextInt (); 15 | if (n == 0) break; 16 | long [][] arr = new long [n][3]; 17 | for (int i = 0; i < n; i++) 18 | for (int j = 0; j < 3; j++) { 19 | arr [i][j] = in.nextInt (); 20 | if (i == 0 && j == 0) arr [i][j] = Long.MAX_VALUE >> 2; 21 | if (!(i == 0 && j == 1)) { 22 | long min = Long.MAX_VALUE >> 2; 23 | long up = (i == 0) ? min : arr [i - 1][j]; 24 | long left = (j == 0) ? min : arr [i][j - 1]; 25 | long upleft = (i == 0 || j == 0) ? min : arr [i - 1][j - 1]; 26 | long upright = (i == 0 || j == 2) ? min: arr [i - 1][j + 1]; 27 | arr [i][j] += Math.min (Math.min (up, left), Math.min (upleft, upright)); 28 | } 29 | } 30 | out.println (t + ". " + arr [n - 1][1]); 31 | } 32 | System.exit (0); 33 | } 34 | } 35 | 36 | /** Faster input **/ 37 | class Reader { 38 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 39 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 40 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 41 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 42 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 43 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 44 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 45 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 46 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 47 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 48 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 49 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 50 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 51 | public void close()throws IOException{if(din==null) return;din.close();} 52 | } -------------------------------------------------------------------------------- /adfruits.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class adfruits { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main (String [] args) throws IOException { 9 | in = new BufferedReader (new InputStreamReader (System.in)); 10 | out = new PrintWriter (System.out, true); 11 | String line = in.readLine(); 12 | while (line != null) { 13 | String [] sp = line.split (" "); 14 | String a = combinef (sp [0], sp [1]); int x1 = a.length(); 15 | String b = combinef (sp [1], sp [0]); int x2 = b.length(); 16 | String c = combiner (sp [0], sp [1]); int x3 = c.length(); 17 | String d = combiner (sp [1], sp [0]); int x4 = d.length(); 18 | if (x1 <= x2 && x1 <= x3 && x1 <= x4) out.println (a); 19 | else if (x2 <= x1 && x2 <= x3 && x2 <= x4) out.println (b); 20 | else if (x3 <= x1 && x3 <= x2 && x3 <= x4) out.println (c); 21 | else if (x4 <= x1 && x4 <= x2 && x4 <= x3) out.println (d); 22 | line = in.readLine (); 23 | } 24 | } 25 | 26 | private static String combinef (String a, String b) { 27 | char [] arr = a.toCharArray(), brr = b.toCharArray(); 28 | int j = 0; 29 | for (int i = 0; i < arr.length && j < brr.length; i++) 30 | if (arr [i] == brr [j]) j++; 31 | return new String (arr) + new String (brr, j, brr.length - j); 32 | } 33 | 34 | private static String combiner (String a, String b) { 35 | char [] arr = a.toCharArray(), brr = b.toCharArray(); 36 | int j = brr.length - 1; 37 | for (int i = arr.length - 1; i >= 0 && j >= 0; i--) 38 | if (arr [i] == brr [j]) j--; 39 | return new String (brr, 0, j + 1) + new String (arr); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /ae00.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class ae00 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int N = parseInt(f.readLine()); 12 | int i; 13 | 14 | long total = 0; 15 | for(i = 1; N >= i*i; i++) total += (N/i - (i-1)); 16 | 17 | System.out.println(total); 18 | System.out.flush(); 19 | System.exit(0); 20 | } 21 | } -------------------------------------------------------------------------------- /ae1b.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class ae1b 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | StringTokenizer st = new StringTokenizer(f.readLine()); 12 | int n = parseInt(st.nextToken()); 13 | int k = parseInt(st.nextToken()); 14 | int s = parseInt(st.nextToken()); 15 | 16 | int t = k*s; 17 | 18 | PriorityQueue pq = new PriorityQueue(); 19 | st = new StringTokenizer(f.readLine()); 20 | 21 | while(st.hasMoreTokens()) 22 | pq.add(-parseInt(st.nextToken())); 23 | 24 | while(t > 0) 25 | t += pq.poll(); 26 | 27 | System.out.println( (n-pq.size()) ); 28 | System.out.flush(); 29 | 30 | System.exit(0); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /ae2a.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class ae2a 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = parseInt(f.readLine()); 12 | StringTokenizer st; 13 | int n, k; 14 | while(T-- > 0) 15 | { 16 | st = new StringTokenizer(f.readLine()); 17 | n = parseInt(f.readLine()); 18 | k = parseInt(f.readLine()); 19 | 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /ags.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class ags { 5 | private static Reader in; 6 | private static PrintWriter out; 7 | 8 | public static void main(String[] args) throws IOException { 9 | in = new Reader(); 10 | out = new PrintWriter(System.out, true); 11 | 12 | int T = in.nextInt(); 13 | } 14 | 15 | 16 | 17 | private static int gcd (int a, int b) { 18 | if (b == 0) return a; 19 | return gcd (b, a % b); 20 | } 21 | 22 | 23 | static class Reader { 24 | final private int BUFFER_SIZE = 1 << 16; 25 | private DataInputStream din; 26 | private byte[] buffer; 27 | private int bufferPointer, bytesRead; 28 | 29 | public Reader() { 30 | din = new DataInputStream(System.in); 31 | buffer = new byte[BUFFER_SIZE]; 32 | bufferPointer = bytesRead = 0; 33 | } 34 | 35 | public Reader(String file_name) throws IOException { 36 | din = new DataInputStream(new FileInputStream(file_name)); 37 | buffer = new byte[BUFFER_SIZE]; 38 | bufferPointer = bytesRead = 0; 39 | } 40 | 41 | public String readLine() throws IOException { 42 | byte[] buf = new byte[1024]; 43 | int cnt = 0, c; 44 | while ((c = read()) != -1) { 45 | if (c == '\n') 46 | break; 47 | buf[cnt++] = (byte) c; 48 | } 49 | return new String(buf, 0, cnt); 50 | } 51 | 52 | public int nextInt() throws IOException { 53 | int ret = 0; 54 | byte c = read(); 55 | while (c <= ' ') 56 | c = read(); 57 | boolean neg = (c == '-'); 58 | if (neg) 59 | c = read(); 60 | do { 61 | ret = ret * 10 + c - '0'; 62 | } while ((c = read()) >= '0' && c <= '9'); 63 | if (neg) 64 | return -ret; 65 | return ret; 66 | } 67 | 68 | public long nextLong() throws IOException { 69 | long ret = 0; 70 | byte c = read(); 71 | while (c <= ' ') 72 | c = read(); 73 | boolean neg = (c == '-'); 74 | if (neg) 75 | c = read(); 76 | do { 77 | ret = ret * 10 + c - '0'; 78 | } while ((c = read()) >= '0' && c <= '9'); 79 | if (neg) 80 | return -ret; 81 | return ret; 82 | } 83 | 84 | public double nextDouble() throws IOException { 85 | double ret = 0, div = 1; 86 | byte c = read(); 87 | while (c <= ' ') 88 | c = read(); 89 | boolean neg = (c == '-'); 90 | if (neg) 91 | c = read(); 92 | do { 93 | ret = ret * 10 + c - '0'; 94 | } while ((c = read()) >= '0' && c <= '9'); 95 | if (c == '.') 96 | while ((c = read()) >= '0' && c <= '9') 97 | ret += (c - '0') / (div *= 10); 98 | if (neg) 99 | return -ret; 100 | return ret; 101 | } 102 | 103 | private void fillBuffer() throws IOException { 104 | bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 105 | if (bytesRead == -1) 106 | buffer[0] = -1; 107 | } 108 | 109 | private byte read() throws IOException { 110 | if (bufferPointer == bytesRead) 111 | fillBuffer(); 112 | return buffer[bufferPointer++]; 113 | } 114 | 115 | public void close() throws IOException { 116 | if (din == null) 117 | return; 118 | din.close(); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /amr12d.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class amr12d { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main(String[] args) throws IOException { 9 | in = new BufferedReader(new InputStreamReader (System.in)); 10 | out = new PrintWriter(System.out, true); 11 | 12 | int T = Integer.parseInt (in.readLine()); 13 | while (T-- > 0) { 14 | String s = in.readLine(); 15 | boolean ok = true; 16 | outer : for (int i = 0; i <= s.length(); i++) 17 | for (int j = i + 1; j <= s.length(); j++) { 18 | if (!s.contains (rev (s.substring (i, j)))) { 19 | ok = false; 20 | break outer; 21 | } 22 | } 23 | out.println (ok ? "YES" : "NO"); 24 | } 25 | out.close(); 26 | System.exit(0); 27 | } 28 | 29 | private static String rev (String s) { 30 | StringBuffer b = new StringBuffer (); 31 | for (int i = s.length() - 1; i >= 0; i--) 32 | b.append (s.charAt (i)); 33 | return b.toString(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /anarc09b.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Long.parseLong; 4 | 5 | public class anarc09b 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | StringTokenizer st; 12 | long w, h; 13 | for(;;) 14 | { 15 | st = new StringTokenizer(f.readLine()); 16 | w = parseLong(st.nextToken()); 17 | h = parseLong(st.nextToken()); 18 | if(w == 0 && h == 0) break; 19 | System.out.println(lcm(w,h)); 20 | System.out.flush(); 21 | } 22 | System.exit(0); 23 | } 24 | 25 | private static long lcm(long x, long y) 26 | { 27 | long gcd = gcd(x,y); 28 | return (x / gcd) * (y / gcd); 29 | } 30 | 31 | private static long gcd(long x, long y) 32 | { 33 | if(y > x){x^=y;y^=x;x^=y;} 34 | while(y != 0) 35 | { 36 | x %= y; 37 | if(y > x){x^=y;y^=x;x^=y;} 38 | } 39 | return x; 40 | } 41 | } -------------------------------------------------------------------------------- /army.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class army 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int t = parseInt(f.readLine()); 12 | int max, a; boolean godzilla; 13 | StringTokenizer st; 14 | while(t-->0) 15 | { 16 | f.readLine(); f.readLine(); 17 | godzilla = true; max = 0; 18 | st = new StringTokenizer(f.readLine()); 19 | while(st.hasMoreTokens()) 20 | { 21 | a = parseInt(st.nextToken()); 22 | if(a > max) max = a; 23 | } 24 | st = new StringTokenizer(f.readLine()); 25 | while(st.hasMoreTokens()) 26 | { 27 | a = parseInt(st.nextToken()); 28 | if(a > max){ max = a; godzilla = false;} 29 | } 30 | 31 | if(godzilla) System.out.println("Godzilla"); 32 | else System.out.println("MechaGodzilla"); 33 | System.out.flush(); 34 | } 35 | System.exit(0); 36 | } 37 | } -------------------------------------------------------------------------------- /assembly.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class assembly { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main (String [] args) throws IOException { 9 | in = new BufferedReader (new InputStreamReader (System.in)); 10 | out = new PrintWriter (System.out, true); 11 | 12 | boolean first = true; 13 | while (true) { 14 | int N = Integer.parseInt (in.readLine()); 15 | if (N == 0) break; 16 | 17 | if (!first) out.println (); 18 | first = false; 19 | 20 | int [] to = new int [26]; 21 | char [] arr = new char [N]; 22 | StringTokenizer st = new StringTokenizer (in.readLine()); 23 | for (int i = 0; i < N; i++) { 24 | arr [i] = st.nextToken().charAt (0); 25 | to [arr [i] - 'a'] = i; 26 | } 27 | 28 | int [][] costMat = new int [N][N]; 29 | int [][] toMat = new int [N][N]; 30 | for (int i = 0; i < N; i++) { 31 | st = new StringTokenizer (in.readLine()); 32 | for (int j = 0; j < N; j++) { 33 | String [] s = st.nextToken().split ("-"); 34 | costMat [i][j] = Integer.parseInt (s [0]); 35 | toMat [i][j] = to [s [1].charAt (0) - 'a']; 36 | } 37 | } 38 | 39 | int Q = Integer.parseInt (in.readLine()); 40 | while (Q-- > 0) { 41 | String s = in.readLine(); 42 | int M = s.length(); 43 | int [] array = new int [M]; 44 | for (int i = 0; i < M; i++) 45 | array [i] = to [s.charAt (i) - 'a']; 46 | 47 | int [][][] dp = new int [N][M][M]; 48 | for (int i = 0; i < N; i++) 49 | for (int k = 0; k < M; k++) 50 | Arrays.fill (dp [i][k], 1 << 29); 51 | 52 | for (int i = 0; i < M; i++) { 53 | dp [array [i]][i][i] = 0; 54 | } 55 | 56 | for (int j = 1; j < M; j++) 57 | for (int i = 0; i < M - j; i++) { 58 | for (int a = 0; a < N; a++) 59 | for (int b = 0; b < N; b++) { 60 | for (int k = i; k < i + j; k++) { 61 | dp [toMat [a][b]][i][i + j] = 62 | Math.min (dp [toMat [a][b]][i][i + j], 63 | dp [a][i][k] + dp [b][k + 1][i + j] + costMat [a][b]); 64 | } 65 | } 66 | } 67 | 68 | long minCost = Long.MAX_VALUE; 69 | int letter = -1; 70 | for (int i = 0; i < N; i++) 71 | if (dp [i][0][M - 1] < minCost) { 72 | minCost = dp [i][0][M - 1]; 73 | letter = i; 74 | } 75 | 76 | out.printf ("%d-%s\n", minCost, arr [letter]); 77 | } 78 | } 79 | out.close(); 80 | System.exit(0); 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /backpack.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class backpack 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int t = parseInt(f.readLine()); 12 | int[] dp, temp, val, vol, ind; 13 | int[][] att; boolean[] isatt; 14 | int cap, n, i, a, j, k, m; 15 | StringTokenizer st; 16 | while(t-->0) 17 | { 18 | st = new StringTokenizer(f.readLine()); 19 | cap = parseInt(st.nextToken()); 20 | n = parseInt(st.nextToken()); 21 | dp = new int[cap+1]; 22 | vol = new int[n]; val = new int[n]; 23 | att = new int[n][3]; ind = new int[n]; 24 | isatt = new boolean[n]; 25 | for(i = 0; i < n; i++) 26 | { 27 | st = new StringTokenizer(f.readLine()); 28 | vol[i] = parseInt(st.nextToken()); 29 | val[i] = parseInt(st.nextToken())*vol[i]; 30 | a = parseInt(st.nextToken()); 31 | if(a == 0) continue; a--; 32 | att[a][ind[a]++] = i; 33 | isatt[i] = true; 34 | } 35 | 36 | for(i = 0; i < n; i++) 37 | { 38 | if(isatt[i]) continue; 39 | temp = new int[cap+1]; 40 | for(k = cap; k >= vol[i]; k--) 41 | temp[k] = dp[k-vol[i]]+val[i]; 42 | 43 | for(j = 0; j < ind[i]; j++) 44 | { 45 | m = att[i][j]; 46 | for(k = cap; k >= vol[i]+vol[m]; k--) 47 | { 48 | if(temp[k] < dp[k-vol[i]-vol[m]]+val[i]+val[m]) 49 | temp[k] = dp[k-vol[i]-vol[m]]+val[i]+val[m]; 50 | if(temp[k] < temp[k-vol[m]]+val[m]) 51 | temp[k] = temp[k-vol[m]]+val[m]; 52 | } 53 | } 54 | 55 | for(k = 0; k <= cap; k++) 56 | if(dp[k] < temp[k]) 57 | dp[k] = temp[k]; 58 | } 59 | 60 | System.out.println(dp[cap]); 61 | System.out.flush(); 62 | } 63 | System.exit(0); 64 | } 65 | } -------------------------------------------------------------------------------- /bcake.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class bcake { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main (String [] args) throws IOException { 9 | in = new BufferedReader (new InputStreamReader (System.in)); 10 | out = new PrintWriter (System.out, true); 11 | 12 | int T = Integer.parseInt (in.readLine()); 13 | StringTokenizer st; 14 | while (T-- > 0) { 15 | st = new StringTokenizer (in.readLine ()); 16 | int H = Integer.parseInt (st.nextToken ()); 17 | int W = Integer.parseInt (st.nextToken ()); 18 | int K = Integer.parseInt (st.nextToken ()); 19 | 20 | if (K == 0) { 21 | out.println (0); 22 | continue; 23 | } 24 | 25 | int [][] grid = new int [H + 1][W + 1]; 26 | for (int i = 1; i <= H; i++) { 27 | char [] line = in.readLine().toCharArray(); 28 | for (int j = 1; j <= W; j++) { 29 | grid [i][j] = grid [i - 1][j] + grid [i][j - 1] 30 | + (line [j - 1] == 'C' ? 1 : 0) - grid [i - 1][j - 1]; 31 | } 32 | } 33 | 34 | int min = Integer.MAX_VALUE; 35 | for (int i = 1; i <= H; i++) 36 | for (int j = i + 1; j <= H; j++) 37 | for (int k = 1; k <= W; k++) 38 | for (int m = k + 1; m <= W; m++) { 39 | int chips = grid [j][m] - grid [i][m] - grid [j][k] + grid [i][k]; 40 | if (chips != K) continue; 41 | if ((m - k) * (j - i) < min) 42 | min = (m - k) * (j - i); 43 | } 44 | 45 | out.println (min == Integer.MAX_VALUE ? -1 : min); 46 | } 47 | 48 | out.close(); 49 | System.exit(0); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /bishops.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.math.BigInteger; 4 | 5 | public class bishops 6 | { 7 | private static BufferedReader in; 8 | private static PrintWriter out; 9 | 10 | public static void main (String [] args) throws IOException { 11 | in = new BufferedReader (new InputStreamReader (System.in)); 12 | out = new PrintWriter (System.out, true); 13 | BigInteger two = new BigInteger ("2"); 14 | for (;;) { 15 | String line = in.readLine (); 16 | if (line == null) break; 17 | BigInteger num = new BigInteger (line); 18 | if (num.equals (BigInteger.ONE)){ 19 | out.println (num); 20 | } else { 21 | num = num.shiftLeft (1).subtract (two); 22 | out.println (num); 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /bomarble.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class bomarble 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | int [] dp = new int [1001]; 11 | dp [1] = 5; 12 | for (int i = 2; i <= 1000; i++) { 13 | dp [i] = dp [i - 1] + 5 + 3 * (i - 2) + 2; 14 | } 15 | in = new Reader (); 16 | out = new PrintWriter (System.out, true); 17 | for (;;) { 18 | int n = in.nextInt (); 19 | if (n == 0) break; 20 | out.println (dp [n]); 21 | } 22 | } 23 | } 24 | 25 | /** Faster input **/ 26 | class Reader { 27 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 28 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 29 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 30 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 31 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 32 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 33 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 34 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 35 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 36 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 37 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 38 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 39 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 40 | public void close()throws IOException{if(din==null) return;din.close();} 41 | } -------------------------------------------------------------------------------- /bomedian.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class bomedian 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int t = in.nextInt (); 13 | while (t-- > 0) { 14 | int d = in.nextInt (), n = in.nextInt (); 15 | int [] arr = new int [n], ans = new int [n]; 16 | int idx = 0; 17 | for (int i = 0; i < n; i++) { 18 | int a = in.nextInt (); 19 | // insertion sort 20 | int j = i; 21 | while (j > 0 && a < arr [j - 1]) { 22 | arr [j] = arr [j - 1]; 23 | j--; 24 | } 25 | arr [j] = a; 26 | if (i % 2 == 0) 27 | ans [idx++] = arr [i / 2]; 28 | } 29 | out.print (d + " " + idx); 30 | for (int i = 0; i < idx; i++) { 31 | if (i % 10 == 0) { 32 | out.println (); 33 | out.print (ans [i]); 34 | } 35 | else out.print (" " + ans [i]); 36 | } 37 | if (idx % 10 != 0) out.println (); 38 | } 39 | } 40 | } 41 | 42 | /** Faster input **/ 43 | class Reader { 44 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 45 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 46 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 47 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 48 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 49 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 50 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 51 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 52 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 53 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 54 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 55 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 56 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 57 | public void close()throws IOException{if(din==null) return;din.close();} 58 | } -------------------------------------------------------------------------------- /books1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class books1 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | 12 | int T = parseInt(f.readLine()); 13 | StringTokenizer st; 14 | int[] books, splits; int num, s, i, j, lo, hi, mid; 15 | while(T-- > 0) 16 | { 17 | st = new StringTokenizer(f.readLine()); 18 | num = parseInt(st.nextToken()); 19 | s = parseInt(st.nextToken()); 20 | 21 | st = new StringTokenizer(f.readLine()); 22 | books = new int[num]; 23 | lo = hi = 0; 24 | for(i = 0; i < num; i++) 25 | { 26 | books[i] = parseInt(st.nextToken()); 27 | hi += books[i]; 28 | lo = Math.max(lo, books[i]); 29 | } 30 | 31 | splits = new int[s-1]; 32 | while(lo < hi) 33 | { 34 | mid = (lo+hi)>>1; 35 | if(assign(mid, splits, books)) hi = mid; 36 | else lo = mid+1; 37 | } 38 | 39 | assign(lo, splits, books); 40 | 41 | System.out.print(books[0]); 42 | j = 0; 43 | for(i = 1; i < num; i++) 44 | { 45 | if(j < splits.length && i == splits[j]) 46 | { 47 | System.out.print(" /"); 48 | j++; 49 | } 50 | System.out.print(" " + books[i]); 51 | } 52 | 53 | System.out.println(); 54 | System.out.flush(); 55 | } 56 | System.exit(0); 57 | } 58 | 59 | private static boolean assign(int num, int[] splits, int[] books) 60 | { 61 | int s = splits.length; 62 | int sum = 0; 63 | for(int i = books.length-1; i >= 0; i--) 64 | { 65 | sum += books[i]; 66 | if(sum >= num) 67 | { 68 | if(sum > num) i++; 69 | sum = 0; 70 | if(s <= 0) return false; 71 | splits[--s] = i; 72 | } 73 | 74 | if(i == s) 75 | {// assign all books to remaining scribes 76 | for(; i > 0; i--) 77 | splits[--s] = i; 78 | break; 79 | } 80 | } 81 | return s == 0 && sum < num; 82 | } 83 | } -------------------------------------------------------------------------------- /boperish.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class boperish 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | for (;;) { 13 | int n = in.nextInt (); 14 | if (n == 0) break; 15 | int [] papers = new int [2001]; 16 | for (int i = 0; i < n; i++) 17 | papers [in.nextInt ()]++; 18 | for (int i = 1999; i >= 0; i--) { 19 | papers [i] += papers [i + 1]; 20 | if (papers [i] >= i) { 21 | out.println (i); 22 | break; 23 | } 24 | } 25 | } 26 | } 27 | } 28 | 29 | /** Faster input **/ 30 | class Reader { 31 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 32 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 33 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 34 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 35 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 36 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 37 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 38 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 39 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 40 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 41 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 42 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 43 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 44 | public void close()throws IOException{if(din==null) return;din.close();} 45 | } -------------------------------------------------------------------------------- /bovaluel.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class bovaluel 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int n = in.nextInt (); 13 | while (n-- > 0) { 14 | int t = in.nextInt (); 15 | int [] arr = new int [10]; 16 | for (int i = 0; i < 10; i++) arr [i] = in.nextInt (); 17 | Arrays.sort (arr); 18 | out.println (t + " " + arr [6]); 19 | } 20 | } 21 | } 22 | 23 | /** Faster input **/ 24 | class Reader { 25 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 26 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 27 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 28 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 29 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 30 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 31 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 32 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 33 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 34 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 35 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 36 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 37 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 38 | public void close()throws IOException{if(din==null) return;din.close();} 39 | } -------------------------------------------------------------------------------- /bytese1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class bytese1 6 | { 7 | private static BufferedReader f; 8 | public static final int[] dx = new int[] {-1,0,1,0}; 9 | public static final int[] dy = new int[] {0,-1,0,1}; 10 | 11 | public static void main(String[] args) throws IOException { 12 | f = new BufferedReader(new InputStreamReader(System.in)); 13 | int K = parseInt(f.readLine()); 14 | int t, hx, hy, x, y, i, j, cost, nx, ny, ax, ay; 15 | int[][] grid; PriorityQueue pq; 16 | StringTokenizer st; Position dequeue; 17 | boolean[][] visited; 18 | while(K-->0) 19 | { 20 | st = new StringTokenizer(f.readLine()); 21 | x = parseInt(st.nextToken()); 22 | y = parseInt(st.nextToken()); 23 | grid = new int[x][y]; 24 | for(i = 0; i < x; i++) 25 | { 26 | st = new StringTokenizer(f.readLine()); 27 | for(j = 0; j < y; j++) 28 | grid[i][j] = parseInt(st.nextToken()); 29 | } 30 | st = new StringTokenizer(f.readLine()); 31 | hx = parseInt(st.nextToken())-1; hy = parseInt(st.nextToken())-1; 32 | t = parseInt(st.nextToken()); 33 | pq = new PriorityQueue(); 34 | visited = new boolean[x][y]; 35 | 36 | pq.add(new Position(0,0,grid[0][0])); 37 | cost = 0; 38 | while(pq.size() > 0) 39 | { 40 | dequeue = pq.poll(); 41 | ax = dequeue.x; ay = dequeue.y; 42 | cost = dequeue.cost; 43 | 44 | if(ax == hx && ay == hy) break; 45 | if(visited[ax][ay]) continue; 46 | if(cost > t) break; 47 | visited[ax][ay] = true; 48 | 49 | for(i = 0; i < 4; i++) 50 | { 51 | nx = ax+dx[i]; 52 | ny = ay+dy[i]; 53 | if(nx < 0 || nx >= x || ny < 0 || ny >= y || visited[nx][ny])continue; 54 | 55 | pq.add(new Position(nx,ny,cost+grid[nx][ny])); 56 | } 57 | } 58 | 59 | if(cost <= t) 60 | { 61 | System.out.println("YES"); 62 | System.out.println(t-cost); 63 | } 64 | else System.out.println("NO"); 65 | System.out.flush(); 66 | } 67 | System.exit(0); 68 | } 69 | 70 | static class Position implements Comparable 71 | { 72 | public int x, y, cost; 73 | public Position(int x, int y, int cost) 74 | { 75 | this.x = x; this.y = y; 76 | this.cost = cost; 77 | } 78 | public int compareTo(Position other) 79 | {return cost - other.cost;} 80 | } 81 | } -------------------------------------------------------------------------------- /bytese2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class bytese2 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = parseInt(f.readLine()); 12 | Time[] times; int N, i, j, max, countS, countF; 13 | StringTokenizer st; 14 | while(T-->0) 15 | { 16 | N = parseInt(f.readLine()); 17 | times = new Time[N]; 18 | for(i = 0; i < N; i++) 19 | { 20 | st = new StringTokenizer(f.readLine()); 21 | times[i] = new Time(parseInt(st.nextToken()), parseInt(st.nextToken())); 22 | } 23 | max = 0; 24 | for(i = 0; i < N; i++) 25 | { 26 | countS = 1; 27 | countF = 1; 28 | for(j = 0; j < N; j++) 29 | { 30 | if(times[i].start < times[j].finish && times[i].start > times[j].start) 31 | countS++; 32 | if(times[i].finish < times[j].finish && times[i].finish > times[j].start) 33 | countF++; 34 | } 35 | if(max < countS) max = countS; 36 | if(max < countF) max = countF; 37 | } 38 | System.out.println(max); 39 | System.out.flush(); 40 | } 41 | System.exit(0); 42 | } 43 | 44 | static class Time 45 | { 46 | public int start, finish; 47 | public Time(int start, int finish) 48 | { 49 | this.start = start; 50 | this.finish = finish; 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /bytesm2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class bytesm2 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = parseInt(f.readLine()); 12 | int w, h, i, j, max; StringTokenizer st; 13 | int[][] grid; 14 | while(T-->0) 15 | { 16 | st = new StringTokenizer(f.readLine()); 17 | w = parseInt(st.nextToken()); 18 | h = parseInt(st.nextToken()); 19 | grid = new int[w][h+2]; 20 | for(i = 0; i < w; i++) 21 | { 22 | for(j = 1; j <= h; j++) 23 | { 24 | if(!st.hasMoreTokens()) st = new StringTokenizer(f.readLine()); 25 | grid[i][j] = parseInt(st.nextToken()); 26 | } 27 | grid[i][0] = grid[i][h+1] = -100000000; 28 | } 29 | 30 | for(i = 1; i < w; i++) 31 | for(j = 1; j <= h; j++) 32 | grid[i][j] += max(grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1]); 33 | 34 | max = 0; 35 | for(j = 1; j <= h; j++) 36 | max = max(max, grid[w-1][j]); 37 | 38 | System.out.println(max); 39 | System.out.flush(); 40 | } 41 | System.exit(0); 42 | } 43 | 44 | private static int max(int... x) 45 | { 46 | int max = -Integer.MAX_VALUE; 47 | for(int i : x) if(max < i) max = i; 48 | return max; 49 | } 50 | } -------------------------------------------------------------------------------- /c1ljutnj.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class c1ljutnj 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | long n = in.nextInt (), m = in.nextInt (); 13 | long sum = 0; 14 | for (int i = 0; i < m; i++) 15 | sum += in.nextInt (); 16 | long need = sum - n; 17 | long c = need / m, d = need % m;; 18 | out.println ((m - d) * c * c + d * (c + 1) * (c + 1)); 19 | System.exit (0); 20 | } 21 | } 22 | 23 | /** Faster input **/ 24 | class Reader { 25 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 26 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 27 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 28 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 29 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 30 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 31 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 32 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 33 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 34 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 35 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 36 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 37 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 38 | public void close()throws IOException{if(din==null) return;din.close();} 39 | } -------------------------------------------------------------------------------- /cmpls.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class cmpls 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int T = in.nextInt (); 14 | while (T-- > 0) { 15 | int N = in.nextInt (), K = in.nextInt (); 16 | int [][] dp = new int [N][N + K]; 17 | for (int i = 0; i < N; i++) 18 | dp [0][i] = in.nextInt (); 19 | for (int i = 1; i < N; i++) 20 | for (int j = 0; j < N - i; j++) 21 | dp [i][j] = dp [i - 1][j + 1] - dp [i - 1][j]; 22 | 23 | Arrays.fill (dp [N - 1], dp [N - 1][0]); 24 | int st = 1; 25 | for (int i = N - 2; i >= 0; i--) 26 | for (int j = st++; j < N + K; j++) 27 | dp [i][j] = dp [i][j - 1] + dp [i + 1][j - 1]; 28 | 29 | out.print (dp [0][N]); 30 | for (int i = N + 1; i < N + K; i++) 31 | out.print (" " + dp [0][i]); 32 | out.println (); 33 | } 34 | } 35 | } 36 | 37 | /** Faster input **/ 38 | class Reader { 39 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 40 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 41 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 42 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 43 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 44 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 45 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 46 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 47 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 48 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 49 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 50 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 51 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 52 | public void close()throws IOException{if(din==null) return;din.close();} 53 | } -------------------------------------------------------------------------------- /coins.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class coins 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | private static Map mp; 9 | private static int[] f; 10 | 11 | public static void main(String[] args) throws IOException { 12 | in = new BufferedReader(new InputStreamReader(System.in)); 13 | out = new PrintWriter(System.out, true); 14 | mp = new HashMap(); 15 | mp.put(0, 0L); 16 | String line; 17 | while(true) 18 | { 19 | line = in.readLine(); 20 | if(line == null) break; 21 | int n = Integer.parseInt(line); 22 | out.println(maxConvert(n)); 23 | } 24 | System.exit(0); 25 | } 26 | 27 | public static long maxConvert (int n) 28 | { 29 | if(n == 0) return 0; 30 | long half = n/2, third = n/3, fourth = n/4; 31 | 32 | Long test = mp.get(n/2); 33 | if(test == null) { 34 | half = maxConvert(n/2); 35 | mp.put(n/2, half); 36 | } 37 | else half = test; 38 | 39 | test = mp.get(n/3); 40 | if(test == null) { 41 | third = maxConvert(n/3); 42 | mp.put(n/3, third); 43 | } 44 | else third = test; 45 | 46 | test = mp.get(n/4); 47 | if(test == null) { 48 | fourth = maxConvert(n/4); 49 | mp.put(n/4, fourth); 50 | } 51 | else fourth = test; 52 | 53 | long max = Math.max(n, half+third+fourth); 54 | mp.put(n, max); 55 | return max; 56 | } 57 | } -------------------------------------------------------------------------------- /cointoss.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.math.*; 4 | 5 | public class cointoss { 6 | private static Reader in; 7 | 8 | public static void main (String [] args) throws IOException { 9 | for (int i = (in = new Reader()).nextInt(); i > 0; i--) System.out.printf ("%s.00\n", new BigInteger ("0").setBit (in.nextInt() + 1).subtract (BigInteger.ONE).xor (new BigInteger ("0").setBit (in.nextInt() + 1).subtract (BigInteger.ONE))); 10 | } 11 | 12 | static class Reader { 13 | final private int BUFFER_SIZE = 1 << 16; 14 | private DataInputStream din; 15 | private byte [] buffer; 16 | private int bufferPointer, bytesRead; 17 | 18 | public Reader () { 19 | din = new DataInputStream (System.in); 20 | buffer = new byte[BUFFER_SIZE]; 21 | bufferPointer = bytesRead = 0; 22 | } 23 | 24 | public int nextInt () throws IOException { 25 | int ret = 0; 26 | byte c = read (); 27 | while (c <= ' ') 28 | c = read (); 29 | boolean neg = (c == '-'); 30 | if (neg) 31 | c = read (); 32 | do { 33 | ret = ret * 10 + c - '0'; 34 | } while ((c = read ()) >= '0' && c <= '9'); 35 | if (neg) 36 | return -ret; 37 | return ret; 38 | } 39 | 40 | private void fillBuffer () throws IOException { 41 | bytesRead = din.read (buffer, bufferPointer = 0, BUFFER_SIZE); 42 | if (bytesRead == -1) 43 | buffer[0] = -1; 44 | } 45 | 46 | private byte read () throws IOException { 47 | if (bufferPointer == bytesRead) 48 | fillBuffer (); 49 | return buffer[bufferPointer++]; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /complete.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class complete 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int T = in.nextInt (), idx = 0; 14 | while (T-- > 0) { 15 | int N = in.nextInt (); 16 | boolean [] have = new boolean [1 << 16], need = new boolean [1 << 16]; 17 | for (int i = 0; i < N; i++) { 18 | int a = in.nextInt (); 19 | have [a] = true; 20 | if (!need [a]) { 21 | need [a] = true; 22 | for (int j = 0; j < 1 << 16; j++) 23 | if (need [j]) 24 | need [a & j] = need [a | j] = true; 25 | } 26 | } 27 | 28 | int count = 0; 29 | for (int i = 0; i < 1 << 16; i++) 30 | if (!have [i] && need [i]) 31 | count++; 32 | 33 | out.printf ("Case #%d: %d\n", ++idx, count); 34 | } 35 | } 36 | } 37 | 38 | /** Faster input **/ 39 | class Reader { 40 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 41 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 42 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 43 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 44 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 45 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 46 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 47 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 48 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 49 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 50 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 51 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 52 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 53 | public void close()throws IOException{if(din==null) return;din.close();} 54 | } -------------------------------------------------------------------------------- /cowcar.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class cowcar 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | StringTokenizer st = new StringTokenizer(f.readLine()); 12 | int N = parseInt(st.nextToken()), M = parseInt(st.nextToken()), 13 | D = parseInt(st.nextToken()), L = parseInt(st.nextToken()), i; 14 | int[] S = new int[N]; 15 | for(i = 0; i < N; i++) S[i] = parseInt(f.readLine()); 16 | Arrays.sort(S); 17 | int total = 0; 18 | 19 | for(i = 0; i < N; i++) 20 | if(S[i] - D*(total/M) >= L) 21 | total++; 22 | 23 | System.out.println(total); 24 | System.out.flush(); 25 | System.exit(0); 26 | } 27 | } -------------------------------------------------------------------------------- /cran04.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class cran04 { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main(String[] args) throws IOException { 9 | in = new BufferedReader(new InputStreamReader (System.in)); 10 | out = new PrintWriter(System.out, true); 11 | 12 | int T = Integer.parseInt (in.readLine()); 13 | StringTokenizer st; 14 | while (T-- > 0) { 15 | st = new StringTokenizer (in.readLine()); 16 | int N = Integer.parseInt (st.nextToken()); 17 | int K = Integer.parseInt (st.nextToken()); 18 | 19 | char [] s = in.readLine().toCharArray(); 20 | long res = 0; 21 | int [] count = new int [N + 1]; 22 | int ccount = 0; 23 | count [0]++; 24 | for (char c : s) { 25 | if (c == '1') ccount++; 26 | if (ccount >= K) { 27 | res += count [ccount - K]; 28 | } 29 | count [ccount]++; 30 | } 31 | 32 | out.println (res); 33 | } 34 | out.close(); 35 | System.exit(0); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /ctgame.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class ctgame 6 | { 7 | private static BufferedReader f; 8 | public static int INF = 1000000000; 9 | 10 | public static void main(String[] args) throws IOException { 11 | f = new BufferedReader(new InputStreamReader(System.in)); 12 | int T = parseInt(f.readLine()); 13 | int R, C, i, j, best, index; 14 | int[][] rocks; char[] line; 15 | int[] height, left, right; 16 | while(T-- > 0) 17 | { 18 | StringTokenizer st = new StringTokenizer(f.readLine()); 19 | R = Integer.parseInt(st.nextToken()); 20 | C = Integer.parseInt(st.nextToken()); 21 | ArrayList[] temp = new ArrayList[R]; 22 | rocks = new int[R][0]; 23 | for(i = 0; i < R; i++) 24 | { 25 | temp[i] = new ArrayList(); 26 | temp[i].add(-1); 27 | } 28 | 29 | for(i = 0; i < R; i++) 30 | { 31 | line = f.readLine().toCharArray(); 32 | for(j = 0; j < C*2; j += 2) 33 | if(line[j] == 'R') 34 | temp[i].add(j/2); 35 | } 36 | 37 | for(i = 0; i < R; i++) 38 | { 39 | temp[i].add(C); 40 | Collections.sort(temp[i]); 41 | rocks[i] = new int[temp[i].size()]; 42 | j = 0; 43 | for(int k : temp[i]) 44 | rocks[i][j++] = k; 45 | } 46 | 47 | height = new int[C+1]; 48 | left = new int[C+1]; 49 | right = new int[C+1]; 50 | Arrays.fill(height, 0); 51 | Arrays.fill(left, INF); 52 | Arrays.fill(right, INF); 53 | 54 | best = -1; 55 | for(i = 0; i < R; i++) 56 | { 57 | index = 1; 58 | for(j = 0; j < C; j++) 59 | { 60 | if(j == rocks[i][index]) 61 | { 62 | height[j] = 0; 63 | left[j] = right[j] = INF; 64 | while(j == rocks[i][index]) 65 | index++; 66 | } 67 | else 68 | { 69 | height[j]++; 70 | if(left[j] > j-rocks[i][index-1]) left[j] = j-rocks[i][index-1]; 71 | if(right[j] > rocks[i][index] - j) right[j] = rocks[i][index]-j; 72 | } 73 | if(best < height[j] * (left[j]+right[j]-1)) 74 | best = height[j] * (left[j]+right[j]-1); 75 | } 76 | } 77 | 78 | System.out.println(best*3); 79 | System.out.flush(); 80 | 81 | f.readLine(); 82 | } 83 | 84 | System.exit(0); 85 | } 86 | } -------------------------------------------------------------------------------- /distance.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class distance 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int n = in.nextInt (), d = in.nextInt (); 13 | int [][] points = new int [n][d]; 14 | int max = 0; 15 | for (int i = 0; i < n; i++) { 16 | for (int j = 0; j < d; j++) 17 | points [i][j] = in.nextInt (); 18 | for (int j = 0; j < i; j++) { 19 | int dist = 0; 20 | for (int k = 0; k < d; k++) 21 | dist += Math.abs (points [i][k] - points [j][k]); 22 | if (dist > max) 23 | max = dist; 24 | } 25 | } 26 | out.println (max); 27 | } 28 | } 29 | 30 | /** Faster input **/ 31 | class Reader { 32 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 33 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 34 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 35 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 36 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 37 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 38 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 39 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 40 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 41 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 42 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 43 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 44 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 45 | public void close()throws IOException{if(din==null) return;din.close();} 46 | } -------------------------------------------------------------------------------- /divsum.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class divsum 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | int MAX = 500000; 11 | int[] divSum = new int [MAX + 1]; 12 | for (int i = 1; i <= MAX; i++) 13 | for (int j = i*2; j <= MAX; j += i) 14 | divSum [j] += i; 15 | in = new Reader (); 16 | out = new PrintWriter (System.out, true); 17 | int t = in.nextInt (); 18 | while (t-->0) 19 | out.println (divSum[in.nextInt()] ); 20 | } 21 | } 22 | 23 | /** Faster input **/ 24 | class Reader { 25 | final private int BUFFER_SIZE = 1 << 16; 26 | private DataInputStream din; 27 | private byte[] buffer; 28 | private int bufferPointer, bytesRead; 29 | public Reader(){ 30 | din=new DataInputStream(System.in); 31 | buffer=new byte[BUFFER_SIZE]; 32 | bufferPointer=bytesRead=0; 33 | } 34 | 35 | public Reader(String file_name) throws IOException{ 36 | din=new DataInputStream(new FileInputStream(file_name)); 37 | buffer=new byte[BUFFER_SIZE]; 38 | bufferPointer=bytesRead=0; 39 | } 40 | 41 | public String readLine() throws IOException{ 42 | byte[] buf=new byte[64]; // line length 43 | int cnt=0,c; 44 | while((c=read())!=-1){ 45 | if(c=='\n')break; 46 | buf[cnt++]=(byte)c; 47 | } 48 | return new String(buf,0,cnt); 49 | } 50 | 51 | public int nextInt() throws IOException{ 52 | int ret=0;byte c=read(); 53 | while(c<=' ')c=read(); 54 | boolean neg=(c=='-'); 55 | if(neg)c=read(); 56 | do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 57 | if(neg)return -ret; 58 | return ret; 59 | } 60 | 61 | public long nextLong() throws IOException{ 62 | long ret=0;byte c=read(); 63 | while(c<=' ')c=read(); 64 | boolean neg=(c=='-'); 65 | if(neg)c=read(); 66 | do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 67 | if(neg)return -ret; 68 | return ret; 69 | } 70 | 71 | public double nextDouble() throws IOException{ 72 | double ret=0,div=1;byte c=read(); 73 | while(c<=' ')c=read(); 74 | boolean neg=(c=='-'); 75 | if(neg)c = read(); 76 | do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 77 | if(c=='.')while((c=read())>='0'&&c<='9') 78 | ret+=(c-'0')/(div*=10); 79 | if(neg)return -ret; 80 | return ret; 81 | } 82 | 83 | private void fillBuffer() throws IOException{ 84 | bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE); 85 | if(bytesRead==-1)buffer[0]=-1; 86 | } 87 | 88 | private byte read() throws IOException{ 89 | if(bufferPointer==bytesRead)fillBuffer(); 90 | return buffer[bufferPointer++]; 91 | } 92 | 93 | public void close() throws IOException{ 94 | if(din==null) return; 95 | din.close(); 96 | } 97 | } -------------------------------------------------------------------------------- /dragoncu.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class dragoncu 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | long d; 13 | while ((d = in.nextInt ()) != -1) { 14 | if (d == 0) out.println ("(0,0)"); 15 | else out.println (pos (d)); 16 | } 17 | } 18 | 19 | private static Pair pos (long n) { 20 | Pair c = new Pair (0, 1); 21 | long length = 1; 22 | while (length < n) { 23 | c = new Pair (c.x + c.y, c.y - c.x); 24 | length <<= 1; 25 | } 26 | if (length == n) return c; 27 | Pair c2 = pos (length - n); 28 | return new Pair (c.x - c2.y, c.y + c2.x); 29 | } 30 | 31 | static class Pair { 32 | public long x, y; 33 | public Pair (long _x, long _y) { 34 | x = _x; y = _y; 35 | } 36 | public String toString () { 37 | return "("+y+","+x+")"; 38 | } 39 | } 40 | } 41 | 42 | /** Faster input **/ 43 | class Reader { 44 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 45 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 46 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 47 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 48 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 49 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 50 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 51 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 52 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 53 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 54 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 55 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 56 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 57 | public void close()throws IOException{if(din==null) return;din.close();} 58 | } -------------------------------------------------------------------------------- /dsubseq.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class dsubseq 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int T = in.nextInt (); 14 | long mod = 1000000007; 15 | while (T-- > 0) { 16 | char [] s = in.readLine().toCharArray(); 17 | int N = s.length; 18 | long [] dp = new long [N + 1]; 19 | int [] last = new int [26]; 20 | Arrays.fill (last, -1); 21 | dp[0] = 1; 22 | for (int i = 1; i <= N; i++) { 23 | int k = (int)(s[i - 1] - 'A'); 24 | dp[i] = (2 * dp[i - 1] - (last[k] == -1 ? 0 : dp[last[k] - 1]) + mod); 25 | while (dp[i] >= mod) dp[i] -= mod; 26 | last[k] = i; 27 | } 28 | out.println (dp[N]); 29 | } 30 | } 31 | } 32 | 33 | /** Faster input **/ 34 | class Reader { 35 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 36 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 37 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 38 | private byte[]buf=new byte[150000]; 39 | public String readLine()throws IOException{int cnt=0,c; 40 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 41 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 42 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 43 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 44 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 45 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 46 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 47 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 48 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 49 | public void close()throws IOException{if(din==null) return;din.close();} 50 | } -------------------------------------------------------------------------------- /edist.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class edist 6 | { 7 | private static BufferedReader f; 8 | public static final int MATCH = 1; 9 | public static final int INSERT = 2; 10 | public static final int DELETE = 3; 11 | public static final int REPLACE = 4; 12 | 13 | public static void main(String[] args) throws IOException { 14 | f = new BufferedReader(new InputStreamReader(System.in)); 15 | 16 | int T = parseInt(f.readLine()); 17 | 18 | while(T-- > 0) 19 | { 20 | System.out.println(match(f.readLine(), f.readLine())); 21 | System.out.flush(); 22 | } 23 | System.exit(0); 24 | } 25 | 26 | private static int match(String x, String y) 27 | { 28 | if(x == null || x.length() == 0 || y == null || y.length() == 0) return 0; 29 | 30 | char[] a = x.toCharArray(); 31 | char[] b = y.toCharArray(); 32 | 33 | int n = a.length; 34 | int m = b.length; 35 | 36 | int[][] dp = new int[n+1][m+1]; 37 | int[][] parent = new int[n+1][m+1]; 38 | int i, j; 39 | 40 | dp[0][0] = 0; parent[0][0] = 0; 41 | 42 | for(i = 1; i <= n; i++) { 43 | parent[i][0] = INSERT; 44 | dp[i][0] = dp[i-1][0] + insert(a[i-1]); 45 | } 46 | 47 | for(i = 1; i <= m; i++) { 48 | parent[0][i] = DELETE; 49 | dp[0][i] = dp[0][i-1] + delete(b[i-1]); 50 | } 51 | 52 | for(i = 1; i <= n; i++) { 53 | for(j = 1; j <= m; j++) { 54 | if(a[i-1] == b[j-1]) { 55 | dp[i][j] = dp[i-1][j-1] + match(); 56 | parent[i][j] = MATCH; 57 | } else { 58 | dp[i][j] = dp[i-1][j-1] + replace(a[i-1],b[j-1]); 59 | parent[i][j] = REPLACE; 60 | } 61 | if(dp[i-1][j] + insert(a[i-1]) < dp[i][j]) { 62 | dp[i][j] = dp[i-1][j] + insert(a[i-1]); 63 | parent[i][j] = INSERT; 64 | } 65 | if(dp[i][j-1] + delete(b[j-1]) < dp[i][j]) { 66 | dp[i][j] = dp[i][j-1] + delete(b[j-1]); 67 | parent[i][j] = DELETE; 68 | } 69 | } 70 | } 71 | 72 | return dp[n][m]; 73 | } 74 | 75 | private static int replace(char a, char b) 76 | { 77 | return 1; 78 | } 79 | 80 | private static int delete(char a) 81 | { 82 | return 1; 83 | } 84 | 85 | private static int insert(char b) 86 | { 87 | return 1; 88 | } 89 | 90 | private static int match() 91 | { 92 | return 0; 93 | } 94 | } -------------------------------------------------------------------------------- /eqbox.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class eqbox 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = parseInt(f.readLine()), a, b, x, y; 12 | StringTokenizer st; 13 | while(T-->0) 14 | { 15 | st = new StringTokenizer(f.readLine()); 16 | a = parseInt(st.nextToken()); 17 | b = parseInt(st.nextToken()); 18 | x = parseInt(st.nextToken()); 19 | y = parseInt(st.nextToken()); 20 | if(b < a){a^=b;b^=a;a^=b;} 21 | if(y < x){x^=y;y^=x;x^=y;} 22 | if(x < a && y < b) System.out.println("Escape is possible."); 23 | else System.out.println("Box cannot be dropped."); 24 | System.out.flush(); 25 | } 26 | System.exit(0); 27 | } 28 | } -------------------------------------------------------------------------------- /faketsp.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class faketsp 5 | { 6 | private static PrintWriter out; 7 | 8 | public static void main(String[] args) throws IOException { 9 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 10 | String line = in.readLine(); 11 | int ind1, ind2, ind3; 12 | double x, y, px, py; 13 | double totalDist = 0; 14 | ind1 = line.lastIndexOf('('); 15 | ind2 = line.lastIndexOf(','); 16 | ind3 = line.lastIndexOf(')'); 17 | px = Double.parseDouble(line.substring(ind1+1,ind2)); 18 | py = Double.parseDouble(line.substring(ind2+2,ind3)); 19 | line = in.readLine(); 20 | while(line != null) 21 | { 22 | ind1 = line.lastIndexOf('('); 23 | ind2 = line.lastIndexOf(','); 24 | ind3 = line.lastIndexOf(')'); 25 | x = Double.parseDouble(line.substring(ind1+1,ind2)); 26 | y = Double.parseDouble(line.substring(ind2+2,ind3)); 27 | totalDist += Math.sqrt( (px-x)*(px-x) + (py-y)*(py-y) ); 28 | px = x; py = y; 29 | System.out.printf("The salesman has traveled a total of %.3f kilometers.\n", totalDist); 30 | System.out.flush(); 31 | line = in.readLine(); 32 | } 33 | System.exit(0); 34 | } 35 | } -------------------------------------------------------------------------------- /fctrl.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class fctrl 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = parseInt(f.readLine()); 12 | for(int i = 0; i < T; i++) 13 | { 14 | int k = parseInt(f.readLine()); 15 | 16 | int count = 0; 17 | int five = 5; 18 | 19 | while(five <= k) 20 | { 21 | count += k/five; 22 | five *= 5; 23 | } 24 | 25 | System.out.println(count); 26 | System.out.flush(); 27 | } 28 | System.exit(0); 29 | } 30 | } -------------------------------------------------------------------------------- /fctrl2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | import java.math.BigInteger; 5 | 6 | public class fctrl2 7 | { 8 | private static BufferedReader f; 9 | 10 | public static void main(String[] args) throws IOException { 11 | BigInteger[] big = new BigInteger[101]; 12 | big[1] = BigInteger.ONE; 13 | for(int i = 2; i <= 100; i++) 14 | big[i] = big[i-1].multiply(new BigInteger("" + i)); 15 | f = new BufferedReader(new InputStreamReader(System.in)); 16 | int T = parseInt(f.readLine()); 17 | for(int i = 0; i < T; i++) 18 | { 19 | int k = parseInt(f.readLine()); 20 | System.out.println(big[k]); 21 | System.out.flush(); 22 | } 23 | System.exit(0); 24 | } 25 | } -------------------------------------------------------------------------------- /ftheelf.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Math.*; 4 | 5 | public class ftheelf 6 | { 7 | private static Reader in; 8 | private static PrintWriter out; 9 | private static double v, h; 10 | 11 | public static void main (String [] args) throws IOException { 12 | in = new Reader (); 13 | out = new PrintWriter (System.out, true); 14 | while (true) { 15 | v = in.nextDouble (); 16 | h = in.nextDouble (); 17 | if (v == -1 && h == -1) break; 18 | double g = 9.81; 19 | double lo = 0, hi = Math.PI / 2; 20 | while (lo < hi) { 21 | if (hi - lo <= .000000000001) { 22 | out.printf ("%.12f %.12f\n", (hi + lo) / 2, f ((hi + lo) / 2.0)); 23 | break; 24 | } 25 | double f1 = (2.0 * lo + hi) / 3.0; 26 | double f2 = (lo + 2.0 * hi) / 3.0; 27 | if (f (f1) < f (f2)) lo = f1; 28 | else hi = f2; 29 | } 30 | } 31 | } 32 | 33 | private static double f (double angle) { 34 | double s = sin (angle), c = cos (angle); 35 | return v * c / 9.8 * (v * s + sqrt (v * v * s * s + 2 * 9.8 * h)); 36 | } 37 | } 38 | 39 | /** Faster input **/ 40 | class Reader { 41 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 42 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 43 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 44 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 45 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 46 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 47 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 48 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 49 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 50 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 51 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 52 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 53 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 54 | public void close()throws IOException{if(din==null) return;din.close();} 55 | } -------------------------------------------------------------------------------- /gcj101bb.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class gcj101bb 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int c = in.nextInt (); 13 | for (int d = 1; d <= c; d++) { 14 | int n = in.nextInt (), k = in.nextInt (), 15 | b = in.nextInt (), t = in.nextInt (); 16 | int [] x = new int [n], v = new int [n]; 17 | for (int i = 0; i < n; i++) x [i] = in.nextInt (); 18 | for (int i = 0; i < n; i++) v [i] = in.nextInt (); 19 | int slow = 0, fast = 0, swaps = 0; 20 | for (int i = n - 1; i >= 0; i--) { 21 | if (fast == k) break; 22 | if (x [i] + t * v [i] < b) slow++; 23 | else { 24 | fast++; 25 | swaps += slow; 26 | } 27 | } 28 | out.print ("Case #" + d + ": "); 29 | if (fast >= k) out.println (swaps); 30 | else out.println ("IMPOSSIBLE"); 31 | } 32 | } 33 | } 34 | 35 | /** Faster input **/ 36 | class Reader { 37 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 38 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 39 | public Reader(String file_name) throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 40 | public String readLine() throws IOException{byte[] buf=new byte[64];int cnt=0,c;while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 41 | public int nextInt() throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 42 | public long nextLong() throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 43 | public double nextDouble() throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 44 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 45 | private void fillBuffer() throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 46 | private byte read() throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 47 | public void close() throws IOException{if(din==null) return;din.close();} 48 | } -------------------------------------------------------------------------------- /ghosts.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class ghosts { 5 | private static Reader in; 6 | private static PrintWriter out; 7 | 8 | public static void main (String [] args) throws IOException { 9 | in = new Reader (); 10 | out = new PrintWriter (System.out, true); 11 | 12 | int K = in.nextInt(), T = in.nextInt(); 13 | ArrayList [] arr = new ArrayList [K]; 14 | } 15 | 16 | static class Reader { 17 | final private int BUFFER_SIZE = 1 << 16; 18 | private DataInputStream din; 19 | private byte [] buffer; 20 | private int bufferPointer, bytesRead; 21 | 22 | public Reader () { 23 | din = new DataInputStream (System.in); 24 | buffer = new byte[BUFFER_SIZE]; 25 | bufferPointer = bytesRead = 0; 26 | } 27 | 28 | public Reader (String file_name) throws IOException { 29 | din = new DataInputStream (new FileInputStream (file_name)); 30 | buffer = new byte[BUFFER_SIZE]; 31 | bufferPointer = bytesRead = 0; 32 | } 33 | 34 | public String readLine () throws IOException { 35 | byte [] buf = new byte[1024]; 36 | int cnt = 0, c; 37 | while ((c = read ()) != -1) { 38 | if (c == '\n') break; 39 | buf[cnt++] = (byte) c; 40 | } 41 | return new String (buf, 0, cnt); 42 | } 43 | 44 | public int nextInt () throws IOException { 45 | int ret = 0; 46 | byte c = read (); 47 | while (c <= ' ') 48 | c = read (); 49 | boolean neg = (c == '-'); 50 | if (neg) c = read (); 51 | do { 52 | ret = ret * 10 + c - '0'; 53 | } while ((c = read ()) >= '0' && c <= '9'); 54 | if (neg) return -ret; 55 | return ret; 56 | } 57 | 58 | public long nextLong () throws IOException { 59 | long ret = 0; 60 | byte c = read (); 61 | while (c <= ' ') 62 | c = read (); 63 | boolean neg = (c == '-'); 64 | if (neg) c = read (); 65 | do { 66 | ret = ret * 10 + c - '0'; 67 | } while ((c = read ()) >= '0' && c <= '9'); 68 | if (neg) return -ret; 69 | return ret; 70 | } 71 | 72 | public double nextDouble () throws IOException { 73 | double ret = 0, div = 1; 74 | byte c = read (); 75 | while (c <= ' ') 76 | c = read (); 77 | boolean neg = (c == '-'); 78 | if (neg) c = read (); 79 | do { 80 | ret = ret * 10 + c - '0'; 81 | } while ((c = read ()) >= '0' && c <= '9'); 82 | if (c == '.') while ((c = read ()) >= '0' && c <= '9') 83 | ret += (c - '0') / (div *= 10); 84 | if (neg) return -ret; 85 | return ret; 86 | } 87 | 88 | private void fillBuffer () throws IOException { 89 | bytesRead = din.read (buffer, bufferPointer = 0, BUFFER_SIZE); 90 | if (bytesRead == -1) buffer[0] = -1; 91 | } 92 | 93 | private byte read () throws IOException { 94 | if (bufferPointer == bytesRead) fillBuffer (); 95 | return buffer[bufferPointer++]; 96 | } 97 | 98 | public void close () throws IOException { 99 | if (din == null) return; 100 | din.close (); 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /girlsnbs.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class girlsnbs 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | while (true) { 13 | int b = in.nextInt (), g = in.nextInt (); 14 | if (b == -1 && g == -1) break; 15 | if (g > b) {b^=g;g^=b;b^=g;} 16 | if (g == 0) {out.println (b); continue;} 17 | out.println ((int)Math.ceil ((double)b / ((double)g + 1))); 18 | } 19 | System.exit (0); 20 | } 21 | } 22 | 23 | /** Faster input **/ 24 | class Reader { 25 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 26 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 27 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 28 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 29 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 30 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 31 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 32 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 33 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 34 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 35 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 36 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 37 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 38 | public void close()throws IOException{if(din==null) return;din.close();} 39 | } -------------------------------------------------------------------------------- /glasnici.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class glasnici 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int T = in.nextInt (); 14 | 15 | while (T-- > 0) { 16 | double K = in.nextDouble (); int N = in.nextInt (); 17 | double [] times = new double [N]; 18 | for (int i = 0; i < N; i++) 19 | times [i] = in.nextDouble (); 20 | double lo = 0, hi = 1000000000; 21 | while (hi - lo > 0.001) { 22 | double mid = (lo + hi) / 2; 23 | if (works (mid, times, K)) hi = mid; 24 | else lo = mid; 25 | } 26 | out.printf ("%.3f\n", lo); 27 | } 28 | } 29 | 30 | private static boolean works (double t, double [] arr, double dist) { 31 | double [] next = new double [arr.length]; 32 | System.arraycopy (arr, 0, next, 0, arr.length); 33 | next [0] += t; 34 | for (int i = 1; i < next.length; i++) { 35 | double pos = next [i - 1] + dist; 36 | if (next [i] - t > pos) return false; 37 | next [i] = Math.min (pos, next [i] + t); 38 | } 39 | return true; 40 | } 41 | } 42 | 43 | /** Faster input **/ 44 | class Reader { 45 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 46 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 47 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 48 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 49 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 50 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 51 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 52 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 53 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 54 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 55 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 56 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 57 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 58 | public void close()throws IOException{if(din==null) return;din.close();} 59 | } -------------------------------------------------------------------------------- /gny07a.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class gny07a 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = parseInt(f.readLine()); 12 | StringTokenizer st; 13 | int n, i = 1; String word; 14 | while(T-->0) 15 | { 16 | st = new StringTokenizer(f.readLine()); 17 | n = parseInt(st.nextToken())-1; word = st.nextToken(); 18 | 19 | System.out.format("%d %s%n", i++, word.substring(0,n) + word.substring(n+1)); 20 | System.out.flush(); 21 | } 22 | System.exit(0); 23 | } 24 | } -------------------------------------------------------------------------------- /headshot.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class headshot 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | String s = in.readLine (); 13 | boolean zero = false; 14 | int countAll = 0; int countAfter = 0; 15 | for (char c : s.toCharArray ()) { 16 | if (c == '0') { 17 | countAll++; 18 | if (zero) 19 | countAfter++; 20 | zero = true; 21 | } else zero = false; 22 | } 23 | if (s.charAt (s.length () - 1) == '0' && s.charAt (0) == '0') countAfter++; 24 | int a = countAll * countAll; 25 | int b = countAfter * s.length (); 26 | if (a == b) out.println ("EQUAL"); 27 | else out.println (a > b ? "ROTATE" : "SHOOT"); 28 | } 29 | } 30 | 31 | /** Faster input **/ 32 | class Reader { 33 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 34 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 35 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 36 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 37 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 38 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 39 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 40 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 41 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 42 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 43 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 44 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 45 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 46 | public void close()throws IOException{if(din==null) return;din.close();} 47 | } -------------------------------------------------------------------------------- /hellokit.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class hellokit 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | 9 | public static void main(String[] args) throws IOException { 10 | in = new BufferedReader(new InputStreamReader(System.in)); 11 | out = new PrintWriter(System.out, true); 12 | StringTokenizer st; 13 | char[] word; int len, i, j, k; 14 | while(true) { 15 | st = new StringTokenizer(in.readLine()); 16 | word = st.nextToken().toCharArray(); 17 | if(word.length == 1 && word[0] == '.') break; 18 | len = Integer.parseInt(st.nextToken()); 19 | for(i = 0; i < word.length; i++) // start char 20 | { 21 | for(j = 0; j < len; j++) // repeat 22 | for(k = 0; k < word.length; k++) 23 | out.print(word[(i+k)%word.length]); 24 | out.println(); 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /herding.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class herding 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | public static final int NORTH = 1; 9 | public static final int EAST = 2; 10 | public static final int SOUTH = 3; 11 | public static final int WEST = 4; 12 | private static int [][] grid; 13 | private static boolean [][] visited; 14 | private static int n, m; 15 | 16 | public static void main (String [] args) throws IOException { 17 | in = new BufferedReader (new InputStreamReader (System.in)); 18 | out = new PrintWriter (System.out, true); 19 | StringTokenizer st = new StringTokenizer (in.readLine ()); 20 | n = Integer.parseInt (st.nextToken ()); 21 | m = Integer.parseInt (st.nextToken ()); 22 | grid = new int [n][m]; 23 | for (int i = 0; i < n; i++) { 24 | char [] line = in.readLine ().toCharArray (); 25 | for (int j = 0; j < m; j++) { 26 | switch (line [j]) { 27 | case 'S' : grid [i][j] = SOUTH; break; 28 | case 'W' : grid [i][j] = WEST; break; 29 | case 'N' : grid [i][j] = NORTH; break; 30 | case 'E' : grid [i][j] = EAST; break; 31 | } 32 | } 33 | } 34 | visited = new boolean [n][m]; 35 | int count = 0; 36 | for (int i = 0; i < n; i++) 37 | for (int j = 0; j < m; j++) 38 | if (!visited [i][j]) { 39 | mark (i, j); 40 | count++; 41 | } 42 | 43 | out.println (count); 44 | } 45 | 46 | private static void mark (int x, int y) { 47 | if (visited [x][y]) return; 48 | visited [x][y] = true; 49 | 50 | if (x > 0) 51 | if (grid [x - 1][y] == SOUTH) 52 | mark (x - 1, y); 53 | if (y > 0) 54 | if (grid [x][y - 1] == EAST) 55 | mark (x, y - 1); 56 | if(x < n - 1) 57 | if (grid [x + 1][y] == NORTH) 58 | mark (x + 1, y); 59 | if (y < m - 1) 60 | if (grid [x][y + 1] == WEST) 61 | mark (x, y + 1); 62 | 63 | switch (grid [x][y]) { 64 | case NORTH : mark (x - 1, y); return; 65 | case WEST : mark (x, y - 1); return; 66 | case SOUTH : mark (x + 1, y); return; 67 | case EAST : mark (x, y + 1); return; 68 | } 69 | 70 | } 71 | } -------------------------------------------------------------------------------- /hknap.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class hknap 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = parseInt(f.readLine()); 12 | int N, S, Y, C; 13 | int w, v, i, j; int[] dp; 14 | StringTokenizer st; 15 | while(T-- > 0) 16 | { 17 | st = new StringTokenizer(f.readLine()); 18 | N = parseInt(st.nextToken()); 19 | S = parseInt(st.nextToken()); 20 | Y = parseInt(st.nextToken()); 21 | C = parseInt(st.nextToken()); 22 | dp = new int[Y+1]; 23 | for(i = 0; i < N; i++) 24 | { 25 | st = new StringTokenizer(f.readLine()); 26 | w = parseInt(st.nextToken()); 27 | v = parseInt(st.nextToken()); 28 | for(j = v; j <= Y; j++) 29 | if(dp[j] < dp[j-v] + w) 30 | dp[j] = dp[j-v] + w; 31 | } 32 | long best = 0; 33 | for(i = 0; i <= Y; i++) 34 | if(dp[i] > best) best = dp[i]; 35 | long ans = best; 36 | if(best > C) ans = best*S - C*(S-1); 37 | 38 | System.out.println(ans); 39 | System.out.flush(); 40 | } 41 | System.exit(0); 42 | } 43 | } -------------------------------------------------------------------------------- /hs10gmbl.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class hs10gmbl 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int M = in.nextInt (), N = in.nextInt (); 13 | 14 | PriorityQueue pq = new PriorityQueue (); 15 | for (int i = 0; i < N; i++) 16 | pq.add (new Pair (in.nextInt (), 0)); 17 | 18 | for (int i = 0; i < M; i++) { 19 | Pair d = pq.poll (); 20 | pq.add (new Pair (d.a, d.a + d.b)); 21 | } 22 | 23 | int min = Integer.MAX_VALUE; 24 | while (pq.size () > 0) { 25 | if (pq.peek ().b < min) 26 | min = pq.peek ().b; 27 | pq.poll (); 28 | } 29 | 30 | out.println (Math.max (min - M, 0)); 31 | System.exit (0); 32 | } 33 | 34 | static class Pair implements Comparable { 35 | public int a, b; 36 | public Pair (int $a, int $b) { 37 | a = $a; b = $b; 38 | } 39 | public int compareTo (Pair other) { 40 | return b - other.b; 41 | } 42 | } 43 | } 44 | 45 | /** Faster input **/ 46 | class Reader { 47 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 48 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 49 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 50 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 51 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 52 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 53 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 54 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 55 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 56 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 57 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 58 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 59 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 60 | public void close()throws IOException{if(din==null) return;din.close();} 61 | } -------------------------------------------------------------------------------- /hs10strs.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class hs10strs 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int t = in.nextInt (); 13 | while (t-- > 0) { 14 | int n = in.nextInt (); 15 | char [] s = in.readLine ().toCharArray (); 16 | for (int i = 0; i < 4 * n; i += 4) { 17 | int a = (s [i] - '0') * 10 + (s [i + 2] - '0'); 18 | int b = (s [i + 1] - '0') * 10 + (s [i + 3] - '0'); 19 | out.print ((char)(a + b)); 20 | } 21 | out.println (); 22 | } 23 | } 24 | } 25 | 26 | /** Faster input **/ 27 | class Reader { 28 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 29 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 30 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 31 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 32 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 33 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 34 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 35 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 36 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 37 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 38 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 39 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 40 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 41 | public void close()throws IOException{if(din==null) return;din.close();} 42 | } -------------------------------------------------------------------------------- /importa.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class importa 5 | { 6 | private static PrintWriter out; 7 | 8 | public static void main (String [] args) throws IOException { 9 | BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); 10 | out = new PrintWriter (System.out, true); 11 | double [] exp = new double [27]; 12 | exp [0] = 1; exp [1] = 0.95; 13 | for (int i = 2; i < 27; i++) 14 | exp [i] = exp [i - 1] * 0.95; 15 | for (;;) { 16 | String line = in.readLine (); 17 | if (line == null) break; 18 | int N = Integer.parseInt (line); 19 | int [][] grid = new int [27][27]; 20 | int [] val = new int [27]; 21 | for (int i = 0; i < 27; i++) { 22 | Arrays.fill (grid [i], 100000000); 23 | val [i] = -1000000000; 24 | } 25 | for (int i = 0; i < N; i++) { 26 | StringTokenizer st = new StringTokenizer (in.readLine ()); 27 | int planet = (int)(st.nextToken ().charAt (0) - 'A'); 28 | val [planet] = (int)(Double.parseDouble (st.nextToken ()) * 95); 29 | char [] connect = st.nextToken ().toCharArray (); 30 | for (int j = 0; j < connect.length; j++) { 31 | if (connect [j] == '*') grid [planet][26] = 1; 32 | else grid [planet][connect [j] - 'A'] = 1; 33 | } 34 | } 35 | for (int i = 0; i < 27; i++) 36 | for (int j = 0; j < 27; j++) 37 | for (int k = 0; k < 27; k++) 38 | if (grid [i][k] + grid [k][j] < grid [i][j]) 39 | grid [i][j] = grid [i][k] + grid [k][j]; 40 | 41 | int max = 0; char planet = 'A' - 1; 42 | for (int i = 0; i < 26; i++) { 43 | if (grid [i][26] < 27) 44 | val [i] *= exp [grid [i][26]]; 45 | else 46 | val [i] = 0; 47 | if (val [i] > max) { 48 | max = val [i]; 49 | planet = (char) (i + 'A'); 50 | } 51 | } 52 | out.println ("Import from " + planet); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /incards.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class incards 6 | { 7 | private static BufferedReader f; 8 | public static final int INF = 1000000000; 9 | 10 | public static void main(String[] args) throws IOException { 11 | f = new BufferedReader(new InputStreamReader(System.in)); 12 | int T = parseInt(f.readLine()); 13 | StringTokenizer st; 14 | int P, Q, i, j, a, b, w; 15 | ArrayList[] fw; 16 | ArrayList[] bk; 17 | while(T-- > 0) 18 | { 19 | st = new StringTokenizer(f.readLine()); 20 | P = parseInt(st.nextToken()); 21 | Q = parseInt(st.nextToken()); 22 | 23 | fw = new ArrayList[P+1]; 24 | bk = new ArrayList[P+1]; 25 | for(i = 1; i <= P; i++) 26 | { 27 | fw[i] = new ArrayList(); 28 | bk[i] = new ArrayList(); 29 | } 30 | 31 | for(i = 0; i < Q; i++) 32 | { 33 | st = new StringTokenizer(f.readLine()); 34 | a = parseInt(st.nextToken()); 35 | b = parseInt(st.nextToken()); 36 | w = parseInt(st.nextToken()); 37 | fw[a].add(new Edge(b, w)); 38 | bk[b].add(new Edge(a, w)); 39 | } 40 | 41 | int sum = dijkstra(P,1,fw) + dijkstra(P,1,bk); 42 | 43 | System.out.println(sum); 44 | System.out.flush(); 45 | } 46 | System.exit(0); 47 | } 48 | 49 | private static int dijkstra(int N, int start, ArrayList[] grid) 50 | { 51 | int[] dist = new int[N+1]; 52 | boolean[] visited = new boolean[N+1]; 53 | PriorityQueue queue = new PriorityQueue(); 54 | queue.add(new Edge(start,0)); 55 | Edge dequeue; int node, weight, cost = 0; 56 | while(queue.size() != 0) 57 | { 58 | dequeue = queue.poll(); 59 | node = dequeue.to; 60 | weight = dequeue.weight; 61 | if(visited[node]) continue; 62 | visited[node] = true; 63 | dist[node] = weight; 64 | cost += weight; 65 | for(Edge e : grid[node]) 66 | if(!visited[e.to]) 67 | queue.add(new Edge(e.to, e.weight + weight)); 68 | } 69 | return cost; 70 | } 71 | 72 | static class Edge implements Comparable 73 | { 74 | public int to, weight; 75 | 76 | public Edge(int to, int weight) 77 | { 78 | this.to = to; 79 | this.weight = weight; 80 | } 81 | 82 | public int compareTo(Edge e) 83 | { 84 | return weight - e.weight; 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /intest.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class intest 4 | { 5 | public static void main(String[] args) throws IOException 6 | { 7 | Parser in = new Parser(System.in); 8 | int n = in.nextInt(); 9 | int k = in.nextInt(); 10 | int count = 0; 11 | for(int i = 0; i < n; i++) 12 | { 13 | int a = in.nextInt(); 14 | if(a%k==0) count++; 15 | } 16 | PrintWriter out = new PrintWriter(System.out, true); 17 | out.println(count); 18 | System.exit(0); 19 | } 20 | } 21 | 22 | class Parser 23 | { 24 | final private int BUFFER_SIZE = 1 << 16; 25 | 26 | private DataInputStream din; 27 | private byte[] buffer; 28 | private int bufferPointer, bytesRead; 29 | 30 | public Parser(InputStream in) 31 | { 32 | din = new DataInputStream(in); 33 | buffer = new byte[BUFFER_SIZE]; 34 | bufferPointer = bytesRead = 0; 35 | } 36 | 37 | public int nextInt() throws IOException 38 | { 39 | int ret = 0; 40 | byte c = read(); 41 | while (c <= ' ') c = read(); 42 | boolean neg = c == '-'; 43 | if (neg) c = read(); 44 | do 45 | { 46 | ret = ret * 10 + c - '0'; 47 | c = read(); 48 | } while (c > ' '); 49 | if (neg) return -ret; 50 | return ret; 51 | } 52 | 53 | private void fillBuffer() throws IOException 54 | { 55 | bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 56 | if (bytesRead == -1) buffer[0] = -1; 57 | } 58 | 59 | private byte read() throws IOException 60 | { 61 | if (bufferPointer == bytesRead) fillBuffer(); 62 | return buffer[bufferPointer++]; 63 | } 64 | } -------------------------------------------------------------------------------- /itrix_b.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class itrix_b 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | private static char [] num = new char [] {'2', '3', '5', '7'}; 10 | 11 | public static void main (String [] args) throws IOException { 12 | in = new Reader (); 13 | out = new PrintWriter (System.out, true); 14 | 15 | int T = in.nextInt (); 16 | 17 | while (T-- > 0) { 18 | long p = in.nextLong (); 19 | Node root = null; 20 | while (p > 0) { 21 | p--; 22 | root = new Node (num [(int)(p & 3)], root); 23 | p /= 4; 24 | } 25 | 26 | out.println (root); 27 | } 28 | } 29 | 30 | static class Node { 31 | public char cur; 32 | public Node next; 33 | 34 | public Node (char cur, Node next) { 35 | this.cur = cur; 36 | this.next = next; 37 | } 38 | 39 | public String toString () { 40 | return cur + (next != null ? next.toString () : ""); 41 | } 42 | } 43 | } 44 | 45 | /** Faster input **/ 46 | class Reader { 47 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 48 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 49 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 50 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 51 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 52 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 53 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 54 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 55 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 56 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 57 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 58 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 59 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 60 | public void close()throws IOException{if(din==null) return;din.close();} 61 | } -------------------------------------------------------------------------------- /johnny.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class johnny 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int n = in.nextInt (); 13 | Candy [] candies = new Candy [n]; 14 | for (int i = 0; i < n; i++) candies [i] = new Candy (in.nextInt (), i + 1); 15 | Arrays.sort (candies); 16 | int left = -1, right = n; long lSum = 0, rSum = 0; 17 | rSum += candies [--right].weight; 18 | int [] idx = new int [n]; int i = 0; 19 | outer : while (left != right) { 20 | while (lSum < rSum) { 21 | lSum += candies [++left].weight; 22 | idx [i++] = candies [left].index; 23 | if (left == right) break outer; 24 | } 25 | rSum += candies [--right].weight; 26 | } 27 | for (int j = 0; j < i; j++) out.println (idx [j]); 28 | } 29 | 30 | static class Candy implements Comparable { 31 | public int index; 32 | public long weight; 33 | public Candy (long _weight, int _index) { 34 | weight = _weight; index = _index; 35 | } 36 | public int compareTo (Candy c) { 37 | return (weight == c.weight ? 0 : (weight > c.weight) ? 1 : -1); 38 | } 39 | } 40 | } 41 | 42 | /** Faster input **/ 43 | class Reader { 44 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 45 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 46 | public Reader(String file_name) throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 47 | public String readLine() throws IOException{byte[] buf=new byte[64];int cnt=0,c;while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 48 | public int nextInt() throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 49 | public long nextLong() throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 50 | public double nextDouble() throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 51 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 52 | private void fillBuffer() throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 53 | private byte read() throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 54 | public void close() throws IOException{if(din==null) return;din.close();} 55 | } -------------------------------------------------------------------------------- /julka.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | import java.math.BigInteger; 5 | 6 | public class julka 7 | { 8 | private static BufferedReader f; 9 | 10 | public static void main(String[] args) throws IOException { 11 | f = new BufferedReader(new InputStreamReader(System.in)); 12 | BigInteger total, more, n; 13 | for(int i = 0; i < 10; i++) 14 | { 15 | total = new BigInteger(f.readLine()); 16 | more = new BigInteger(f.readLine()); 17 | n = total.subtract(more).shiftRight(1); 18 | System.out.println( n.add(more) ); 19 | System.out.println( n ); 20 | System.out.flush(); 21 | } 22 | System.exit(0); 23 | } 24 | } -------------------------------------------------------------------------------- /lastdig.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | public class lastdig{ 4 | public static void main(String[] args) throws IOException{ 5 | Scanner in = new Scanner(System.in); 6 | int t=in.nextInt(), a, i, c; long b; String s; 7 | while(t-->0){ 8 | s=in.next();a=Integer.parseInt(s.substring(s.length()-1)); 9 | b=in.nextLong();a%=10; 10 | if(b == 0){System.out.println(1); continue;} 11 | if(a == 0){System.out.println(0); continue;} 12 | b%=4;c=1;if(b==0)b=4; 13 | for(i = 0; i < b; i++)c*=a; 14 | System.out.println(c%10); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /main8_c.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class main8_c 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int T = in.nextInt (); 13 | while (T-- > 0) { 14 | int N = in.nextInt (), K = in.nextInt (); 15 | int max = 0; 16 | int [] arr = new int [N]; 17 | for (int i = 0; i < N; i++) { 18 | arr [i] = in.nextInt (); 19 | if (arr [i] > max) max = arr [i]; 20 | } 21 | int lo = 0, hi = max; 22 | while (lo < hi) { 23 | int mid = (lo + hi + 1) >> 1; 24 | if (works (arr, K, mid)) lo = mid; 25 | else hi = mid - 1; 26 | } 27 | out.println (lo); 28 | } 29 | out.close (); 30 | System.exit (0); 31 | } 32 | 33 | private static boolean works (int [] arr, int left, int each) { 34 | for (int i = 0; i < arr.length; i++) { 35 | left -= arr [i] / each; 36 | if (left <= 0) return true; 37 | } 38 | return false; 39 | } 40 | } 41 | 42 | /** Faster input **/ 43 | class Reader { 44 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 45 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 46 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 47 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 48 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 49 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 50 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 51 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 52 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 53 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 54 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 55 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 56 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 57 | public void close()throws IOException{if(din==null) return;din.close();} 58 | } -------------------------------------------------------------------------------- /majstor.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | public class majstor{ 4 | public static void main(String[] a) throws IOException{ 5 | Scanner in=new Scanner(System.in); 6 | int r=in.nextInt(),f,i,j,c=0,m=0; 7 | int[] p=co(in.next().toCharArray()),fr; 8 | f=in.nextInt(); 9 | int[][] o=new int[r][3]; 10 | for(i=0;i='0'&&c<='9');if(neg)return -ret;return ret;} 38 | public long nextLong() throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 39 | public double nextDouble() throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 40 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 41 | private void fillBuffer() throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 42 | private byte read() throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 43 | public void close() throws IOException{if(din==null) return;din.close();} 44 | } -------------------------------------------------------------------------------- /mcl.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class mcl 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | 9 | public static void main(String[] args) throws IOException { 10 | in = new BufferedReader(new InputStreamReader(System.in)); 11 | out = new PrintWriter(System.out, true); 12 | 13 | String line = in.readLine(); 14 | char[] array; int i; int[] count; 15 | int max; char letter; 16 | while(line != null) { 17 | array = line.toUpperCase().toCharArray(); 18 | count = new int[26]; 19 | for(i = 0; i < array.length; i++) 20 | if(array[i] >= 'A' && array[i] <= 'Z') 21 | count[array[i] - 'A']++; 22 | max = 0; letter = (char)('A'-1); 23 | for(i = 0; i < 26; i++) 24 | if(count[i] > max) { 25 | max = count[i]; 26 | letter = (char)(i+'A'); 27 | } 28 | out.println(letter + " " + max); 29 | line = in.readLine(); 30 | } 31 | System.exit(0); 32 | } 33 | } -------------------------------------------------------------------------------- /mcoins.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class mcoins 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | StringTokenizer st = new StringTokenizer(f.readLine()); 12 | int k = parseInt(st.nextToken()); 13 | int l = parseInt(st.nextToken()); 14 | int m = parseInt(st.nextToken()); 15 | int i; 16 | boolean[] dp = new boolean[1000001]; // true if A can win, false otherwise 17 | for(i = 1; i < k; i++) 18 | dp[i] = !dp[i-1]; 19 | for(i = k; i < l; i++) 20 | dp[i] = !dp[i-1] || !dp[i-k]; 21 | for(i = l; i <= 1000000; i++) 22 | dp[i]= !dp[i-1] || !dp[i-k] || !dp[i-l]; 23 | st = new StringTokenizer(f.readLine()); 24 | for(i = 0; i < m; i++) 25 | if(dp[parseInt(st.nextToken())]) System.out.print("A"); 26 | else System.out.print("B"); 27 | System.out.println(); 28 | System.out.flush(); 29 | System.exit(0); 30 | } 31 | } -------------------------------------------------------------------------------- /mmmgame.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class mmmgame 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int t = in.nextInt (); 13 | while (t-- > 0) { 14 | int n = in.nextInt (); 15 | int xor = 0; 16 | for (int i = 0; i < n; i++) xor ^= in.nextInt (); 17 | out.println (xor == 0 ? "Brother" : "John"); 18 | } 19 | } 20 | } 21 | 22 | /** Faster input **/ 23 | class Reader { 24 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 25 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 26 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 27 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 28 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 29 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 30 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 31 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 32 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 33 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 34 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 35 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 36 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 37 | public void close()throws IOException{if(din==null) return;din.close();} 38 | } -------------------------------------------------------------------------------- /movmrbl.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class movmrbl 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int t = parseInt(f.readLine()); int n, k, p, r; 12 | StringTokenizer st; String line; 13 | while(t-->0) 14 | { 15 | line = f.readLine(); 16 | while(line.length() < 1) line = f.readLine(); 17 | st = new StringTokenizer(line); 18 | n = parseInt(st.nextToken()); 19 | k = parseInt(st.nextToken()); 20 | p = k; r = k-n; 21 | if(n > k) 22 | { 23 | r = 0; 24 | p = n; 25 | } 26 | System.out.println(p + " " + r); 27 | System.out.flush(); 28 | } 29 | System.exit(0); 30 | } 31 | } -------------------------------------------------------------------------------- /mpilot.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class mpilot 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int N = in.nextInt(); 14 | 15 | long [] dp = new long [N / 2 + 1]; 16 | Arrays.fill (dp, 1l << 55); dp [0] = 0; 17 | long sum = 0; 18 | for (int i = 0; i < N; i++) { 19 | int a = in.nextInt(), b = in.nextInt(); 20 | sum += b; int inc = a - b; 21 | dp [0] = 0; 22 | for (int j = (i + 1) / 2; j >= 1; j--) 23 | if (dp [j - 1] + inc < dp [j]) 24 | dp [j] = dp [j - 1] + inc; 25 | } 26 | 27 | out.println (dp [N / 2] + sum); 28 | } 29 | } 30 | 31 | /** Faster input **/ 32 | class Reader { 33 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 34 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 35 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 36 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 37 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 38 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 39 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 40 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 41 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 42 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 43 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 44 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 45 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 46 | public void close()throws IOException{if(din==null) return;din.close();} 47 | } -------------------------------------------------------------------------------- /mtwalk.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | import static java.lang.Math.abs; 5 | 6 | public class mtwalk 7 | { 8 | private static BufferedReader f; 9 | private static int[][] grid; 10 | private static boolean[][] visited; 11 | public static final int[] dx = new int[] {-1,0,1,0}; 12 | public static final int[] dy = new int[] {0,-1,0,1}; 13 | 14 | public static void main(String[] args) throws IOException { 15 | f = new BufferedReader(new InputStreamReader(System.in)); 16 | int n = parseInt(f.readLine()); 17 | grid = new int[n+2][n+2]; 18 | int i, j; 19 | StringTokenizer st; 20 | visited = new boolean[n+2][n+2]; 21 | for(i = 1; i <= n; i++) { 22 | st = new StringTokenizer(f.readLine()); 23 | visited[i][0] = visited[i][n+1] = visited[n+1][i] = visited[0][i] = true; 24 | for(j = 1; j <= n; j++) { 25 | grid[i][j] = parseInt(st.nextToken()); 26 | visited[i][j] = false; 27 | } 28 | } 29 | visited[0][0] = visited[0][n+1] = visited[n+1][0] = visited[n+1][n+1] = true; 30 | PriorityQueue pq = new PriorityQueue(); 31 | pq.add(new State(1,1,grid[1][1], grid[1][1])); 32 | State dequeue = null; 33 | int x, y; 34 | 35 | while(pq.size() > 0) { 36 | dequeue = pq.poll(); 37 | x = dequeue.x; y = dequeue.y; 38 | if(x == n && y == n) break; 39 | if(visited[x][y]) continue; 40 | visited[x][y] = true; 41 | for(i = 0; i < 4; i++) 42 | if(!visited[x+dx[i]][y+dy[i]]) 43 | pq.add(new State(x+dx[i], y+dy[i], 44 | Math.min(dequeue.min, grid[x+dx[i]][y+dy[i]]), 45 | Math.max(dequeue.max, grid[x+dx[i]][y+dy[i]]))); 46 | } 47 | 48 | System.out.println(dequeue.change); 49 | System.out.flush(); 50 | System.exit(0); 51 | } 52 | 53 | static class State implements Comparable 54 | { 55 | public int x, y, min, max, change; 56 | 57 | public State(int x, int y, int min, int max) { 58 | this.x = x; this.y = y; this.min = min; 59 | this.max = max; change = max - min; 60 | } 61 | 62 | public int compareTo(State other) { 63 | return change - other.change; 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /nagay1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class nagay1 { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main(String[] args) throws IOException { 9 | in = new BufferedReader(new InputStreamReader (System.in)); 10 | out = new PrintWriter(System.out, true); 11 | 12 | StringTokenizer st = new StringTokenizer (in.readLine ()); 13 | int N = Integer.parseInt (st.nextToken ()), M = Integer.parseInt (st.nextToken ()); 14 | char [][] curgrid = new char [N][M]; 15 | for (int i = 0; i < N; i++) 16 | curgrid [i] = in.readLine().toCharArray(); 17 | 18 | int vx = 0, vy = 0, jx = 0, jy = 0; 19 | for (int i = 0; i < N; i++) 20 | for (int j = 0; j < M; j++) { 21 | if (curgrid [i][j] == 'V') { 22 | vx = i; vy = j; 23 | } 24 | if (curgrid [i][j] == 'J') { 25 | jx = i; jy = j; 26 | } 27 | } 28 | 29 | int dist = 0; 30 | while (true) { 31 | boolean [][] vis = new boolean [N][M]; 32 | for (int i = 0; i < N; i++) 33 | for (int j = 0; j < M; j++) 34 | vis [i][j] = (curgrid [i][j] == '+'); 35 | 36 | int [] queuex = new int [N * M]; 37 | int [] queuey = new int [N * M]; 38 | int front = 0, back = 0; 39 | if (!vis [vx][vy]) { 40 | queuex [back] = vx; queuey [back++] = vy; 41 | vis [vx][vy] = true; 42 | } 43 | boolean found = false; 44 | while (front < back) { 45 | int cx = queuex [front], cy = queuey [front++]; 46 | if (cx == jx && cy == jy) { 47 | found = true; 48 | break; 49 | } 50 | if (cx - 1 >= 0 && !vis [cx - 1][cy]) { 51 | queuex [back] = cx - 1; 52 | queuey [back++] = cy; 53 | vis [cx - 1][cy] = true; 54 | } 55 | if (cx + 1 < N && !vis [cx + 1][cy]) { 56 | queuex [back] = cx + 1; 57 | queuey [back++] = cy; 58 | vis [cx + 1][cy] = true; 59 | } 60 | if (cy - 1 >= 0 && !vis [cx][cy - 1]) { 61 | queuex [back] = cx; 62 | queuey [back++] = cy - 1; 63 | vis [cx][cy - 1] = true; 64 | } 65 | if (cy + 1 < M && !vis [cx][cy + 1]) { 66 | queuex [back] = cx; 67 | queuey [back++] = cy + 1; 68 | vis [cx][cy + 1] = true; 69 | } 70 | } 71 | 72 | if (!found) break; 73 | char [][] ngrid = new char [N][M]; 74 | for (int i = 0; i < N; i++) 75 | for (int j = 0; j < M; j++) { 76 | if (curgrid [i][j] == '+') { 77 | ngrid [i][j] = '+'; 78 | if (i - 1 >= 0) 79 | ngrid [i - 1][j] = '+'; 80 | if (i + 1 < N) 81 | ngrid [i + 1][j] = '+'; 82 | if (j - 1 >= 0) 83 | ngrid [i][j - 1] = '+'; 84 | if (j + 1 < M) 85 | ngrid [i][j + 1] = '+'; 86 | } 87 | } 88 | curgrid = ngrid; 89 | dist++; 90 | } 91 | 92 | out.println (dist); 93 | out.close(); 94 | System.exit(0); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /nfactor.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class nfactor 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int T = in.nextInt(); 14 | 15 | int maxn = 1000001; 16 | int [] sieve = new int [maxn]; 17 | sieve [0] = 0; 18 | for (int i = 2; i < maxn; i += 2) sieve [i]++; 19 | for (int i = 3; i < maxn; i += 2) { 20 | if (sieve [i] == 0) 21 | for (int j = i; j < maxn; j += i) 22 | sieve [j]++; 23 | } 24 | 25 | int [][] psum = new int [maxn][11]; 26 | psum [1][0] = 1; 27 | for (int i = 2; i < maxn; i++) { 28 | for (int j = 0; j < 10; j++) 29 | psum [i][j] = psum [i - 1][j] + (sieve [i] == j ? 1 : 0); 30 | } 31 | 32 | while (T-- > 0) { 33 | int a = in.nextInt(), b = in.nextInt(), n = in.nextInt(); 34 | out.println (psum [b][n] - psum [a - 1][n]); 35 | } 36 | } 37 | } 38 | 39 | /** Faster input **/ 40 | class Reader { 41 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 42 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 43 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 44 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 45 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 46 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 47 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 48 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 49 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 50 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 51 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 52 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 53 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 54 | public void close()throws IOException{if(din==null) return;din.close();} 55 | } -------------------------------------------------------------------------------- /nicebtre.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class nicebtre { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main(String[] args) throws IOException { 9 | in = new BufferedReader(new InputStreamReader (System.in)); 10 | out = new PrintWriter(System.out, true); 11 | 12 | int T = Integer.parseInt (in.readLine()); 13 | 14 | while (T-- > 0) { 15 | String s = in.readLine(); 16 | if (s.equals ("l")) { 17 | out.println (0); 18 | continue; 19 | } 20 | int depth = 0, ans = 0; 21 | boolean [] lc = new boolean [s.length()]; 22 | lc [0] = true; 23 | for (char c : s.toCharArray()) { 24 | if (c == 'n') { 25 | depth++; 26 | lc [depth] = true; 27 | } else if (c == 'l') { 28 | while (!lc [depth]) 29 | depth--; 30 | lc [depth] = false; 31 | } 32 | ans = Math.max (ans, depth); 33 | } 34 | out.println (ans); 35 | } 36 | out.close(); 37 | System.exit(0); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ntkm.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class ntkm 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int N = in.nextInt(); 14 | 15 | PriorityQueue pq = new PriorityQueue (); 16 | for (int i = 0; i < N; i++) pq.add (in.nextLong()); 17 | 18 | long cost = 0; 19 | while (pq.size() > 1) { 20 | long a = pq.poll () + pq.poll (); 21 | cost += a; 22 | pq.add (a); 23 | } 24 | 25 | out.println (cost); 26 | } 27 | } 28 | 29 | /** Faster input **/ 30 | class Reader { 31 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 32 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 33 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 34 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 35 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 36 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 37 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 38 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 39 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 40 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 41 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 42 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 43 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 44 | public void close()throws IOException{if(din==null) return;din.close();} 45 | } -------------------------------------------------------------------------------- /ny10e.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class ny10e 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | long [][] ways = new long [10][65]; 14 | for (int i = 0; i <= 9; i++) 15 | ways [i][1] = 1; 16 | 17 | for (int j = 2; j <= 64; j++) { 18 | long sum = 0; 19 | for (int i = 0; i <= 9; i++) 20 | ways [i][j] = sum += ways [i][j - 1]; 21 | } 22 | 23 | int P = in.nextInt (); 24 | while (P-- > 0) { 25 | int c = in.nextInt (), k = in.nextInt (); 26 | long ans = 0; 27 | for (int i = 0; i <= 9; i++) 28 | ans += ways [i][k]; 29 | out.printf ("%d %d\n", c, ans); 30 | } 31 | } 32 | } 33 | 34 | /** Faster input **/ 35 | class Reader { 36 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 37 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 38 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 39 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 40 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 41 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 42 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 43 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 44 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 45 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 46 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 47 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 48 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 49 | public void close()throws IOException{if(din==null) return;din.close();} 50 | } -------------------------------------------------------------------------------- /onp.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class onp 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | 9 | public static void main(String[] args) throws IOException { 10 | in = new BufferedReader(new InputStreamReader(System.in)); 11 | out = new PrintWriter(System.out, true); 12 | int n = Integer.parseInt(in.readLine()); 13 | char[] sequence; 14 | while(n-->0){ 15 | sequence = in.readLine().toCharArray(); 16 | out.println(convert(sequence)); 17 | } 18 | System.exit(0); 19 | } 20 | 21 | private static String convert(char[] in) 22 | { 23 | Stack operator = new Stack(); 24 | Queue output = new LinkedList(); 25 | int i; 26 | for(i = 0; i < in.length; i++) { 27 | if(Character.isLetter(in[i])) 28 | output.offer(in[i]); 29 | else if(get(in[i]) > 0) { 30 | while(operator.size() > 0 && get(operator.peek())>0 && 31 | get(in[i]) > get(operator.peek())) 32 | output.offer(operator.pop()); 33 | operator.push(in[i]); 34 | } 35 | else if(in[i] == '(') 36 | operator.push('('); 37 | else if(in[i] == ')') 38 | while(operator.size() > 0) { 39 | if(operator.peek() == '(') { 40 | operator.pop(); 41 | break; 42 | } 43 | output.offer(operator.peek()); 44 | operator.pop(); 45 | } 46 | 47 | } 48 | while(operator.size()>0) 49 | output.add(operator.pop()); 50 | String res = ""; 51 | while(output.size()>0) 52 | res += output.poll(); 53 | return res; 54 | } 55 | 56 | private static int get(char sign) { 57 | switch(sign) { 58 | case '+' : return 1; 59 | case '-' : return 2; 60 | case '*' : return 3; 61 | case '/' : return 4; 62 | case '^' : return 5; 63 | } 64 | return -1; 65 | } 66 | } -------------------------------------------------------------------------------- /pairsort.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class pairsort 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | // 55 44 33 22 11 14 | // 45 54 33 22 11 15 | // 45 34 53 22 11 16 | // 45 34 23 52 11 17 | // 45 34 23 12 51 18 | // 45 32 43 12 51 19 | // 42 35 43 12 51 20 | // 42 35 13 42 51 21 | // 42 31 53 42 51 22 | // 12 34 53 42 51 23 | // 12 34 23 45 51 24 | // 12 34 23 41 55 25 | // 12 32 43 41 55 26 | // 12 32 31 44 55 27 | // 12 12 33 44 55 28 | // 11 22 33 44 55 29 | } 30 | } 31 | 32 | /** Faster input **/ 33 | class Reader { 34 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 35 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 36 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 37 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 38 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 39 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 40 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 41 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 42 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 43 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 44 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 45 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 46 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 47 | public void close()throws IOException{if(din==null) return;din.close();} 48 | } -------------------------------------------------------------------------------- /party.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class party 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | StringTokenizer st = new StringTokenizer(f.readLine()); 12 | 13 | int n = parseInt(st.nextToken()); 14 | int k = parseInt(st.nextToken()); 15 | int[] dp; int i, j, a, b, used, fun; 16 | while(n != 0 && k != 0) 17 | { 18 | dp = new int[n+1]; 19 | Arrays.fill(dp, -1); 20 | dp[0] = 0; 21 | fun = used = 0; 22 | for(i = 0; i < k; i++) 23 | { 24 | st = new StringTokenizer(f.readLine()); 25 | a = parseInt(st.nextToken()); 26 | b = parseInt(st.nextToken()); 27 | for(j = n; j >= a; j--) 28 | if(dp[j-a] >= 0 && dp[j] < dp[j-a] + b) 29 | dp[j] = dp[j-a] + b; 30 | } 31 | for(i = 0; i <= n; i++) 32 | if(dp[i] > fun) 33 | { 34 | fun = dp[i]; 35 | used = i; 36 | } 37 | 38 | System.out.format("%d %d%n", used, fun); 39 | System.out.flush(); 40 | 41 | f.readLine(); 42 | st = new StringTokenizer(f.readLine()); 43 | n = parseInt(st.nextToken()); 44 | k = parseInt(st.nextToken()); 45 | } 46 | 47 | System.exit(0); 48 | } 49 | } -------------------------------------------------------------------------------- /pebble.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class pebble 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new BufferedReader (new InputStreamReader (System.in)); 11 | out = new PrintWriter (System.out, true); 12 | 13 | String line = in.readLine (); 14 | int count = 0; 15 | while (line != null) { 16 | out.printf ("Game #%d: %d\n", ++count, score (line)); 17 | line = in.readLine (); 18 | } 19 | } 20 | 21 | private static int score (String line) { 22 | char curBit = line.charAt (line.length () - 1); 23 | int count = 1; 24 | for (int i = line.length () - 2; i >= 0; i--) { 25 | if (curBit != line.charAt (i)) { 26 | count++; 27 | curBit = line.charAt (i); 28 | } 29 | } 30 | if (line.charAt (0) == '0') count--; 31 | return count; 32 | } 33 | } -------------------------------------------------------------------------------- /pigbank.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class pigbank 6 | { 7 | private static BufferedReader f; 8 | public static final int INF = 1000000000; 9 | 10 | public static void main(String[] args) throws IOException { 11 | f = new BufferedReader(new InputStreamReader(System.in)); 12 | int T = parseInt(f.readLine()); 13 | StringTokenizer st; 14 | int w,i,N, j, val, weight; 15 | int[] dp; 16 | while(T-- > 0) 17 | { 18 | st = new StringTokenizer(f.readLine()); 19 | w = - parseInt(st.nextToken()) + parseInt(st.nextToken()); 20 | dp = new int[w+1]; 21 | Arrays.fill(dp, INF); 22 | dp[0] = 0; 23 | N = parseInt(f.readLine()); 24 | for(i = 0; i < N; i++) 25 | { 26 | st = new StringTokenizer(f.readLine()); 27 | val = parseInt(st.nextToken()); 28 | weight = parseInt(st.nextToken()); 29 | for(j = weight; j <= w; j++) 30 | if(dp[j] > dp[j-weight]+val) 31 | dp[j] = dp[j-weight]+val; 32 | } 33 | 34 | if(dp[w] == INF) 35 | System.out.println("This is impossible."); 36 | else 37 | System.out.format("The minimum amount of money in the piggy-bank is %d.%n", dp[w]); 38 | System.out.flush(); 39 | } 40 | System.exit(0); 41 | } 42 | } -------------------------------------------------------------------------------- /pir.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Double.parseDouble; 4 | import static java.lang.Math.sqrt; 5 | 6 | public class pir 7 | { 8 | private static BufferedReader f; 9 | 10 | public static void main(String[] args) throws IOException { 11 | f = new BufferedReader(new InputStreamReader(System.in)); 12 | int T = Integer.parseInt(f.readLine()); 13 | StringTokenizer st; 14 | double U,V,W,u,v,w,a,b,c,d,X,Y,Z,x,y,z, volume; 15 | while(T-->0) 16 | { 17 | st=new StringTokenizer(f.readLine()); 18 | U=parseDouble(st.nextToken()); 19 | V=parseDouble(st.nextToken()); 20 | w=parseDouble(st.nextToken()); 21 | W=parseDouble(st.nextToken()); 22 | v=parseDouble(st.nextToken()); 23 | u=parseDouble(st.nextToken()); 24 | X=(w-U+v)*(U+v+w); 25 | x=(U-v+w)*(v-w+U); 26 | Y=(u-V+w)*(V+w+u); 27 | y=(V-w+u)*(w-u+V); 28 | Z=(v-W+u)*(W+u+v); 29 | z=(W-u+v)*(u-v+W); 30 | a=sqrt(x*Y*Z); 31 | b=sqrt(y*Z*X); 32 | c=sqrt(z*X*Y); 33 | d=sqrt(x*y*z); 34 | volume=(sqrt((-a+b+c+d)*(a-b+c+d)*(a+b-c+d)*(a+b+c-d))/(192*u*v*w)); 35 | System.out.format("%.4f%n",volume); 36 | System.out.flush(); 37 | } 38 | System.exit(0); 39 | } 40 | } -------------------------------------------------------------------------------- /polynom.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class polynom 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int T = in.nextInt (); 14 | while (T-- > 0) { 15 | int N = in.nextInt (); 16 | long [][] deg = new long [4][N]; 17 | for (int i = 0; i < N; i++) 18 | deg [0][i] = in.nextInt (); 19 | for (int i = 1; i < 4; i++) 20 | for (int j = 0; j < N - i; j++) 21 | deg [i][j] = deg [i - 1][j + 1] - deg [i - 1][j]; 22 | boolean flag = false; 23 | for (int i = 1; i < N - 3; i++) { 24 | if (deg [3][i] != deg [3][0]) { 25 | out.println ("NO"); 26 | flag = true; 27 | break; 28 | } 29 | } 30 | if (!flag) out.println ("YES"); 31 | } 32 | } 33 | } 34 | 35 | /** Faster input **/ 36 | class Reader { 37 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 38 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 39 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 40 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 41 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 42 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 43 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 44 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 45 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 46 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 47 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 48 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 49 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 50 | public void close()throws IOException{if(din==null) return;din.close();} 51 | } -------------------------------------------------------------------------------- /portalun.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class portalun { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | private static int [] eadj, eprev, elast; 9 | private static int eidx; 10 | 11 | private static void addEdge (int a, int b) { 12 | if (b > a) {a ^= b; b ^= a; a ^= b;} 13 | eadj [eidx] = b; eprev [eidx] = elast [a]; elast [a] = eidx++; 14 | } 15 | 16 | public static void main(String[] args) throws IOException { 17 | in = new BufferedReader(new InputStreamReader (System.in)); 18 | out = new PrintWriter(System.out, true); 19 | 20 | String line = in.readLine(); 21 | while (line != null) { 22 | StringTokenizer st = new StringTokenizer (line); 23 | int N = Integer.parseInt (st.nextToken()), M = Integer.parseInt (st.nextToken ()); 24 | 25 | eadj = new int [M]; 26 | eprev = new int [M]; 27 | elast = new int [N]; 28 | eidx = 0; 29 | Arrays.fill (elast, -1); 30 | 31 | for (int i = 0; i < M; i++) { 32 | st = new StringTokenizer (in.readLine ()); 33 | int a = Integer.parseInt (st.nextToken ()), 34 | b = Integer.parseInt (st.nextToken ()); 35 | addEdge (a, b); 36 | } 37 | 38 | int [] nim = new int [N]; 39 | for (int i = 0; i < N; i++) { 40 | boolean [] seen = new boolean [N]; 41 | for (int e = elast [i]; e != -1; e = eprev [e]) { 42 | seen [nim [eadj [e]]] = true; 43 | } 44 | int k = 0; 45 | for(; seen [k]; k++); 46 | nim [i] = k; 47 | } 48 | 49 | int K = Integer.parseInt (in.readLine ()); 50 | int xor = 0; 51 | while (K-- > 0) 52 | xor ^= nim [Integer.parseInt (in.readLine ())]; 53 | 54 | out.println (xor == 0 ? "I lose" : "I win"); 55 | line = in.readLine(); 56 | } 57 | out.close(); 58 | System.exit(0); 59 | } 60 | } -------------------------------------------------------------------------------- /prhyme.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class prhyme 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new BufferedReader (new InputStreamReader (System.in)); 11 | out = new PrintWriter (System.out, true); 12 | String line = in.readLine (); 13 | Node root = new Node (); 14 | while (line.length () > 0) { 15 | root.add (line); 16 | line = in.readLine (); 17 | } 18 | while ( (line=in.readLine()) != null) 19 | out.println (root.findRhyme (line)); 20 | 21 | System.exit (0); 22 | } 23 | static class Node { 24 | public Node [] next; 25 | public ArrayList wordList; 26 | public boolean sorted; 27 | public int count; 28 | 29 | public Node () { 30 | next = new Node [26]; 31 | wordList = new ArrayList (250000); 32 | sorted = true; 33 | count = 0; 34 | } 35 | public void add (String word) { 36 | count++; 37 | if (word.length () == 0) return; 38 | int idx = word.charAt (word.length () - 1) - 'a'; 39 | if (next [idx] == null) next [idx] = new Node (); 40 | wordList.add (word); sorted = false; 41 | next [idx].add (word.substring (0, word.length () - 1)); 42 | } 43 | public String findRhyme (String word) { 44 | if (count == 0) return null; 45 | int last = word.charAt (word.length () - 1) - 'a'; 46 | if (next [last].count > 1) return next [last].findRhyme (word.substring (0, word.length () - 1)) + (char)(last + 'a'); 47 | if (!sorted) { Collections.sort (wordList); sorted = true;} 48 | String temp = wordList.get (0); 49 | if (temp.equals (word)) temp = wordList.get (1); 50 | return temp; 51 | } 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /prime1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class prime1 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | int i, m, n, j, k, p, s; 11 | int[] primes = new int[4000]; 12 | j = 0; 13 | primes[j++] = 2; 14 | boolean prime; 15 | for(i = 3; i <= Math.sqrt(1000000000); i+=2) 16 | { 17 | prime = true; 18 | for(k = 0; k < j && primes[k] <= Math.sqrt(i); k++) 19 | if(i % primes[k] == 0) 20 | { 21 | prime = false; 22 | break; 23 | } 24 | if(prime) primes[j++] = i; 25 | } 26 | 27 | 28 | f = new BufferedReader(new InputStreamReader(System.in)); 29 | 30 | int T = parseInt(f.readLine()); 31 | boolean[] isPrime = new boolean[100001]; 32 | while(T-->0) 33 | { 34 | StringTokenizer st = new StringTokenizer(f.readLine()); 35 | m = parseInt(st.nextToken()); 36 | n = parseInt(st.nextToken()); 37 | if(m < 2) m = 2; 38 | Arrays.fill(isPrime, true); 39 | for(i = 0; i < j; i++) 40 | { 41 | p = primes[i]; 42 | s = 0; 43 | if(p >= m) s = p<<1; 44 | else s = m + ((p-m%p)%p); 45 | for(k = s; k <= n; k+=p) isPrime[k-m] = false; 46 | } 47 | 48 | for(i = m; i <= n; i++) 49 | if(isPrime[i-m]) System.out.println(i); 50 | 51 | System.out.println(); 52 | } 53 | 54 | System.exit(0); 55 | } 56 | } -------------------------------------------------------------------------------- /probor.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class probor 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | double p = 1 - in.nextDouble (); 14 | 15 | int N = in.nextInt (); 16 | 17 | double [] exp = new double [32]; 18 | int c, idx; 19 | for (idx = 0, c = in.nextInt (); c > 0; idx++, c >>= 1) 20 | if ((c & 1) == 1) 21 | exp [idx] = 1; 22 | 23 | for (int i = 1; i < N; i++) { 24 | for (idx = 0, c = in.nextInt (); idx < 32; idx++, c >>= 1) { 25 | if ((c & 1) == 1) 26 | exp [idx] = exp [idx] + p * (1 - exp [idx]); 27 | else 28 | exp [idx] = p * exp [idx]; 29 | } 30 | } 31 | 32 | double ans = 0; 33 | for (idx = 0; idx < 32; idx++) 34 | ans += exp [idx] * (1 << idx); 35 | 36 | out.printf ("%.2f\n", ans); 37 | } 38 | } 39 | 40 | /** Faster input **/ 41 | class Reader { 42 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 43 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 44 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 45 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 46 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 47 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 48 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 49 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 50 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 51 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 52 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 53 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 54 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 55 | public void close()throws IOException{if(din==null) return;din.close();} 56 | } -------------------------------------------------------------------------------- /quality.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class quality 6 | { 7 | private static BufferedReader in; 8 | 9 | public static void main(String[] args) throws IOException { 10 | in = new BufferedReader(new InputStreamReader(System.in)); 11 | StringTokenizer st; 12 | int i = 1; 13 | int a, b, c, d, e, f; 14 | int time, solve; 15 | String line = in.readLine(); 16 | while(line != null) 17 | { 18 | st = new StringTokenizer(line); 19 | a = parseInt(st.nextToken()); 20 | b = parseInt(st.nextToken()); 21 | c = parseInt(st.nextToken()); 22 | d = parseInt(st.nextToken()); 23 | e = parseInt(st.nextToken()); 24 | f = parseInt(st.nextToken()); 25 | time = solve = 0; 26 | if(a != 0) 27 | { 28 | time += (a+(d-1)*20*60); 29 | solve++; 30 | } 31 | if(b != 0) 32 | { 33 | time += (b+(e-1)*20*60); 34 | solve++; 35 | } 36 | if(c != 0) 37 | { 38 | time += (c+(f-1)*20*60); 39 | solve++; 40 | } 41 | 42 | System.out.format("team %d: %d, %d%n",i++,solve,time); 43 | System.out.flush(); 44 | 45 | line = in.readLine(); 46 | } 47 | System.exit(0); 48 | } 49 | } -------------------------------------------------------------------------------- /regpolyg.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class regpolyg { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | private static double EPS = 1e-6; 9 | 10 | public static void main (String [] args) throws IOException { 11 | in = new BufferedReader (new InputStreamReader (System.in)); 12 | out = new PrintWriter (System.out, true); 13 | 14 | while (true) { 15 | String line = in.readLine(); 16 | if (line.equals ("END")) break; 17 | 18 | String [] s = line.split (" "); 19 | double x1 = Double.parseDouble (s [0]); 20 | double y1 = Double.parseDouble (s [1]); 21 | 22 | line = in.readLine(); 23 | s = line.split (" "); 24 | double x2 = Double.parseDouble (s [0]); 25 | double y2 = Double.parseDouble (s [1]); 26 | 27 | line = in.readLine(); 28 | s = line.split (" "); 29 | double x3 = Double.parseDouble (s [0]); 30 | double y3 = Double.parseDouble (s [1]); 31 | 32 | 33 | double a = Math.sqrt ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 34 | double b = Math.sqrt ((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); 35 | double c = Math.sqrt ((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); 36 | 37 | double angle1 = Math.acos ((a * a + b * b - c * c) / (2 * a * b)) / Math.PI; 38 | double angle2 = Math.acos ((a * a + c * c - b * b) / (2 * a * c)) / Math.PI; 39 | double angle3 = Math.acos ((b * b + c * c - a * a) / (2 * b * c)) / Math.PI; 40 | 41 | for (int i = 3; i <= 1000; i++) { 42 | if (eq ((double)((int)(angle1 * i + EPS)) / i, angle1) && 43 | eq ((double)((int)(angle2 * i + EPS)) / i, angle2) && 44 | eq ((double)((int)(angle3 * i + EPS)) / i, angle3)) { 45 | out.println (i); 46 | break; 47 | } 48 | } 49 | } 50 | 51 | out.close(); 52 | System.exit(0); 53 | } 54 | 55 | private static boolean eq (double a, double b) { 56 | return Math.abs (a - b) < EPS; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /resn04.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class resn04 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int t = parseInt(f.readLine()); 12 | int n, moves, i; StringTokenizer st; 13 | while(t-->0) 14 | { 15 | n = parseInt(f.readLine()); 16 | st = new StringTokenizer(f.readLine()); 17 | moves = 0; 18 | for(i = 1; i <= n; i++) 19 | moves += parseInt(st.nextToken())/i; 20 | 21 | if((moves&1)==0) System.out.println("BOB"); 22 | else System.out.println("ALICE"); 23 | System.out.flush(); 24 | } 25 | System.exit(0); 26 | } 27 | } -------------------------------------------------------------------------------- /robotgri.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class robotgri { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main (String [] args) throws IOException { 9 | in = new BufferedReader (new InputStreamReader (System.in)); 10 | out = new PrintWriter (System.out, true); 11 | N = Integer.parseInt (in.readLine ()); 12 | grid = new char [N][N]; 13 | for (int i = 0; i < N; i++) 14 | grid [i] = in.readLine().toCharArray (); 15 | vis = new boolean [N][N]; 16 | flood (0, 0); 17 | if (!vis [N - 1][N - 1]) { 18 | out.println ("INCONCEIVABLE"); 19 | out.close(); 20 | System.exit(0); 21 | } 22 | 23 | long [][] ways = new long [N][N]; 24 | boolean [][] ok = new boolean [N][N]; 25 | ways [N - 1][N - 1] = 1; 26 | ok [N - 1][N - 1] = true; 27 | long mod = (1l << 31) - 1; 28 | for (int i = N - 1; i >= 0; i--) { 29 | for (int j = N - 1; j >= 0; j--) { 30 | if (grid [i][j] == '#') continue; 31 | if (i < N - 1) { 32 | ways [i][j] = (ways [i][j] + ways [i + 1][j]) % mod; 33 | ok [i][j] |= ok [i + 1][j]; 34 | } 35 | if (j < N - 1) { 36 | ways [i][j] = (ways [i][j] + ways [i][j + 1]) % mod; 37 | ok [i][j] |= ok [i][j + 1]; 38 | } 39 | } 40 | } 41 | 42 | out.println (ok [0][0] ? ways [0][0] : "THE GAME IS A LIE"); 43 | out.close(); 44 | System.exit(0); 45 | 46 | 47 | } 48 | 49 | private static boolean [][] vis; 50 | private static int N; 51 | private static char [][] grid; 52 | 53 | private static void flood (int x, int y) { 54 | if (x < 0 || x >= N || y < 0 || y >= N || vis [x][y] || grid [x][y] == '#') return; 55 | 56 | vis [x][y] = true; 57 | flood (x - 1, y); 58 | flood (x + 1, y); 59 | flood (x, y - 1); 60 | flood (x, y + 1); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /rock.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class rock 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int t = in.nextInt (); 13 | while (t-- > 0) { 14 | int n = in.nextInt (); 15 | int [] sum = new int [n + 1]; 16 | for (int i = 1; i <= n; i++) 17 | sum [i] = sum [i - 1] + (in.nextChar () == '1' ? 1 : 0); 18 | int [] dp = new int [n + 1]; 19 | for (int i = 1; i <= n; i++) { 20 | dp [i] = dp [i - 1]; 21 | for (int j = 1; j <= i; j++) 22 | if (sum [i] - sum [i - j] > (j >> 1)) 23 | if (dp [i - j] + j > dp [i]) 24 | dp [i] = dp [i - j] + j; 25 | } 26 | out.println (dp [n]); 27 | } 28 | } 29 | } 30 | 31 | /** Faster input **/ 32 | class Reader { 33 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 34 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 35 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 36 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 37 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 38 | public char nextChar()throws IOException{byte c=read();while(c<=' ')c=read();return (char)c;} 39 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 40 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 41 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 42 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 43 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 44 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 45 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 46 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 47 | public void close()throws IOException{if(din==null) return;din.close();} 48 | } -------------------------------------------------------------------------------- /sqrt2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.math.*; 4 | 5 | public class sqrt2 6 | { 7 | private static PrintWriter out; 8 | 9 | public static void main(String[] args) throws IOException { 10 | long start = System.currentTimeMillis(); 11 | out = new PrintWriter(System.out); 12 | BigDecimal two = new BigDecimal("2"); 13 | BigDecimal guess = new BigDecimal("1.414"); 14 | BigDecimal val = two; 15 | 16 | BigDecimal next; 17 | int cur = guess.scale(); 18 | int max = 40000; 19 | while((cur*=2) <= 2000000) 20 | { 21 | if(cur > max) cur = max; 22 | next = val.divide(guess,cur,BigDecimal.ROUND_DOWN); 23 | guess = (next.add(guess)).divide(two,cur,BigDecimal.ROUND_DOWN); 24 | if(cur == max) break; 25 | } 26 | out.println(guess); 27 | out.println("Time: " + (System.currentTimeMillis()-start)/1000.0 + "s"); 28 | } 29 | 30 | public static String root(int n) { 31 | String s = "" + n; 32 | if(s.length() == 1) 33 | s = "0" + s; 34 | BigInteger p = BigInteger.ZERO, r = BigInteger.ZERO, twenty = new BigInteger("" + 20);; 35 | int y,x; 36 | for(int i = 0; i < 1000; i++) { 37 | try { 38 | y = Integer.parseInt(s.substring(i,i+2)); 39 | } 40 | catch(StringIndexOutOfBoundsException e) { 41 | y = 0; 42 | } 43 | r = r.multiply(new BigInteger("" + 100)); 44 | r = r.add(new BigInteger("" + y)); 45 | for(x = 9; x >= 0; x--) { 46 | BigInteger test = p.multiply(new BigInteger("" + 20)); 47 | test = test.add(new BigInteger("" + x)); 48 | test = test.multiply(new BigInteger("" + x)); 49 | if(test.compareTo(r) <= 0){ 50 | r = r.subtract(test); 51 | break; 52 | } 53 | } 54 | p = new BigInteger(""+p+x); 55 | if(r.compareTo(BigInteger.ZERO) == 0) 56 | return ""; 57 | } 58 | return ""+p; 59 | } 60 | } -------------------------------------------------------------------------------- /stamps.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class stamps 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | 12 | int n = parseInt(f.readLine()); 13 | int a, b, i, j = 1;; StringTokenizer st; 14 | PriorityQueue pq; 15 | while(n-- > 0) 16 | { 17 | st = new StringTokenizer(f.readLine()); 18 | a = parseInt(st.nextToken()); 19 | b = parseInt(st.nextToken()); 20 | st = new StringTokenizer(f.readLine()); 21 | pq = new PriorityQueue(); 22 | for(i = 0; i < b; i++) 23 | pq.add(-parseInt(st.nextToken())); 24 | while(a > 0 && pq.size() > 0) 25 | a += pq.poll(); 26 | System.out.format("Scenario #%d:%n", j++); 27 | if(a > 0) System.out.println("impossible"); 28 | else System.out.println( (b-pq.size()) ); 29 | System.out.println(); 30 | System.out.flush(); 31 | } 32 | System.exit(0); 33 | } 34 | } -------------------------------------------------------------------------------- /stargate.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class stargate 5 | { 6 | private static BufferedReader in; 7 | private static PrintWriter out; 8 | 9 | private static int [] ptr, size; 10 | 11 | public static void main (String [] args) throws IOException { 12 | in = new BufferedReader (new InputStreamReader (System.in)); 13 | out = new PrintWriter (System.out, true); 14 | 15 | String [] st; 16 | int [] commands = new int [6]; 17 | ptr = new int [6000000]; 18 | size = new int [6000000]; 19 | String line = in.readLine (); 20 | while (line != null) { 21 | st = line.split (" "); 22 | char c = st [0].charAt (0); 23 | commands = new int [] {1, 1, 1, 1, 0}; 24 | switch (c) { 25 | case 'd' : case 'D' : 26 | int N = Integer.parseInt (st [1]); 27 | for (int i = 0; i < N; i++) { 28 | ptr [i] = i; 29 | size [i] = 1; 30 | } 31 | break; 32 | case 'c' : case 'C' : 33 | for (int i = 1; i < st.length; i++) 34 | commands [i - 1] = Integer.parseInt (st [i]); 35 | commands [0]--; commands [1]--; 36 | connect (commands); 37 | break; 38 | case 'q' : case 'Q' : 39 | for (int i = 1; i < st.length; i++) 40 | commands [i - 1] = Integer.parseInt (st [i]); 41 | commands [0]--; commands [1]--; 42 | int [] a = query (commands); 43 | out.printf ("%d %d\n", a [0], a [1]); 44 | break; 45 | } 46 | 47 | line = in.readLine (); 48 | } 49 | } 50 | 51 | private static int [] query (int [] x) { 52 | int [] ans = new int [2]; 53 | for (int i = 0; i < x [2]; i++) { 54 | if (query (x [0] + i * x [4], x [1] + i * x [3])) 55 | ans [0]++; 56 | else ans [1]++; 57 | } 58 | return ans; 59 | } 60 | 61 | private static void connect (int [] x) { 62 | for (int i = 0; i < x [2]; i++) 63 | join (x [0] + i * x [4], x [1] + i * x [3]); 64 | } 65 | 66 | private static int find (int a) { 67 | int p1 = ptr [a]; 68 | while (p1 != ptr [p1]) ptr [a] = p1 = ptr [p1]; 69 | return ptr [a] = p1; 70 | } 71 | 72 | private static boolean query (int a, int b) { 73 | return find (a) == find (b); 74 | } 75 | 76 | private static void join (int a, int b) { 77 | int p1 = find (a), p2 = find (b); 78 | if (p1 == p2) return; 79 | if (size [p1] < size [p2]) { 80 | p1 ^= p2; p2 ^= p1; p1 ^= p2; 81 | } 82 | size [p1] += size [p2]; 83 | ptr [p2] = p1; 84 | } 85 | } -------------------------------------------------------------------------------- /stone.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class stone 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = parseInt(f.readLine()); 12 | int n, i; 13 | double [] x, y; 14 | StringTokenizer st; 15 | while(T-- > 0) 16 | { 17 | n = parseInt(f.readLine()); 18 | x = new double [n + 1]; 19 | y = new double [n + 1]; 20 | for(i = 0; i < n; i++) 21 | { 22 | st = new StringTokenizer(f.readLine()); 23 | x [i] = (double)parseInt(st.nextToken()); 24 | y [i] = (double)parseInt(st.nextToken()); 25 | } 26 | x[n] = x[0]; y[n] = y[0]; 27 | double A = 0; 28 | for (i = 0; i < n; i++) 29 | A += x[i] * y[i + 1] - x[i + 1] * y[i]; 30 | A /= 2.; 31 | double cx = 0, cy = 0; 32 | for (i = 0; i < n; i++) { 33 | cx += (x[i] + x[i + 1]) * (x[i] * y[i + 1] - x[i + 1] * y[i]); 34 | cy += (y[i] + y[i + 1]) * (x[i] * y[i + 1] - x[i + 1] * y[i]); 35 | } 36 | cx /= 6. * A; cy /= 6. * A; 37 | System.out.format("%.2f %.2f%n", cx, cy); 38 | System.out.flush(); 39 | } 40 | System.exit(0); 41 | } 42 | } -------------------------------------------------------------------------------- /stpar.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class stpar 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | 12 | StringTokenizer st; 13 | int index, i, a; 14 | Stack s; 15 | for(;;) 16 | { 17 | int n = parseInt(f.readLine()); 18 | if(n == 0) break; 19 | st = new StringTokenizer(f.readLine()); 20 | index = 1; 21 | s = new Stack(); 22 | for(i = 0; i < n; i++) 23 | { 24 | a = parseInt(st.nextToken()); 25 | 26 | if(a == index) {index++; continue; } 27 | 28 | while(s.size() > 0 && s.peek() == index) 29 | { s.pop(); index++; } 30 | 31 | if(s.size() > 0 && s.peek() < a) break; 32 | 33 | s.push(a); 34 | } 35 | 36 | if(i == n) System.out.println("yes"); 37 | else System.out.println("no"); 38 | System.out.flush(); 39 | } 40 | System.exit(0); 41 | } 42 | } -------------------------------------------------------------------------------- /sublex.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class sublex { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main(String[] args) throws IOException { 9 | in = new BufferedReader(new InputStreamReader (System.in)); 10 | out = new PrintWriter(System.out, true); 11 | 12 | char [] s = in.readLine().toCharArray(); 13 | int N = s.length; 14 | int [] arr = suffix_array (s); 15 | int [] arr3 = new int [N]; 16 | for (int i = 0; i < N; i++) 17 | arr3 [arr [i]] = i; 18 | int [] arr2 = lcp (s, arr, arr3); 19 | 20 | int [] pcount = new int [N]; 21 | pcount [0] = N - arr3 [0]; 22 | for (int i = 1; i < N; i++) 23 | pcount [i] = pcount [i - 1] + (N - arr3 [i]) - arr2 [i]; 24 | 25 | int T = Integer.parseInt (in.readLine()); 26 | while (T-- > 0) { 27 | int idx = Integer.parseInt (in.readLine()); 28 | int lo = 0, hi = N - 1; 29 | while (lo < hi) { 30 | int mid = (lo + hi) >> 1; 31 | if (pcount [mid] < idx) lo = mid + 1; 32 | else hi = mid; 33 | } 34 | 35 | for (int i = arr3 [lo]; i <= N - (pcount [lo] - idx) - 1; i++) 36 | out.print (s [i]); 37 | out.println(); 38 | } 39 | out.close(); 40 | System.exit(0); 41 | } 42 | 43 | private static int [] suffix_array (char [] s) { 44 | int n = s.length; 45 | int [] id = new int [n]; 46 | for (int i = 0; i < n; i++) 47 | id[i] = s[i] - 'a'; 48 | 49 | for (int k = 1; k <= n; k <<= 1) { 50 | Triplet [] elem = new Triplet [n]; 51 | for (int i = 0; i < n; i++) 52 | elem[i] = new Triplet (id[i], (i + k < n) ? id[i + k] : -1, i); 53 | Arrays.sort (elem); 54 | int cur = -1; 55 | for (int i = 0; i < n; i++) { 56 | if (i == 0 57 | || !(elem[i].a == elem[i - 1].a && elem[i].b == elem[i - 1].b)) 58 | cur++; 59 | id[elem[i].c] = cur; 60 | } 61 | } 62 | 63 | return id; 64 | } 65 | 66 | private static int [] lcp (char [] A, int [] order, int [] rank) { 67 | int n = order.length; 68 | int [] height = new int [n]; 69 | 70 | int h = 0; 71 | for (int i = 0; i < n; i++) { 72 | if (order [i] > 0) { 73 | int j = rank [order [i] - 1]; 74 | while (i + h < n && j + h < n 75 | && A [i + h] == A [j + h]) h++; 76 | height [order [i]] = h; 77 | if (h > 0) h--; 78 | } 79 | } 80 | 81 | return height; 82 | } 83 | 84 | static class Triplet implements Comparable { 85 | public int a, b, c; 86 | 87 | public Triplet (int _a, int _b, int _c) { 88 | a = _a; 89 | b = _b; 90 | c = _c; 91 | } 92 | 93 | public int compareTo (Triplet other) { 94 | return (a == other.a) ? b - other.b : a - other.a; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /sumtrian.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class sumtrian 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | 12 | int N = parseInt(f.readLine()); 13 | 14 | while(N-- > 0) 15 | { 16 | int b = parseInt(f.readLine()); 17 | int[][] tri = new int[b][b]; 18 | for(int i = 0; i < b; i++) 19 | { 20 | StringTokenizer st = new StringTokenizer(f.readLine()); 21 | for(int j = 0; j <= i; j++) 22 | tri[j][i] = parseInt(st.nextToken()); 23 | } 24 | 25 | System.out.println(findMax(tri)); 26 | System.out.flush(); 27 | } 28 | System.exit(0); 29 | } 30 | 31 | public static int findMax(int[][] triangle) 32 | { 33 | for(int i = triangle.length-1; i > 0; i--) 34 | for(int j = 0; j < i; j++) 35 | triangle[j][i-1] += Math.max(triangle[j][i],triangle[j+1][i]); 36 | return triangle[0][0]; 37 | } 38 | } -------------------------------------------------------------------------------- /test.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class test 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int a = parseInt(f.readLine()); 12 | while(a != 42) 13 | { 14 | System.out.println(a); 15 | System.out.flush(); 16 | a = parseInt(f.readLine()); 17 | } 18 | 19 | System.exit(0); 20 | } 21 | } -------------------------------------------------------------------------------- /traverse.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class traverse 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int N = in.nextInt (); 14 | int [][] arr = new int [N][N]; 15 | for (int i = 0; i < N; i++) { 16 | String line = in.readLine (); 17 | for (int j = 0; j < N; j++) 18 | arr [i][j] = line.charAt (j) - '0'; 19 | } 20 | 21 | long [][] dp = new long [N][N]; 22 | dp [N - 1][N - 1] = 1; 23 | 24 | for (int i = N - 1; i >= 0; i--) { 25 | for (int j = N - 1; j >= 0; j--) { 26 | int cur = arr [i][j]; 27 | if (cur == 0) continue; 28 | if (j + cur < N) 29 | dp [i][j] += dp [i][j + cur]; 30 | if (i + cur < N) 31 | dp [i][j] += dp [i + cur][j]; 32 | } 33 | } 34 | 35 | out.println (dp [0][0]); 36 | } 37 | } 38 | 39 | /** Faster input **/ 40 | class Reader { 41 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 42 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 43 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 44 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 45 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 46 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 47 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 48 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 49 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 50 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 51 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 52 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 53 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 54 | public void close()throws IOException{if(din==null) return;din.close();} 55 | } -------------------------------------------------------------------------------- /trex.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class trex 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int t= in.nextInt (); 13 | while (t-- > 0) { 14 | int c = in.nextInt (), d = in.nextInt (), m = in.nextInt (); 15 | int [] best = new int [d + 1]; 16 | best [0] = c; 17 | for (int i = 1; i <= d; i++) { 18 | best [i] = 0; 19 | if (m < 1) break; 20 | int x = best [i - 1]; 21 | int count = 0; 22 | while (x > m) { 23 | x -= m; 24 | count += m - 1; 25 | if (count > best [i]) 26 | best [i] = count; 27 | count--; 28 | } 29 | count += x - 1; 30 | if (count > best [i]) 31 | best [i] = count; 32 | } 33 | out.println (best[d]); 34 | } 35 | } 36 | } 37 | 38 | /** Faster input **/ 39 | class Reader { 40 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 41 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 42 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 43 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 44 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 45 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 46 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 47 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 48 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 49 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 50 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 51 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 52 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 53 | public void close()throws IOException{if(din==null) return;din.close();} 54 | } -------------------------------------------------------------------------------- /tricount.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | public class tricount{ 3 | public static void main(String[] args) throws IOException { 4 | BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); 5 | int t = Integer.parseInt(f.readLine()); long n; 6 | while(t-->0){n=Long.parseLong(f.readLine()); 7 | System.out.println((n*(n+2)*(2*n+1)/8)); 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /trip2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class trip2 { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | public static void main(String[] args) throws IOException { 9 | in = new BufferedReader(new InputStreamReader (System.in)); 10 | out = new PrintWriter(System.out, true); 11 | 12 | String line; StringTokenizer st; 13 | 14 | while ((line = in.readLine()) != null) { 15 | st = new StringTokenizer (line); 16 | 17 | int N = Integer.parseInt (st.nextToken()), M = Integer.parseInt (st.nextToken ()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /trt.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class trt 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int n = parseInt(f.readLine()); 12 | int[] v = new int[n]; 13 | int i, j; 14 | for(i = 0; i < n; i++) v[i] = parseInt(f.readLine()); 15 | 16 | int[] prev = new int[n+4]; 17 | int[] dp = new int[n+4]; 18 | for(i = 1; i <= n; i++) 19 | { 20 | for(j = 0; j < i; j++) 21 | { 22 | dp[j] = Math.max(dp[j],prev[j]+i*v[n-i+j]); 23 | dp[j+1] = Math.max(dp[j+1],prev[j]+i*v[j]); 24 | } 25 | prev = dp; 26 | dp = new int[n+4]; 27 | } 28 | int max = 0; 29 | for(int k:prev) max = Math.max(max,k); 30 | System.out.println(max); 31 | System.out.flush(); 32 | System.exit(0); 33 | } 34 | } -------------------------------------------------------------------------------- /ttreat.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class ttreat 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | while (true) { 13 | int m = in.nextInt (), n = in.nextInt (); 14 | if (m == 0 && n == 0) break; 15 | int [] start = new int [m], finish = new int [m]; 16 | for (int i = 0; i < m; i++) { 17 | in.nextInt (); in.nextInt (); 18 | start [i] = in.nextInt (); 19 | finish [i] = in.nextInt () + start [i]; 20 | } 21 | for (int i = 0; i < n; i++) { 22 | int count = 0; 23 | int s = in.nextInt (), f = in.nextInt () + s; 24 | for (int j = 0; j < m; j++) { 25 | if ((f > start [j] && f <= finish [j]) || 26 | (s >= start [j] && s < finish [j]) || 27 | (finish [j] > s && finish [j] <= f) || 28 | (start [j] >= s && start [j] < f)) 29 | count++; 30 | } 31 | out.println (count); 32 | } 33 | } 34 | } 35 | } 36 | 37 | /** Faster input **/ 38 | class Reader { 39 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 40 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 41 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 42 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 43 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 44 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 45 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 46 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 47 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 48 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 49 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 50 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 51 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 52 | public void close()throws IOException{if(din==null) return;din.close();} 53 | } -------------------------------------------------------------------------------- /wingold.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class wingold 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | int T = in.nextInt (); 13 | 14 | while (T-- > 0) { 15 | double p = in.nextDouble (); 16 | int N = in.nextInt (), M = in.nextInt (); 17 | if ((p == 1 || p == 0)) { 18 | out.println (N == 1 ? "1.0000" : "0.0000"); 19 | continue; 20 | } 21 | double [][] dp = new double [N + 1][M]; 22 | dp [N][0] = 1; 23 | for (int i = 1; i < M; i++) { 24 | double start = 1; 25 | for (int k = 1; k <= N; k++) { 26 | start *= (1 - p); 27 | double q = start; 28 | for (int j = 0; j <= k; j++) { 29 | dp [j][i] += q * dp [k][i - 1]; 30 | q *= p / (1 - p); 31 | } 32 | } 33 | } 34 | 35 | double ans = 0; 36 | for (int i = 0; i < M; i++) { 37 | ans += dp [1][i]; 38 | } 39 | 40 | out.printf ("%.4f\n", ans); 41 | } 42 | } 43 | } 44 | 45 | /** Faster input **/ 46 | class Reader { 47 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 48 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 49 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 50 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 51 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 52 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 53 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 54 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 55 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 56 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 57 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 58 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 59 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 60 | public void close()throws IOException{if(din==null) return;din.close();} 61 | } -------------------------------------------------------------------------------- /wrong.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class wrong { 5 | private static BufferedReader in; 6 | private static PrintWriter out; 7 | 8 | static class Pair { 9 | public int x, y; 10 | public Pair (int x, int y) { 11 | this.x = x; 12 | this.y = y; 13 | } 14 | } 15 | 16 | public static HashSet mp; 17 | public static int xpos, ypos, dir, cxpos, cypos, cdir; 18 | 19 | public static void main(String[] args) throws IOException { 20 | in = new BufferedReader(new InputStreamReader (System.in)); 21 | out = new PrintWriter(System.out, true); 22 | 23 | char [] command = new String (in.readLine()).toCharArray(); 24 | xpos = 0; ypos = 0; dir = 0; 25 | for (char c : command) { 26 | switch (c) { 27 | case 'F': switch (dir) { 28 | case 0:ypos++;break; 29 | case 1:xpos++;break; 30 | case 2:ypos--;break; 31 | case 3:xpos--;break; 32 | } 33 | break; 34 | case 'R': 35 | dir = (dir+1)%4; 36 | case 'L': 37 | dir = (dir+3)%4; 38 | } 39 | } 40 | 41 | mp = new HashSet (); 42 | cxpos = 0; cypos = 0; cdir = 0; 43 | for (char c : command) { 44 | switch (c) { 45 | case 'F': mp.add(R()); mp.add(L()); 46 | switch (dir) { 47 | case 0:cypos++;ypos--;break; 48 | case 1:cxpos++;xpos--;break; 49 | case 2:cypos--;ypos++;break; 50 | case 3:cxpos--;xpos++;break; 51 | } break; 52 | case 'R': mp.add(F()); mp.add(L()); 53 | cdir = (cdir+1)%4; dir = (dir+3)%4; break; 54 | case 'L': mp.add(F()); mp.add(R()); 55 | cdir = (cdir+3)%4; dir = (dir+1)%4; break; 56 | } 57 | } 58 | 59 | out.println (mp.size()); 60 | out.close(); 61 | System.exit(0); 62 | } 63 | 64 | private static Pair F () { 65 | return null; 66 | } 67 | 68 | private static Pair R () { 69 | return null; 70 | } 71 | 72 | private static Pair L () { 73 | return null; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /xoinc.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | import static java.lang.Math.max; 5 | 6 | public class xoinc 7 | { 8 | private static BufferedReader f; 9 | private static PrintWriter out; 10 | private static int[] a, sum; 11 | private static int[][] dp; 12 | 13 | public static void main(String[] args) throws IOException { 14 | f = new BufferedReader(new InputStreamReader(System.in)); 15 | 16 | int N = parseInt(f.readLine()); 17 | sum = new int[N+1]; 18 | a = new int[N+1]; 19 | int i, j; 20 | for(i = 1; i <= N; i++) a[N-i+1] = parseInt(f.readLine()); 21 | 22 | dp = new int[N+1][N+1]; 23 | 24 | for(i = 1; i <= N; i++) 25 | { 26 | sum[i] = sum[i-1] + a[i]; 27 | for(j = 1; j <= N; j++) 28 | { 29 | dp[i][j] = max(dp[i][j], dp[i][j-1]); 30 | if(i >= 2*j-1) 31 | dp[i][j] = max(dp[i][j], sum[i] - dp[i-(2*j-1)][2*j-1]); 32 | if(i >= 2*j) 33 | dp[i][j] = max(dp[i][j], sum[i] - dp[i-2*j][2*j]); 34 | } 35 | } 36 | 37 | System.out.println(dp[N][1]); 38 | System.out.flush(); 39 | System.exit(0); 40 | } 41 | } -------------------------------------------------------------------------------- /ykh.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.Integer.parseInt; 4 | 5 | public class ykh 6 | { 7 | private static BufferedReader f; 8 | 9 | public static void main(String[] args) throws IOException { 10 | f = new BufferedReader(new InputStreamReader(System.in)); 11 | int t = parseInt(f.readLine()); 12 | int B, c, i; StringTokenizer st; 13 | int[] a, b; double[] o; 14 | double best; 15 | String line; 16 | while(t-->0) 17 | { 18 | line = f.readLine(); 19 | while(line.length() < 1) line = f.readLine(); 20 | st = new StringTokenizer(line); 21 | B = parseInt(st.nextToken()); 22 | c = parseInt(st.nextToken()); 23 | best = 0.0; 24 | a = new int[c]; b = new int[c]; 25 | o = new double[c]; 26 | for(i = 0; i < c; i++) 27 | { 28 | st = new StringTokenizer(f.readLine()); 29 | a[i] = parseInt(st.nextToken()); 30 | b[i] = parseInt(st.nextToken()); 31 | o[i] = (double)a[i]/(2.0*b[i]); 32 | } 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /youtube.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class youtube 5 | { 6 | private static Reader in; 7 | private static PrintWriter out; 8 | 9 | public static void main (String [] args) throws IOException { 10 | in = new Reader (); 11 | out = new PrintWriter (System.out, true); 12 | 13 | int N = in.nextInt (), K = in.nextInt (), M = in.nextInt () - 1; 14 | 15 | int log = Integer.numberOfTrailingZeros (Integer.highestOneBit (M)) + 1; 16 | 17 | int [][] next = new int [K][log + 1]; 18 | 19 | int [] start = new int [N]; 20 | for (int i = 0; i < N; i++) 21 | start [i] = in.nextInt () - 1; 22 | 23 | for (int i = 0; i < K; i++) 24 | next [i][0] = in.nextInt () - 1; 25 | 26 | for (int i = 1; 1 << i <= M; i++) 27 | for (int j = 0; j < K; j++) 28 | next [j][i] = next [next [j][i - 1]][i - 1]; 29 | 30 | for (int i = 0; 1 << i <= M; i++) 31 | if (((1 << i) & M) > 0) 32 | for (int j = 0; j < N; j++) 33 | start [j] = next [start [j]][i]; 34 | 35 | out.print (start [0] + 1); 36 | for (int i = 1; i < N; i++) 37 | out.print (" " + (start [i] + 1)); 38 | out.println (); 39 | out.close (); 40 | System.exit (0); 41 | } 42 | } 43 | 44 | /** Faster input **/ 45 | class Reader { 46 | final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead; 47 | public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 48 | public Reader(String file_name)throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;} 49 | public String readLine()throws IOException{byte[] buf=new byte[1024];int cnt=0,c; 50 | while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);} 51 | public int nextInt()throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 52 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 53 | public long nextLong()throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-'); 54 | if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;} 55 | public double nextDouble()throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9'); 56 | if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;} 57 | private void fillBuffer()throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;} 58 | private byte read()throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];} 59 | public void close()throws IOException{if(din==null) return;din.close();} 60 | } --------------------------------------------------------------------------------