Category Archives: poj

Poj Solution 2414

http://poj.org/problem?id=2414 #include"stdio.h" #include"string.h" int best[1024][26]; int min[1024]; char m[1024][1001]; char answer[1001]; int n,l; bool init() { int i; scanf("%d %d",&n,&l); if(n==0&&l==0) return 0; for(i=0;i<n;i++) scanf("%s",m[i]); return 1; } inline int min_int(int a,int b) { return a<b?a:b; } void doit(int k) … Continue reading

Posted in poj | Leave a comment

Poj Solution 2413

http://poj.org/problem?id=2413 import java.io.BufferedInputStream; import java.math.BigInteger; import java.util.Scanner; /** * * poj2413 * @author NC */ public class Main { public static void main(String[] args) { Scanner scan = new Scanner(new BufferedInputStream(System.in)); while (scan.hasNext()) { BigInteger a = scan.nextBigInteger(); BigInteger b … Continue reading

Posted in poj | Leave a comment

Poj Solution 2411

http://poj.org/problem?id=2411 import java.util.Scanner; import java.util.Arrays; public class Main{ /* f[i][j]表示第i行,方格排布为二进制数j(第k位上为1表示凸出一个格子,为0表示不凸出) 的方案数。用DFS进行状态转移。*/ static long f[][]=new long[12][2048]; static int n, m; static void dfs(int i, int j, int jj, int s)//j是初始状态,jj是目标状态.s表示列数 { if (s == m)//把i行m列放好 f[i + 1][jj] += f[i][j];//等于I+1行被占去的格子的2进制为JJ应该可以多放f[i][j]的方略 else … Continue reading

Posted in poj | Leave a comment

Poj Solution 2407

http://poj.org/problem?id=2407 import java.util.Scanner; import java.util.Arrays; public class Main { // ���1��n-1��n���ʵ���ĸ���,�������� static int eular(int n) { int ret = 1,i; for (i = 2;i * i <= n;i++) if (n % i == 0) { n /= i; ret *= … Continue reading

Posted in poj | Leave a comment

Poj Solution 2406

http://poj.org/problem?id=2406 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAXN 1000000 char substr[MAXN + 2]; int sublen; int next[MAXN + 1]; void getNext() { next[1] = 0; int i, j = 0; for (i = 2; i <= … Continue reading

Posted in poj | Leave a comment

Poj Solution 2405

http://poj.org/problem?id=2405 //* @author ������&lt;hongxp11@163.com&gt; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(true) { int D = in.nextInt(); int V = in.nextInt(); if(D==0 && V==0) break; //double v1 = (d * … Continue reading

Posted in poj | Leave a comment

Poj Solution 2403

http://poj.org/problem?id=2403 import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); String[] fLine = cin.nextLine().split(" "); int kwNum = Integer.valueOf(fLine[0]).intValue(); int descNum = Integer.valueOf(fLine[1]).intValue(); HashMap hm = new HashMap(); for(int i = 0; … Continue reading

Posted in poj | Leave a comment

Poj Solution 2402

http://poj.org/problem?id=2402 #include<iostream> #include<cmath> using namespace std; //int a[20]={9,9,90,90,900,900,9000,9000,90000,90000,900000,900000,9000000,9000000,90000000};//x位上的回文数个数为f(x)=9*10^((x+1)/2); __int64 b[19]={0,9,18,108,198,1098,1998,10998,19998,109998,199998,1099998,1999998, 10999998,19999998,109999998,199999998,1099999998,1999999998};//前x位上的回文数总个数 int c[10]; int main() { __int64 i,j,n,d,k,l; double temp; while(scanf("%I64d",&i)!=EOF&&i) { for(j=0;;j++) { if(i<=b[j]) break; } //j回文数的位数 n=b[j-1];//1…n-1 数的回文数总数 d=i-n-1; temp=(j+1)/2-1; k=(int)pow(10.0,temp); k+=d; l=k; if(j%2==1) l=k/10; for(j=0;l>0;j++) { c[j]=l%10;//前半部分和后半部分对称 … Continue reading

Posted in poj | Leave a comment

Poj Solution 2400

http://poj.org/problem?id=2400 #include<iostream> #include"string.h" #include"stdio.h" using namespace std; #define valuetype int #define null 0 const int size=20; bool maxmatch(int n,int m,bool w[size][size],int *p,bool *setn,bool *setm) { int p_n[size]; int p_m[size]; bool sign[size]; int q[size],from[size],s,t; int i,j,link,now,h; for(i=0;i<n;i++)p_n[i]=-1; for(j=0;j<m;j++)p_m[j]=-1; for(i=0;i<n;i++) if(p_n[i]==-1) { … Continue reading

Posted in poj | Leave a comment

Poj Solution 2398

http://poj.org/problem?id=2398 /* @author:����acmilan_fan@yahoo.cn */ import java.io.*; import java.util.Map; import java.util.TreeMap; public class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n, m, y1, y2, i, t; String s; String[] ss; Point p,p2; … Continue reading

Posted in poj | Leave a comment

Poj Solution 2395

http://poj.org/problem?id=2395 /* @author: */ import java.util.Scanner; public class Main{ public static void main(String args[]) { int p[][]=new int[2001][2001]; int n,m,i,j,k; int dis[]=new int[2001]; int u,v,l; int ne[]=new int[2001]; Scanner sc=new Scanner(System.in); n=sc.nextInt(); m=sc.nextInt(); int max=0; for(i=0;i< n;i++) for(j=0;j< n;j++) p[i][j]=1000000001; … Continue reading

Posted in poj | Leave a comment

Poj Solution 2394

http://poj.org/problem?id=2394 //* @author: ccQ.SuperSupper import java.io.*; import java.util.*; import java.math.*; import java.text.*; public class Main { public static int mat[][]=new int[505][505]; public static int d[]=new int[505]; public static void main(String args[]) throws Exception { Scanner cin=new Scanner(System.in); int F,P,C,M; int … Continue reading

Posted in poj | Leave a comment

Poj Solution 2393

http://poj.org/problem?id=2393 import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int m = n; int s = cin.nextInt(); Node[] node = new Node[n]; int i = 0; long sum … Continue reading

Posted in poj | Leave a comment

Poj Solution 2392

http://poj.org/problem?id=2392 //* @author: import java.util.Scanner; import java.util.Arrays; public class Main{ private int k; private Block block[]; private boolean access[]=new boolean[400001]; public Main(int k,Block block[]){ this.k=k; this.block=block; } private int doIt(){ int maxs=0; access[0]=true; Arrays.sort(block); for(int i=0;i< k;i++) { int t=0; … Continue reading

Posted in poj | Leave a comment

Poj Solution 2390

http://poj.org/problem?id=2390 import java.io.BufferedInputStream; import java.math.BigInteger; import java.util.Scanner; /** * * Poj2390 * @author NC */ public class Main { public static void main(String[] args) { Scanner scan = new Scanner(new BufferedInputStream(System.in)); if (scan.hasNext()) { int r = scan.nextInt(); long m … Continue reading

Posted in poj | Leave a comment

Poj Solution 2389

http://poj.org/problem?id=2389 import java.io.BufferedInputStream; import java.math.BigInteger; import java.util.Scanner; /** * * poj2389 * @author NC */ public class Main { public static void main(String[] args) { Scanner scan = new Scanner(new BufferedInputStream(System.in)); if (scan.hasNext()) { BigInteger a = scan.nextBigInteger(); BigInteger b … Continue reading

Posted in poj | Leave a comment

Poj Solution 2388

http://poj.org/problem?id=2388 import java.io.BufferedInputStream; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(new BufferedInputStream(System.in)); if (scan.hasNext()) { int n = scan.nextInt(); int[] array = new int[n + 1]; for (int i = 0; i … Continue reading

Posted in poj | Leave a comment

Poj Solution 2387

http://poj.org/problem?id=2387 import java.util.*; public class Main{ public static void main(String args[]){ Scanner in=new Scanner(System.in); int c=0; int t=in.nextInt();//����˳�����WA��N�Σ������������⣡ int n=in.nextInt(); int dist[]=new int[n+1];//��������ȷ�������С������RE��N�Σ� int[][] w=new int[n+1][n+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ w[i][j]=Integer.MAX_VALUE; } } for(int i=0;i< t;i++){//����}�㼰֮����� int sn=in.nextInt(); int en=in.nextInt(); … Continue reading

Posted in poj | Leave a comment

Poj Solution 2386

http://poj.org/problem?id=2386 //* @author: 82638882@163.com import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int a=in.nextInt(); int b=in.nextInt(); int count=0; int[][] arr=new int[a+2][b+2]; for(int i=1;i<=a;i++){ String s=in.next(); for(int j=1;j<=b;j++) arr[i][j]=s.charAt(j-1); } for(int i=1;i<=a;i++) for(int j=1;j<=b;j++) … Continue reading

Posted in poj | Leave a comment

Poj Solution 2385

http://poj.org/problem?id=2385 //* @author: ccQ.SuperSupper import java.io.*; import java.util.*; public class Main { /** * @param args */ public static int max(int x,int y) { if(x>y) return x; return y; } public static void main(String[] args) throws Exception{ // TODO Auto-generated … Continue reading

Posted in poj | Leave a comment

Poj Solution 2383

http://poj.org/problem?id=2383 //* @author: ccQ.SuperSupper import java.io.*; import java.util.*; import java.math.*; class circle{ int x,y,r,color; } class Set{ int n,m; int next[][] = new int[1010][1010]; void init(int _n,int _m){ for(n=0;n<=_n;++n) for(m=0;m<=_m;++m) next[n][m] = m; n = _n; m = _m; } … Continue reading

Posted in poj | Leave a comment

Poj Solution 2381

http://poj.org/problem?id=2381 #include <stdio.h> #include <string.h> char b[16000002]; int main() { unsigned long a,m,c,r; memset(b,0,sizeof(b)); scanf("%lu%lu%lu%lu",&a,&c,&m,&r); while(1) { r=(a*r+c)%m; if(b[r]) break; else b[r]=1; } long i,max=0,last=0; for(i=0;i<16000002;i++) if(b[i]) break; last=i++; for(;i<16000002;i++) { if(b[i]) { // printf("%lu ",i); if(i-last>max) { max=i-last; } … Continue reading

Posted in poj | Leave a comment

Poj Solution 2380

http://poj.org/problem?id=2380 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<string.h> #include<math.h> #define INF 30000 #define NMAX 500003 typedef struct { long q,s,v; }data; data a[NMAX]; data b[NMAX]; long y[NMAX]; long x[NMAX]; long N; int cmp(const void *a,const void *b) { if(((data*)a)->s==((data*)b)->s) return ((data*)a)->q-((data*)b)->q; else return … Continue reading

Posted in poj | Leave a comment

Poj Solution 2377

http://poj.org/problem?id=2377 //* @author: <strong>Yeming&nbsp;Hu</strong>&quot;cslittleye@gmail.com&quot; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(new BufferedInputStream(System.in)); int n = sc.nextInt(); int m = sc.nextInt(); int[] mind = new int[n]; boolean[] reached = … Continue reading

Posted in poj | Leave a comment

Poj Solution 2376

http://poj.org/problem?id=2376  

Posted in poj | Leave a comment

Poj Solution 2372

http://poj.org/problem?id=2372 #include <stdio.h> #include <string.h> char w[1000], *symbol="=+-*/0123456789n"; bool check_comment(int &s) { do { while( w[s] && w[s] != ‘*’ ) s++; if( !w[s] || !w[ s+1 ] ) return false; else if( w[ ++s ] == ‘)’ ) return … Continue reading

Posted in poj | Leave a comment

Poj Solution 2371

http://poj.org/problem?id=2371 import java.util.Arrays; import java.util.Scanner; public class Main { public static void quickSort(int[] data, int s, int t) { int i = s+1; int j = t; int x = data[s]; while (i <= j) { while (i <= j … Continue reading

Posted in poj | Leave a comment

Poj Solution 2370

http://poj.org/problem?id=2370 //* @author: 82638882@163.com import java.util.*; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int a=in.nextInt(),t=0,i; int[] b=new int[a]; for(i=0;i< a;i++) b[i]=in.nextInt(); a/=2; Arrays.sort(b); for(i=0;i<=a;i++) t+=b[i]/2+1; System.out.println(t); } }

Posted in poj | Leave a comment

Poj Solution 2367

http://poj.org/problem?id=2367 import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * * Accepted. BigInteger is used here. * * */ public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int[][] … Continue reading

Posted in poj | Leave a comment

Poj Solution 2366

http://poj.org/problem?id=2366 //* @author: ccQ.SuperSupper import java.io.*; import java.util.*; public class Main { static double N = 10000.0; public static void main(String []args) throws Exception{ double temp; int i,j,n,m,t=1; Map first = new HashMap(); StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); … Continue reading

Posted in poj | Leave a comment

Poj Solution 2365

http://poj.org/problem?id=2365 //* @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))); int n=scanner.nextInt(); double r=scanner.nextDouble(); double[] x=new double[n]; double[] y=new double[n]; for (int i=0;i< n ;i++ ){ x[i]=scanner.nextDouble(); y[i]=scanner.nextDouble(); } … Continue reading

Posted in poj | Leave a comment

Poj Solution 2363

http://poj.org/problem?id=2363 //* @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))); int c=scanner.nextInt(); int n,sm; for (int l=0;l< c ;l++ ){ n=scanner.nextInt(); sm=4*n+2; for (int i=1;i<=n ;i++ ){ for (int … Continue reading

Posted in poj | Leave a comment

Poj Solution 2362

http://poj.org/problem?id=2362 import java.io.*; import java.util.*; public class Main { //记录数字的总长度。 int totalNum; //记录各个数字的值的数组。 int[] numArr; //记录每个小短棒是否被使用过。 boolean[] used; public static void main(String args[]) throws Exception { Main m=new Main(); m.begin(); } private void begin() throws Exception { Scanner cin = … Continue reading

Posted in poj | Leave a comment

Poj Solution 2361

http://poj.org/problem?id=2361 /* @author: */ import java.util.Scanner; public class Main{ public static void main(String args[]) { char cc[][]=new char[3][3]; Scanner sc=new Scanner(System.in); int n,i,j; n=sc.nextInt(); while((n–)!=0) { for(i=0;i< 3;i++) cc[i]=sc.next().toCharArray(); int x=0,o=0; boolean xw=false,ow=false; boolean bb=true; for(i=0;i< 3;i++) for(j=0;j< 3;j++) { … Continue reading

Posted in poj | Leave a comment

Poj Solution 2353

http://poj.org/problem?id=2353 #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #define NMAX 501 #define INF 2000000001 long m[NMAX][NMAX]={0}; long a[NMAX][NMAX]={0}; long M,N; long b[NMAX*NMAX]={0}; int xp[3]={-1,0,1},yp[3]={0,-1,0}; void solve() { int i,j; for(i=1;i<=N;i++) { m[1][i]=a[1][i]; } for(i=2;i<=M;i++) { for(j=1;j<=N;j++) { if(m[i][j]>a[i][j]+m[i-1][j]) m[i][j]=a[i][j]+m[i-1][j]; } for(j=2;j<=N;j++) { if(m[i][j]>a[i][j]+m[i][j-1]) … Continue reading

Posted in poj | Leave a comment

Poj Solution 2352

http://poj.org/problem?id=2352 import java.io.*; public class Main { static int[] star=new int[32002]; static int[] lev=new int[15000]; public static void main(String[] args) throws NumberFormatException, IOException { InputStreamReader is=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(is); int a=Integer.parseInt(in.readLine()); int n=a; while((a–)!=0) { String[] ss=in.readLine().split(" "); int … Continue reading

Posted in poj | Leave a comment

Poj Solution 2350

http://poj.org/problem?id=2350 import java.util.Scanner; /** * poj2350 easy * @author NC */ public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); if (scan.hasNext()) { int n = scan.nextInt(); for (int i = 0; i < … Continue reading

Posted in poj | Leave a comment

Poj Solution 2349

http://poj.org/problem?id=2349 //* @author: 82638882@163.com import java.io.*; import java.util.Arrays; public class Main { static int[] p; public static void main(String[] args) throws NumberFormatException, IOException { InputStreamReader is=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(is); int n=Integer.parseInt(in.readLine()); while((n–)!=0) { String[] ss=in.readLine().split(" "); int a=Integer.parseInt(ss[0]); int … Continue reading

Posted in poj | Leave a comment

Poj Solution 2348

http://poj.org/problem?id=2348 /* @author: */ import java.util.Scanner; public class Main{ static boolean judge(int x,int y) { int t; if(x< y) {t=x;x=y;y=t;} if(x%y==0) return true; if(x-y< y) return !judge(y,x-y); return true; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int … Continue reading

Posted in poj | Leave a comment

Poj Solution 2346

http://poj.org/problem?id=2346 import java.util.*; /** * * @author leo */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, max=0; int[][] dp = new int[6][46]; for(int i=0;i<=9;i++){ dp[1][i]=1; } for(int i=1;i<=5;i++){ dp[i][0]=1; } … Continue reading

Posted in poj | Leave a comment

Poj Solution 2344

http://poj.org/problem?id=2344 #include"stdio.h" #include"algorithm" #include"math.h" using namespace std; int n,m; const double eps=1e-8; const int size=2000; double a[size][size]; bool sign[size]; void swap_c(int k,int l) { int i; for(i=0;i<m;i++) std::swap(a[i][k],a[i][l]); } bool guass() { int i,j,k,l;double temp; for(i=0;i<m;i++) sign[i]=1; for(l=0,i=0;i<n;i++) { while(fabs(a[l][i])<eps) … Continue reading

Posted in poj | Leave a comment

Poj Solution 2339

http://poj.org/problem?id=2339 //* @author ������&lt;hongxp11@163.com&gt; import java.util.Scanner; public class Main { /** * @param args */ public static int row; public static int column; public static int replace(int i, int j, int[][] array) { int temp = 0; if (array[i][j] == … Continue reading

Posted in poj | Leave a comment

Poj Solution 2336

http://poj.org/problem?id=2336 //* @author ������&lt;hongxp11@163.com&gt; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); for(int i = 0; i< num; i++) { int n = in.nextInt(); int t = … Continue reading

Posted in poj | Leave a comment

Poj Solution 2333

http://poj.org/problem?id=2333 #include <stdio.h> const int size = 5010; struct point { int x, y; }p[size], pt[size]; __int64 s[size]; inline __int64 cross( point &o, point &a, point &b ) { return (__int64)(a.x-o.x)*(b.y-o.y) – (__int64)(a.y-o.y)*(b.x-o.x); } inline __int64 dis( point &a, point … Continue reading

Posted in poj | Leave a comment

Poj Solution 2328

http://poj.org/problem?id=2328 import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); List guess = new ArrayList(); List response = new ArrayList(); while(cin.hasNext()) { String guessNum = cin.nextLine(); if(guessNum.equals("0")) break; String result = cin.nextLine(); … Continue reading

Posted in poj | Leave a comment

Poj Solution 2325

http://poj.org/problem?id=2325 //* @author import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { BigInteger n,zero,ten; int count,flag,a[],i,j; a=new int[2000]; Scanner cin = new Scanner(System.in); while(cin.hasNext()) { n = cin.nextBigInteger(); zero=BigInteger.ZERO; ten=BigInteger.TEN; if(n.compareTo(BigInteger.valueOf(-1))==0) break; if(n.compareTo(ten)< … Continue reading

Posted in poj | Leave a comment

Poj Solution 2323

http://poj.org/problem?id=2323 //* @author:alpc12 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.math.BigInteger; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { new Main().run(); } BigInteger [][] dp; private void run() throws FileNotFoundException { Scanner … Continue reading

Posted in poj | Leave a comment

Poj Solution 2318

http://poj.org/problem?id=2318 /* @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() != false) ; } static boolean run(){ int n = cin.nextInt(); if(n==0) return false; Box box … Continue reading

Posted in poj | Leave a comment

Poj Solution 2316

http://poj.org/problem?id=2316 //* @author: 82638882@163.com import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int[] a=new int[300]; int k=0; while(in.hasNextInt()) { String s=in.next(); for(int i=0;i< s.length();i++) a[i]=(a[i]+s.charAt(i)-‘0’)%10; k=s.length(); } for(int i=0;i< k;i++) { System.out.print(a[i]); } … Continue reading

Posted in poj | Leave a comment

Poj Solution 2313

http://poj.org/problem?id=2313 //* @author: import java.util.*; public class Main { static long abs(long a) { if(a< 0) return -a; else return a; } static boolean in(long a,long b,long c) { if(abs(a-c)+abs(b-c)==abs(a-b)) return true; else return false; } static public void main( … Continue reading

Posted in poj | Leave a comment