Author Archives: toad, die

Poj Solution 1188

http://poj.org/problem?id=1188 /* @author: */ import java.util.Scanner; import java.util.Arrays; public class Main{ static int mins=Integer.MIN_VALUE; static int maxs=Integer.MAX_VALUE; static int min(int a,int b) { return a<=b?a:b; } static int max(int a,int b) { return a>=b?a:b; } public static void main(String args[]) … Continue reading

Posted in poj | Leave a comment

Poj Solution 1187

http://poj.org/problem?id=1187 //* @author: import java.util.*; public class Main { static int s[][][][]=new int[31][11][11][11]; static int get( int d, int l1, int l2, int l3 ) { int t, j, k, i, h; if( l1 == 0 && l2 == 0 … Continue reading

Posted in poj | Leave a comment

Poj Solution 1186

http://poj.org/problem?id=1186 //* @author: import java.math.BigInteger; import java.util.Arrays; import java.util.Comparator; import java.util.Hashtable; import java.util.Scanner; public class Main { static int[]k=new int[6]; static int[]p=new int[6]; static int n,m,mark; static int total; static Hashtable< Long,Integer>hash=new Hashtable< Long,Integer>(); public static void main(String[] args) { … Continue reading

Posted in poj | Leave a comment

Poj Solution 1185

http://poj.org/problem?id=1185 //* @author: /*��Ŀ������һ��n*m�ķ����з����ڱ�Ҫ�����ڣ���û���ڱ�������ܷŶ��� *��ʼ��Ϊû���뵽�õķ�������������һ�Σ�tle… *֪���й���״̬ѹ����㷨���ɲ�̫�����һֱ����û����쿴��һ��״̬ѹ��ģ��㷨 *������д����4���������ڣ�c.. *״̬ѹ����λ4��ʾ״̬������λ�����ʡʱ�䣨���Ǻ�����ᣩ *ö��״̬������״̬ת�ƴӶ�õ����Ž⣬��Ϊ״̬ѹ��DP… */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; class cin { static BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static int leave=0; static int nextInt()throws IOException { return Integer.parseInt(next()); } … Continue reading

Posted in poj | Leave a comment

Poj Solution 1183

http://poj.org/problem?id=1183 //* @author popop0p0popo import java.util.*; import java.io.*; public class Main{ public static void main(String[] args){ Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in))); long a=scanner.nextInt(); long m=1; for (long i=a;i>=1 ;i– ){ if ((a*a+1)%i==0){ m=i; break; } } System.out.println(2*a+m+(a*a+1)/m); } }

Posted in poj | Leave a comment

Poj Solution 1182

http://poj.org/problem?id=1182 import java.io.*; public class Main { public static void main(String[] args) throws IOException { Main poj = new Main(); BufferedReader read = new BufferedReader(new InputStreamReader( System.in)); String[] tm = read.readLine().split(" "); int N = new Integer(tm[0]); int K = … Continue reading

Posted in poj | Leave a comment

Poj Solution 1178

http://poj.org/problem?id=1178 //* @author: import java.util.*; public class Main { static int dis[][][][]=new int[8][8][8][8]; static int x[]=new int[64],y[]=new int[64], n; static void floyd() { int i1, i2, i3, j1, j2, j3, t; for( i1=0; i1< 8; i1++ ) for( j1=0; j1< … Continue reading

Posted in poj | Leave a comment

Poj Solution 1171

http://poj.org/problem?id=1171 //* @author: ccQ.SuperSupper import java.io.*; import java.util.*; public class Main { static final int N = 1000+10; static int top,ans; static String opt = new String(); static String str[] =new String[N]; static int score[]={2,5,4,4,1,6,5,5,1,7,6,3,5,2,3,5,7,2,1,2,4,6,6,7,5,7}; public static void main(String[]args)throws Exception{ … Continue reading

Posted in poj | Leave a comment

Poj Solution 1166

http://poj.org/problem?id=1166 /* @author: */ import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int arr[]=new int[9]; int ans[]=new int[9]; int i=0; for(i=0;i< 9;i++) arr[i]=sc.nextInt(); int a1,a2,a3,a4,a5,a6,a7,a8,a9,cnt; int u[]=new int[9]; int min=100; for(a1=0;a1< 4;a1++) for(a2=0;a2< 4;a2++) for(a3=0;a3< … Continue reading

Posted in poj | Leave a comment

Poj Solution 1165

http://poj.org/problem?id=1165 //* @author: import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int sum=in.nextInt(); int lefttop=in.nextInt(); cnt=new int[11][11][11][11][11]; for (int i = 0; i < cnt.length; i++) { for (int j = … Continue reading

Posted in poj | Leave a comment

Poj Solution 1164

http://poj.org/problem?id=1164 #include<iostream> #include<string> #include<cmath> #include<algorithm> using namespace std; int mat[55][55]; int num; bool vist[55][55]; void DFS(int x,int y) { num++; vist[x][y]=true; if(mat[x][y]%2==0) //搜索该位置的西方 { if(!vist[x][y-1]) DFS(x,y-1); } mat[x][y]/=2; if(mat[x][y]%2==0) //搜索该位置的北方 { if(!vist[x-1][y]) DFS(x-1,y); } mat[x][y]/=2; if(mat[x][y]%2==0) //搜索该位置的东方 { if(!vist[x][y+1]) DFS(x,y+1); … Continue reading

Posted in poj | Leave a comment

Poj Solution 1160

http://poj.org/problem?id=1160 /* @author: */ import java.util.Scanner; import java.util.Arrays; public class Main{ static int num[]=new int[302]; static int s[][]=new int[302][302]; static void cost(int n){ int i,j,k; for(i=1;i< n;i++){ for(k=i+1; k <= n; k ++){ int mid=(i+k)/2; for(j=i;j< mid;j++) s[i][k]+=num[mid]-num[j]; for(j=mid+1;j<=k;j++) s[i][k]+=num[j]-num[mid]; … Continue reading

Posted in poj | Leave a comment

Poj Solution 1159

http://poj.org/problem?id=1159 import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader (System.in)); int total = Integer.parseInt(in.readLine()); String string = in.readLine(); System.out.println(total-LCS(string,new StringBuffer(string).reverse().toString())); } //����}��string��lcs�ij��� public static int LCS(String str1,String … Continue reading

Posted in poj | Leave a comment

Poj Solution 1158

http://poj.org/problem?id=1158 #include <stdio.h> #include <memory.h> #include <vector> #include <queue> using namespace std; int n, begin, end; int b[300], p[300], sum[300]; int r[300]; typedef pair<int,int> p_i; bool s[300][30000]; vector< p_i > e[300]; struct cmp { bool operator()( const p_i &a, const … Continue reading

Posted in poj | Leave a comment

Poj Solution 1157

http://poj.org/problem?id=1157 //* @author: 82638882@163.com import java.io.*; public class Main { static boolean[][] p; static int sz; static String[] ss; public static void main(String[] args) throws IOException { InputStreamReader is=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(is); String[] ss=in.readLine().split(" "); int a=Integer.parseInt(ss[0]); int b=Integer.parseInt(ss[1]); … Continue reading

Posted in poj | Leave a comment

Poj Solution 1155

http://poj.org/problem?id=1155 /* @author: */ import java.util.*; import java.util.Scanner; public class Main{ static int[][] dp; static int[] offspring; static int[] vert; static int[] parent; static int[] leaf; static int n,k; public static void main(String[] args) { Scanner cin=new Scanner(System.in); n=cin.nextInt(); leaf=new … Continue reading

Posted in poj | Leave a comment

Poj Solution 1154

http://poj.org/problem?id=1154 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class Main { private static String[] a = null; private static int max = -1; private static int n = 0; private static int m = 0; private static void dfs(int x, … Continue reading

Posted in poj | Leave a comment

Poj Solution 1153

http://poj.org/problem?id=1153 #include <stdio.h> #include<iostream> #include<algorithm> using namespace std; int P[300000]; int SEG=10000000; int main() { long n; long i; long j; double result,tempresult; cin>>n; for(i=0;i<n;i++) { cin>>P[i]; } sort(P,P+n); for(i=0;i<n;i++) { P[i+n]=P[i]+SEG; P[i+2*n]=P[i+n]+SEG; } tempresult=0; int s; s=P[n]+SEG/2; for(i=n+1;P[i]<=s;++i) { … Continue reading

Posted in poj | Leave a comment

Poj Solution 1152

http://poj.org/problem?id=1152 //* @author: ccQ.SuperSupper import java.io.*; import java.util.*; public class Main { static int get_base(char c){ if(c>=’0’&&c<=’9′) return c-‘0’+0; if(c>=’A’ && c<=’Z’) return c-‘A’+ 10; if(c>=’a’ && c<=’z’) return c-‘a’ + 36; return -1; } public static void main(String[]args) throws … Continue reading

Posted in poj | Leave a comment

Poj Solution 1151

http://poj.org/problem?id=1151 #include<iostream> #include"stdio.h" #include"vector" #include <algorithm> using namespace std; struct rect {double xt,yt,xb,yb; }; struct point {double x,y;}; double max(double a,double b) {if(a>b)return a; else return b; } double min(double a,double b) {if(a>b)return b; else return a; } /* rect … Continue reading

Posted in poj | Leave a comment

Poj Solution 1150

http://poj.org/problem?id=1150 #include<iostream> #include<cstring> #include<cmath> using namespace std; int get2(int n)//计算n!中质因子2的出现次数 { if(n==0) return 0; return n/2+get2(n/2); } int get5(int n)//计算n!中质因子5的出现次数 { if(n==0) return 0; return n/5+get5(n/5); } ////////////////////////////////////////////////////////////////////////// int g(int n,int x)//计算f(1) to f(n) 中,奇数数列中末尾为x的数出现的次数 { if(n==0) return 0; return … Continue reading

Posted in poj | Leave a comment

Poj Solution 1149

http://poj.org/problem?id=1149 #include <list> #include <vector> using namespace std; class pre_f { public: enum{ MAX = 1000000000 }; //��������� enum{ size = 103 }; //ͼ�����ģ(*) // ����һ��� from �� to �� ��Ϊ c �ı� void insert_edge( int from, int to, int … Continue reading

Posted in poj | Leave a comment

Poj Solution 1148

http://poj.org/problem?id=1148 #include <stdio.h> #include <algorithm> using namespace std; int num[20000], n; int ans_x[10000], ans_y[10000],sign_x[10000], sign_y[10000]; const int xsign[]={ 1, -1, -1, 1 },ysign[]={ 1, 1, -1, -1}, h[]={ 1, -1}; void creat(int end, int num[], int sign[], int ans[], int … Continue reading

Posted in poj | Leave a comment

Poj Solution 1147

http://poj.org/problem?id=1147 #include"stdio.h" int a[3001]; int next[3001]; int main() { int n,i,j,k=0; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); if(!a[i])k++; } j=1; k++; for(i=1;i<=n;i++) if(a[i]) { next[k]=i; k++; } else { next[j]=i; j++; } k=1; for(i=1;i<=n;i++) { k=next[k]; printf("%d ",a[k]); } return 0; }

Posted in poj | Leave a comment

Poj Solution 1146

http://poj.org/problem?id=1146 /* @author:zeropinzuo */ import java.io.*; import java.util.*; public class Main{ static Scanner cin; public static void main(String args[]){ cin = new Scanner(System.in); while(run(cin.next())) ; } static boolean run(String input){ if(input.compareTo("#") == 0) return false; System.out.println(next(input)); return true; } static … Continue reading

Posted in poj | Leave a comment

Poj Solution 1142

http://poj.org/problem?id=1142 import java.util.Scanner; public class Main { public static int sum = 0; public static boolean isPrime(int num) { for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) return false; } return true; … Continue reading

Posted in poj | Leave a comment

Poj Solution 1141

http://poj.org/problem?id=1141 //* @author: import java.util.*; public class Main { //f[i][j]��λ��i��λ��j����Ҫ�������С�ַ���ans[i][j]��ʾi��j֮������(��ƥ���ַ� private char s[]; private int len; private int f[][]; private String ans[][]; public Main(char s[]){ this.s=s; this.len=s.length; f=new int[len][len]; ans=new String[len][len]; for(int i=0;i< len;i++) Arrays.fill(ans[i],""); dp(); } public String[][] getAns(){ … Continue reading

Posted in poj | Leave a comment

Poj Solution 1140

http://poj.org/problem?id=1140 /* @author: */ import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(new BufferedInputStream(System.in)); int a,b,i,j,c[],f,d; c=new int[2000]; for(;;) { a=cin.nextInt(); b=cin.nextInt(); if(a==0&b==0)break; c[0]=a;i=0;f=0;j=0;d=1; System.out.print("."); for(;;) { a=(a*10)%b; if(a!=0) { … Continue reading

Posted in poj | Leave a comment

Poj Solution 1137

http://poj.org/problem?id=1137 //* @author:alpc12 import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.lang.Integer; class Node { int x, mask; int s; int f; int action; public Node(int x, int mask, int s, int f, int aa) … Continue reading

Posted in poj | Leave a comment

Poj Solution 1135

http://poj.org/problem?id=1135 import java.util.Scanner; public class Main { static final int Inf=9999999; public static void main(String[] args) { Scanner in=new Scanner(System.in); int cnt=0; while(true) { cnt++; int n=in.nextInt(); int m=in.nextInt(); if(n==0&&m==0)break; int[][] p=new int[n][n]; for(int i=0;i< n;i++) for(int j=0;j< n;j++) p[i][j]=Inf; … Continue reading

Posted in poj | Leave a comment

Poj Solution 1133

http://poj.org/problem?id=1133 #include<iostream> #include"math.h" #include<algorithm> #include<vector> using namespace std; struct point {long x,y;}; typedef point vect; struct {point p; int bright; }star[1001]; int sn,vn; double v_sin,v_cos,bi; vect vec[1001]; double len; point pt[1001]; inline double jl(point &a,point &b) {return sqrt(double(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));} inline long … Continue reading

Posted in poj | Leave a comment

Poj Solution 1132

http://poj.org/problem?id=1132 #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #define NMAX 33 #define INF 1000000001 int xp[4]={0,1,0,-1},yp[4]={1,0,-1,0}; int dx[4]={0,0,-1,-1},dy[4]={0,-1,-1,0}; int stx,sty; int map[NMAX][NMAX]; char in[NMAX*NMAX]; int set(char t) { switch(t) { case ‘N’:return 0; case ‘E’:return 1; case ‘S’:return 2; case ‘W’:return 3; } … Continue reading

Posted in poj | Leave a comment

Poj Solution 1131

http://poj.org/problem?id=1131 import java.io.*; import java.util.*; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin=new Scanner(System.in); BigDecimal temp,sum,ans,num; String str; int i,len; while(cin.hasNext()){ str=cin.next(); len=str.length(); temp=BigDecimal.valueOf(8.0); sum=BigDecimal.ONE; ans=BigDecimal.ZERO; for(i=2;i< len;i++) { int val=str.charAt(i)-‘0’; num=BigDecimal.valueOf(val); sum=sum.multiply(temp); ans=ans.add(num.divide(sum)); } System.out.printf("%s … Continue reading

Posted in poj | Leave a comment

Poj Solution 1129

http://poj.org/problem?id=1129 import java.util.*; import java.io.*; /* *����������ŵ7��䡣��һ������ͼ��Ϊÿ����������ɫ��Ҫ���������ڵĶ�����ɫ��ͬ�� * �����ٵ���ɫ���Ƕ���? *������������ɫ���?ֱ��ö����ɫ��+DFS������DFS�DZ�fö��ÿ�����ɫ���Ա��ҵ�һ����н⡣ */ public class Main{ public static int[][] g=new int[26][26]; public static int solve(int n){ int i,j,cnum; boolean tag=true; // �ޱ�ͼֻ��1ɫ���� for(i=0;i< n && tag;i++){ for(j=i+1;j< n && tag;j++){ if(g[i][j]==1) tag=false; … Continue reading

Posted in poj | Leave a comment

Poj Solution 1126

http://poj.org/problem?id=1126 import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { Scanner s = new Scanner(System.in); String sentence = null; int count; char ch; while ((sentence = s.nextLine()) != null) { count = … Continue reading

Posted in poj | Leave a comment

Poj Solution 1122

http://poj.org/problem?id=1122 //* @author: 82638882@163.com import java.util.Arrays; import java.util.Scanner; public class Main { static final int Inf=9999999; public static void main(String[] args) { Scanner in=new Scanner(System.in); int a=in.nextInt(); int[][] p=new int[a][a]; for(int i=0;i< a;i++) for(int j=0;j< a;j++) p[i][j]=in.nextInt(); int loc =in.nextInt()-1; … Continue reading

Posted in poj | Leave a comment

Poj Solution 1120

http://poj.org/problem?id=1120 /* @author: */ import java.util.Scanner; import java.util.Arrays; public class Main{ int map[][]=new int[22][22]; int d[]=new int[16]; char code[]={‘.’,’!’,’X’,’#’}; int sum(int i,int j){ return map[i][j]+map[i-1][j]+map[i+1][j]+map[i][j-1]+map[i][j+1]; } public void doIt(){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=0;i< 16;i++) d[i]=sc.nextInt(); for(int i=1;i<=20;i++) … Continue reading

Posted in poj | Leave a comment

Poj Solution 1119

http://poj.org/problem?id=1119 import java.io.BufferedReader; import java.io.InputStreamReader; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.HashMap; import java.util.Map; public class Main{ private static String convertString(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) >= … Continue reading

Posted in poj | Leave a comment

Poj Solution 1118

http://poj.org/problem?id=1118 //* @author: 82638882@163.com import java.util.*; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); while(true) { int a=in.nextInt(); if(a==0)break; int[] arrx=new int[a]; int[] arry=new int[a]; for(int i=0;i< a;i++) { arrx[i]=in.nextInt(); arry[i]=in.nextInt(); } int max=0; for(int … Continue reading

Posted in poj | Leave a comment

Poj Solution 1113

http://poj.org/problem?id=1113 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { class Point { int x; int y; public Point(int x, int y) { this.x = x; this.y = y; } } public static void main(String[] args) throws IOException { … Continue reading

Posted in poj | Leave a comment

Poj Solution 1112

http://poj.org/problem?id=1112 #include <vector> #include <algorithm> #include <stdio.h> #include <memory.h> #include <math.h> using namespace std; vector<int> unknow[100]; vector< pair<int,int> > s; int sign[100]; bool e[100][100]; int flag[110][110]; int a, b, an, bn; bool search( int k, int key ) { int … Continue reading

Posted in poj | Leave a comment

Poj Solution 1111

http://poj.org/problem?id=1111 //* @author: 82638882@163.com import java.io.*; class Main { static int cnt; static char[][] map; static int[][] bool; public static void main(String[] args) throws IOException { InputStreamReader is=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(is); while(true) { cnt=0; String[] ss=in.readLine().split(" "); int row=Integer.parseInt(ss[0]); … Continue reading

Posted in poj | Leave a comment

Poj Solution 1110

http://poj.org/problem?id=1110 #include "stdio.h" char map[80][10][80]; int n; int main( ) { int n, r, c, i, j, k, l, test = 0; while( scanf( "%d%d%d", &n, &r, &c ) == 3 ) { if( n == 0 ) break; for( … Continue reading

Posted in poj | Leave a comment

Poj Solution 1108

http://poj.org/problem?id=1108 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> struct window { char c; int hig, wid; int rc, lc; bool al; } wi[70]; char str[70]; int stack[70]; int top; char res[70][70]; void change(int len, bool worh, int w) { int rl, ll, rn, … Continue reading

Posted in poj | Leave a comment

Poj Solution 1105

http://poj.org/problem?id=1105 /* @author: */ import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n,m,times=1,map[]=new int[7]; char bits[]=new char[129],s[]=new char[20]; while(true){ n=sc.nextInt(); if(n==0) break; for(int i=0;i< n;i++){ s=sc.next().toCharArray(); map[s[1]-‘1’]=(char)i; } bits=sc.next().toCharArray(); m=sc.nextInt(); System.out.printf("S-Tree #%d:n",times++); … Continue reading

Posted in poj | Leave a comment

Poj Solution 1103

http://poj.org/problem?id=1103 #include <iostream> #include <memory> #define MAX_N 75 #define MAX_T 150 using namespace std; bool visited[MAX_N + 5][MAX_N + 5][2]; char input[MAX_N + 5][MAX_N + 5]; int w, h, cyNum, lCy = INT_MIN; int startH, startW, startP; bool inRange(int curH, … Continue reading

Posted in poj | Leave a comment

Poj Solution 1102

http://poj.org/problem?id=1102 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader read = new BufferedReader(new InputStreamReader( System.in)); int s; String[] ss; String str; String[] num = new String[] { "1111110", "0000110", … Continue reading

Posted in poj | Leave a comment

Poj Solution 1101

http://poj.org/problem?id=1101 #include <iostream> #include <queue> using namespace std; const int MAXN = 80; char cmap[MAXN][MAXN]; bool used[MAXN][MAXN]; short turn[MAXN][MAXN]; // [i,j]’s turn direction int vis[MAXN][MAXN]; // the number int w, h, res; bool isSucceed; struct node { int x, y; … Continue reading

Posted in poj | Leave a comment

Poj Solution 1100

http://poj.org/problem?id=1100 #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; #define NIL (unsigned)-1 #define MAX 12 typedef vector <int> VI; typedef unsigned int UI; int cas, N, F; int equ, tmp_num[MAX], UFS[MAX]; VI num, prio, pos, opr, v0, … Continue reading

Posted in poj | Leave a comment

Poj Solution 1096

http://poj.org/problem?id=1096 #include<iostream> #include"memory.h" using namespace std; char map[60][60][60]; long answer; int n,m,h; void find() {int i,j,k,key; answer=0; for(i=0;i<m;i++) for(j=0;j<n;j++) {key=1; for(k=0;k<h;k++)if(map[k][i][j]==1&&key){answer++;key=0;} else if(map[k][i][j]==2)key=1; key=1; for(k=h-1;k>=0;k–)if(map[k][i][j]==1&&key){answer++;key=0;} else if(map[k][i][j]==2)key=1; } for(i=0;i<h;i++) for(j=0;j<m;j++) {key=1; for(k=0;k<n;k++)if(map[i][j][k]==1&&key){answer++;key=0;} else if(map[i][j][k]==2)key=1; key=1; for(k=n-1;k>=0;k–)if(map[i][j][k]==1&&key){answer++;key=0;} else if(map[i][j][k]==2)key=1; } for(i=0;i<h;i++) … Continue reading

Posted in poj | Leave a comment