Poj Solution 2479

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

 import java.io.BufferedReader;
 import java.io.InputStreamReader;

 public class Main {

     int t;
     int l;
     int[] a;
     String[] s;
     int[] left;
     int[] right;
     int max = 0;

     int temp;

     public Main() throws Exception {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         t = Integer.parseInt(read.readLine());
         for (int i = 0; i < t; i++) {
             read.readLine();
             l = Integer.parseInt(read.readLine());
             a = new int[l];
             left = new int[l];
             right = new int[l];
             s = read.readLine().split(" ");
             for (int j = 0; j < l; j++) {
                 a[j] = Integer.parseInt(s[j]);
             }
             search();
             System.out.println(max);
         }
     }

     public void search() {
         temp = a[0];
         left[0] = temp;
         for (int i = 1; i < l; i++) {
             if (temp < 0) {
                 temp = 0;
             }
             temp += a[i];
             if (temp > left[i - 1]) {
                 left[i] = temp;
             } else {
                 left[i] = left[i - 1];
             }
         }
         temp = a[l - 1];
         right[0] = temp;
         for (int i = l - 2, k = 1; i >= 0; i--, k++) {
             if (temp < 0) {
                 temp = 0;
             }
             temp += a[i];
             if (temp > right[k - 1]) {
                 right[k] = temp;
             } else {
                 right[k] = right[k - 1];
             }
         }
         max = left[0] + right[l - 2];
         for (int i = 0; i < l - 1; i++) {
             if (left[i] + right[l - i - 2] > max) {
                 max = left[i] + right[l - i - 2];
             }
         }
     }

     public int getLength(int m, int n) {
         max = Integer.MIN_VALUE;
         temp = 0;
         for (int i = m; i < n; i++) {
             temp += a[i];
             if (temp > max) {
                 max = temp;
             }
             if (temp < 0) {
                 temp = 0;
             }
         }
         return max;
     }

     public static void main(String[] args) throws Exception {
         new Main();
     } 
}

							
Posted in poj | Leave a comment

Poj Solution 2477

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

#include<iostream>
#include"stdio.h"
using namespace std;
struct tree
{
    int l,r;
    __int64 value;
    int child;
}t[100];
int n,root;
bool init()
{
    int i,times[100];
    cin>>n;
    if(n==0) return 0;
    for(i=1;i<=n;i++)
        times[i]=0;
    for(i=1;i<=n;i++)
    {
        cin>>t[i].l>>t[i].r;
        if(t[i].l>0)
            times[t[i].l]++;
        if(t[i].r>0)
            times[t[i].r]++;
    }
    for(i=1;i<=n;i++)
        if(times[i]==0)
        {
            root=i;
            break;
        }

    return 1;
}
const __int64 maxvalue=(__int64)1<<60;
int travel(int a)
{
    if(a<=0)return 0;
    
    t[a].child=travel(t[a].l)+travel(t[a].r)+1;

    return t[a].child;
}
void great(int s,__int64 value)
{
    t[s].value=value;
    
    if(t[s].l>0)
        great(t[s].l,value/2/t[s].child*t[t[s].l].child);
    if(t[s].r>0)
        great(t[s].r,value/2/t[s].child*t[t[s].r].child);

    return ;
}
int main()
{
    int i;
    while(init())
    {
        travel(root);
        great(root,maxvalue);
        for(i=1;i<=n;i++)
        {
            printf("%I64d",t[i].value);
            if(i<n)printf(" ");
        }
        printf("n");
    }
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2473

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

#include<iostream>
#include"algorithm"
using namespace std;

struct rect
{
    int x1,x2,y1,y2;
}r[210];

int n,w,h,n1,n2,m;
int x[411],y[411];

void init()
{
    int i;
    cin>>n>>w>>h;
    for(i=0;i<n;i++)
    {
        cin>>r[i].x1>>r[i].y1>>r[i].x2>>r[i].y2;
        x[i]=r[i].x2;
        y[i]=r[i].y2;
    }

    m=n+1;
    x[n]=0,y[n]=0;

    std::sort(x,x+m);
    std::sort(y,y+m);
    n1=std::unique_copy(x,x+m,x)-x;
    n2=std::unique_copy(y,y+m,y)-y;
}

inline int max(int a,int b)
{
    return a>b?a:b;
}
inline int min(int a,int b)
{
    return a<b?a:b;
}

void doit()
{
    int ww,hh,i,j,k=0;
    
    cin>>ww>>hh;
    
    for(i=0;i<n1;i++)
    if(y[i]+hh<=h)
        for(j=0;j<n2;j++)
        {
            if(x[j]+ww<=w)
            {
                for(k=0;k<n;k++)
                if(min(r[k].x2,x[j]+ww)>max(r[k].x1,x[j])&&min(r[k].y2,y[i]+hh)>max(r[k].y1,y[i]))
                    break;
                if(k==n)
                    goto loop;
            }
        }
loop:
    if(i<n1)
        cout<<x[j]<<' '<<y[i]<<endl;
    else cout<<"Fail!"<<endl;
}

int main()
{
    int cas;
    cin>>cas;
    while(cas--)
    {
        init();
        doit();
    }
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2472

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

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

public class Main {
 static int N = 100+5;
    
 static int n;
 static double Graph[][] = new double[N][N];
 public static void init(){
    int i,j;
    for(i=0;i< N;++i) for(j=0;j< N;++j)
        Graph[i][j] = 0.0;
 }

  public static void main(String args[]) throws Exception{
        
   double temp;
   int i,j,u,v,m;
   //Scanner cin = new Scanner(new FileInputStream("input.txt"));
   Scanner cin = new Scanner(System.in);
    
   while(cin.hasNext()){
    n = cin.nextInt();
    if(n==0) break;
    
    init();
    
    m = cin.nextInt();
    for(i=0;i< m;++i){
        u = cin.nextInt();
        v = cin.nextInt();
        temp = cin.nextDouble();
        Graph[v][u] = Graph[u][v] = temp/100.0;
    }
            
    temp = dijkstra()*100;
    System.out.printf("%.6f", temp);
    System.out.println(" percent");
   }
 }

  public static double dijkstra(){
    int i,j,k;
    boolean flag[] = new boolean[N];
    double meat[] = new double[N];
        
    for(i=1;i<=n;++i) {
    meat[i] = Graph[1][i];
    flag[i] = false;
    }
    flag[1] = true;
        
    for(i=0;i< n;++i){
    k = -1;
    for(j=1;j<=n;++j) if(!flag[j]){
        if(k==-1 || meat[k]< meat[j])
            k = j;
    }
    if(k==-1) break;
    flag[k] = true;
        
    for(j=1;j<=n;++j)if(!flag[j]){
        if(meat[j]< meat[k]*Graph[k][j])
            meat[j] = meat[k]*Graph[k][j];
    }
    }
    return meat[n];        
  }
}

							
Posted in poj | Leave a comment

Poj Solution 2470

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

//* @author ������&lt;hongxp11@163.com&gt;
//�t���� buffered

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
 public static void main(String[] args) throws IOException {
        
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

   while (true) {
    int n = Integer.parseInt(in.readLine());
    if (n == 0)
        break;
    //StringTokenizer st = new StringTokenizer(in.readLine()," ");
    String s = in.readLine();
    String[] sArray=s.split(" ");
    Map< Integer, Integer> map = new HashMap< Integer, Integer>();
    int[] array = new int[n];
    int i = 0;
//    while(st.hasMoreTokens()) {
//        int key = Integer.parseInt(st.nextToken());
//        array[i] = key;
//        map.put(key, i+1);
//        i++;
//    }
//    int sl = sArray.length;
    //System.out.println(sl);
    for(i = 0; i< n; i++){
        int key = Integer.parseInt(sArray[i]);
        array[i] = key;
        map.put(key, i+1);
    }
    int count = 0;
    for (i = 0; i < n; i++) {
        if (map.get(i+1) == array[i])
            count++;
        else
            break;
        //System.out.println(array[i]+" "+map.get(i+1));
    
    }
    if (count == n)
        System.out.println("ambiguous");
    else
        System.out.println("not ambiguous");
        
  }
 }
}

							
Posted in poj | Leave a comment

Poj Solution 2469

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

#include<iostream>
using namespace std;
char *suit[]={"Clubs", "Diamonds", "Hearts", "Spades"};
char *value[]={"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

int shuffles[101][52],n;
int card[52];

void init()
{
    int i,j;
    cin>>n;
    
    for(i=0;i<n;i++)
    for(j=0;j<52;j++)
        cin>>shuffles[i][j];

    for(i=0;i<52;i++)
        card[i]=i;
}

int main()
{
    int temp[52];
    int k,i;
    
    init();
    
    while(cin>>k)
    {
        k--;
        for(i=0;i<52;i++)
            temp[i]=card[shuffles[k][i]-1];
        
        for(i=0;i<52;i++)
            card[i]=temp[i];

        for(i=0;i<52;i++)
        {
            cout<<value[card[i]%13]<<" of "<<suit[card[i]/13]<<endl;
        }
        cout<<endl;
    }

    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2465

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

#include<iostream>
using namespace std;
inline int min(int a,int b)
{
    return a<b?a:b;
}

int dis[112],price[112];
int l,n;

void init()
{
    n=0;
    cin>>l;
    while(cin>>dis[n])
        cin>>price[n++];
    price[n]=99999999;
    dis[n]=l;
    n++;
}

int doit()
{
    int i,j,s,temp[212],p;
    int best[212];

    for(i=0;i<201;i++)
        best[i]=-1;
    best[100]=0;

    for(s=dis[0],i=0;i<n;i++)
    {
        for(j=0;j<201;j++)
            temp[j]=-1;
        for(j=s;j<201;j++)
            temp[j-s]=best[j];
        
        
        if(i<n-1)
        {
            p=-1;
            for(j=0;j<201;j++)
            {
                if(temp[j]>=0)
                {
                    if(p<0||p>temp[j])
                        p=temp[j];
                }

                if(p>=0&&(temp[j]<0||p<temp[j]))
                    temp[j]=p;
                if(p>=0)p+=price[i];
            }
        }

        for(j=0;j<201;j++)
            best[j]=temp[j];
        s=dis[i+1]-dis[i];
    }
    
    int ans=-1;

    for(j=100;j<201;j++)
        if(best[j]>=0&&(ans<0||best[j]<ans))
            ans=best[j];
    return ans;
}

int main()
{
    int ans;
    init();
    ans=doit();
    if(ans<0)
        cout<<"Impossible"<<endl;
    else cout<<ans<<endl;

    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2463

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

#include<iostream>
#include"math.h"
using namespace std;

double k,l,s,w;
const double g=9.81;

bool init()
{
    cin>>k>>l>>s>>w;
    return !(k==0&&l==0&&s==0&&w==0);
}
int calculate()
{
    double v,v0,h,vv;
    if(s<=l||k==0)
    {
        v=sqrt(2*s*g);
        
        if(v>10)return -1;
        else return 1;
    }
    else
    {
        v0=sqrt(2*l*g);
        h=(2*w*g+sqrt(4*w*w*g*g+8*k*w*g*l))/(2*k);
        h+=l;
        
        if(h<s)return 0;
        vv=(2*w*g*s-k*(s-l)*(s-l))/w;

        if(vv<0)while(1)cout<<"faint"<<endl;
        v=sqrt(vv);
        if(v>10)return -1;
        else return 1;
    }
}
int main()
{
    while(init())
    {
        switch(calculate())
        {
        case 1:cout<<"James Bond survives."<<endl;break;
        case 0:cout<<"Stuck in the air."<<endl;break;
        case -1:cout<<"Killed by the impact."<<endl;break;
        }
    }
    return 0;
}
        
							
Posted in poj | Leave a comment

Poj Solution 2462

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

 
							
Posted in poj | Leave a comment

Poj Solution 2460

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

//* @author: ccQ.SuperSupper
import java.io.*;
class point{
    int x,y;
}
public class Main {
 static int n;
 static final int N = 200000+100;
 static point position[] = new point[N];
    
 public static void main(String[]args) throws Exception{
        
  int i;
  start();
        
  StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
  while(true){
    n = Get_Num(cin);
    if(n<=0) break;
    for(i=0;i< n;++i){
        position[i].x = Get_Num(cin);
        position[i].y = Get_Num(cin);
    }
    solve();
  }
 }

 static void start(){
  for(int i=0;i< N;++i) position[i] = new point();
  }

  static void solve(){
  int i,x,y,ansa,ansb;
  ansa = ansb = 0;
  x = position[n/2].x;
  y = position[n/2].y;
  for(i=0;i< n;++i){
    if((position[i].x>x && position[i].y>y)||(position[i].x< x && position[i].y< y))
        ++ansa;
    if((position[i].x>x && position[i].y< y)||(position[i].x< x && position[i].y>y))
        ++ansb;
  }
  System.out.println(ansa+" "+ansb);
 }

 static int Get_Num(StreamTokenizer cin) throws Exception{
    cin.nextToken();
    return (int)cin.nval;
 }
}

							
Posted in poj | Leave a comment

Poj Solution 2459

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

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

public class Main {
 static final int N = 2000+10;
 static int DP[] = new int[N];
 public static void main(String[]args) throws Exception{
    int n,F1,F2,Day,i,sum,a,b,j;
    //Scanner cin = new Scanner(new FileInputStream("input.txt"));
    Scanner cin = new Scanner(System.in);
    n = cin.nextInt();
    F1 = cin.nextInt();
    F2 = cin.nextInt();
    Day = cin.nextInt();
    for(i=0;i< N;++i) DP[i] = 0;
    for(i=0;i< n;++i){
        a = cin.nextInt();
        b = cin.nextInt();
        for(j=a;j<=b;++j)
            ++DP[j];
    }
    sum = 0;
    for(i=Day;i>=0;--i){
        sum+=DP[i];
        if(sum==F1-F2){
            System.out.println(i);
            break;
        }
    }
 }
}

							
Posted in poj | Leave a comment

Poj Solution 2458

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

#include "stdio.h"

char map[6][6];
bool sign[6][6];
int flag[6][6];
bool inline inmap( int x, int y )
{
    return 0<=x&&x<5&&0<=y&&y<5;
}
int dx[]={ 0,-1, 0, 1};
int dy[]={-1, 0, 1, 0};
int ans;
int reach;
int visit;
void find( int x, int y )
{
    int i, xx, yy;
    reach++;
    flag[x][y] = visit;

    for( i=0; i<4; i++ )
    if( inmap( xx=x+dx[i], yy=y+dy[i] ) && !sign[xx][yy] && flag[xx][yy] < visit )
    {
        find( xx, yy );
        if( reach == 7 ) return;
    }
}
void search( int j , int step , int J, int H )
{
    int i = j;
    if( H >= 4 )return;
    if( step == 7 )
    {
        reach = 0;
        visit++;
        find( i/5, i%5 );
        if( reach == 7 ) ans++;        
        return;
    }
    for( i++; i<25 &&( j<0 || ( i/5 - j/5 ) <= 1 ); i++ )
    {
        sign[i/5][i%5] = false;
        search( i, step+1, J+(int)(map[i/5][i%5]=='J'), H+(int)(map[i/5][i%5]=='H') );
        sign[i/5][i%5] = true;
    }
}
int main()
{
    int i, j;
    for( i=0; i<5; i++ )    
        scanf( "%s", map[i] );    
    for( i=0; i<5; i++ )
    for( j=0; j<5; j++ )
    {
        sign[i][j] = true;
        flag[i][j] = 0;
    }
    ans = 0;visit = 0;    
    search( -1, 0, 0, 0 );
    printf( "%dn", ans );
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2457

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

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;
public class Main {
    static final int N = 10000+10;
    static int meat[] = new int[N],pre[] = new int[N],n,K;
    static Vector Graph[] = new Vector[N];
    static Queue que = new LinkedList<Integer>();
    static void start(){
        for(int i=0;i< N;++i)
            Graph[i] = new Vector();
    }
    static void init(){
        que.clear();
        for(int i=0;i< N;++i){
            Graph[i].clear();
            meat[i] = N+N;
            pre[i] = -1;
        }
        meat[1] = 1;
        que.offer(1);
    }

 public static void main(String[]args) throws Exception{
  int i,u,v;
  start();
  init();
  StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

  n = Get_Num(cin);
  K = Get_Num(cin);
  for(i=0;i< n;++i){
    v = Get_Num(cin);
    u = Get_Num(cin);
    Graph[v].add(u);
  }

  solve();
  if(meat[K]>=N) System.out.println(-1);
  else{
    System.out.println(meat[K]);
    make_ans();
  }
 }
 
  static void solve(){
   int i,j,k,temp;
   while(!que.isEmpty()){
    temp = (Integer) que.element();
    que.remove();
    k = Graph[temp].size();
    for(i=0;i< k;++i){
        j = (Integer) Graph[temp].elementAt(i);
        if(meat[j]>meat[temp]+1){
            meat[j] = meat[temp]+1;
            pre[j] = temp;
            que.add(j);
        }
    }
   }
 }

  static void make_ans(){
   int ans[] = new int[N],i,k=K,top=0;
   while(true){
    ans[top++] = k;
    k = pre[k];
    if(k< 0) break;
   }
   PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
   for(i=top-1;i>=0;--i)
    out.println(ans[i]);
   out.flush();
 }

  static int Get_Num(StreamTokenizer cin) throws Exception{
   cin.nextToken();
   return (int) cin.nval;
 }
}

							
Posted in poj | Leave a comment

Poj Solution 2456

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

#include <stdio.h>
#include <algorithm>
using namespace std;

int pos[100000], n, cc;

bool check( int l )
{
    int i, r, j;
    for( r=cc-1,i=0,j=1; j<n && r; j++ )
    {
        if( pos[j]-pos[i]>=l )
        {
            r--;
            i=j;
        }
    }
    return r == 0;
}

int main()
{
    int i, j, a, b, c;
    scanf( "%d %d", &n, &cc );

    for( i=0; i<n; i++ )
    scanf( "%d", &pos[i] );

    sort( pos, pos+n );    

    a = 0, b = ( pos[n-1] - pos[0] ) / ( cc - 1 ) + 1;
    
    while( b > a+1 )
    {
        c = ( a + b ) / 2;
        if( check(c) ) a = c;
        else b = c;
    }
    printf( "%dn", a );

    return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 2454

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

#include"stdio.h"
#include"memory.h"
#include"algorithm"
#include"stdlib.h"

using namespace std;

typedef pair<int,int> city;

city c[180];
int n, k;


void init()
{
    int i;
    scanf( "%d", &n );
    k = n;
    n *= 3;

    for( i=0; i<n; i++ )
    {
        scanf( "%d", &c[i].first );
        c[i].second = i;
    }

    sort( c, c+n );
    
}

int a[60],b[60];


void search( int s1, int s2, int a[60], int b[60] , int s )
{
    int x, y, i, j;
    while( s1 <= s || s2 <= s )
    {
        i = rand()%k;
        j = rand()%k;
        x = c[a[i]].first;
        y = c[b[j]].first;

        s1 += y - x;
        s2 += x - y;
        swap( a[i], b[j] );
    }
}
    


void doit()
{
    int i,j,t,h,s1=0,s2=0;
    
    for( i=k; i<n; i++ )
    if( ( i - k ) % 2 == 0 )
    {
        s1 += c[i].first;
        a[ ( i - k ) / 2 ] = i;
    }
    else
    {
        s2 += c[i].first;
        b[ ( i - k ) / 2 ] = i;
    }

    if( s1 <= k*500 )
    search( s1, s2, a, b, k*500 );
    
    for( i=0; i<k; i++ )
    printf( "%dn", c[a[i]].second+1 );
    
    for( i=0; i<k; i++ )
    printf( "%dn", c[b[i]].second+1 );
    
    for( i=0; i<k; i++ )
    printf( "%dn", c[i].second+1 );

}

int main()
{
    init();
    doit();

    return 0;
}



							
Posted in poj | Leave a comment

Poj Solution 2453

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

import java.util.Scanner;    
  
public class Main {   
       
 /**  
  * 获得一个Integer对应的二进制中1的个数  
  * @param i  
  * @return sum  
  */  
 public static int getSum(int i) {   
  int sum = 0;   
  while (i > 0) {   
   i = i & (i - 1);   
   ++sum;   
  }   
  return sum;   
 }    
  
 public static void main(String[] args) {   
  Scanner sc = new Scanner(System.in);   
  while (sc.hasNext()) {   
   int i = sc.nextInt();   
   if (i == 0)   
    break;   
   int sum1 = getSum(i);   
   for (int j = i + 1;; ++j) {   
    int sum2 = getSum(j);   
    if (sum2 == sum1) {   
     System.out.println(j);   
     break;   
    }   
   }   
  }   
 }   
  
}  
//出处:<a href="http://blog.csdn.net/lazy_p/archive/2009/12/22/5054607.aspx" target="_blank">http://blog.csdn.net/lazy_p/archive/2009/12/22/5054607.aspx</a>

							
Posted in poj | Leave a comment

Poj Solution 2452

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

//* @author:
import java.util.*;
public class Main {
 
static public void main( String [] str ) throws Exception{
   Scanner sc = new Scanner(System.in);
   int i, j, h, ans;
    
   while(sc.hasNext())
   {
      int n=sc.nextInt();
      int s[]=new int[n];
      for( i=0; i< n; i++ )
     s[i]=sc.nextInt();
      ans = 0;
      for( i=0; i< n && i+ans< n; i++ )
     if( i==0 || s[i-1]>s[i] ) 
       for( h=s[i], j=i+1; j< n; j++ )
       {
         if( s[i] > s[j] ) break;
        if( s[j] > h )
        {
           h = s[j];
           if( j-i > ans ) ans = j-i;
        }
        }

    if( ans == 0 ) ans = -1;
    System.out.printf( "%dn", ans );
     }

   }
}


							
Posted in poj | Leave a comment

Poj Solution 2450

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

//* @author:

import java.util.*;
import java.math.*;

public class Main {
  static int gcd(int a,int b)
  {
    int c;
    while( (a%=b) != 0 )
    {
         c=b;
      b=a;
      a=c;
    }
    return b;
   }
    
  static int a,b,c,d,n,m,p,q;
  static int t[] = new int[100001];
    
  static BigInteger comb(int a,int b)
 {
   int i,j,g,k,last;
   if( b > a/2 )
     b = a - b;
   last = b;
    
   for(i=0;i< b;i++)
   {
    t[i]=a-i;
   }
        
   for(i=2;i<=b;i++)
   {
    k=i;
    for(j=0;j< b/*last*/&&k>1;j++)
    if(t[j]>1)
    {
         g=gcd(t[j],k);
      t[j]/=g;
      k/=g;
    }
    }
                    
    BigInteger ans = BigInteger.valueOf(1);
    int temp=1;
    for(i=0;i< last;i++)
    if(t[i]>1)
    {
      temp*=t[i];
      if(temp>=10000)
      {
        ans=ans.multiply( BigInteger.valueOf(temp) );
        temp=1;
       }
    }
        
    ans=ans.multiply( BigInteger.valueOf(temp) );
    return ans;
   }

   static void doit()
   {
     int x1,x2,n1,m1,n2,m2,temp1,temp2, temp;
     boolean key;
     BigInteger ans = BigInteger.valueOf( 0 );
     if( (int)(((n-p)/a)< ((m-q)/b)?((n-p)/a):((m-q)/b)) > 
    (int)(((n-p)/c)< ((m-q)/d)?((n-p)/c):((m-q)/d)) ) 
     {
    temp = a; a = c; c = temp;
    temp = b; b = d; d = temp;
     }
        
    for( x1=0 ; (n1=n-a*x1)>=0 && (m1=m-b*x1)>=0 ; x1++ )
    {    
    temp1=(n1>p) ? (n1-p+c-1)/c : 0;
    temp2=(m1>q) ? (m1-q+d-1)/d : 0;
    x2 = temp1>temp2 ? temp1 : temp2 ;
    n2 = n1-c*x2; m2 = m1-d*x2;
    while( n2>p&&m2>q )
         System.out.println( "error" );
                        
    key = true;
    while( n2>=0 && m2>=0 && key )
    {
       key = false;
       if( x1 != 0 && ( n2 + a > p || m2 + b > q ) ) {
        ans = ans.add( comb( x1+x2-1, x1-1 ) );
        key = true;
       }
                        
     if( x2 != 0 && ( n2 + c > p || m2 + d > q ) ) {
       ans = ans.add( comb( x1+x2-1, x2-1 ) );
       key = true;
    }
                
    n2 -= c;
    m2 -= d;
    x2++;
    }
   }
        
   System.out.println( ans );
    //ans.output('n');
  }

  public static void main( String [] str )
  {
    int cas;
    Scanner cin = new Scanner( System.in );
    cas = cin.nextInt();
    while( cas-- != 0 )
    {    
        n = cin.nextInt();
     m = cin.nextInt();
     p = cin.nextInt();
     q = cin.nextInt();
     a = cin.nextInt();
     b = cin.nextInt();
     c = cin.nextInt();
     d = cin.nextInt();
    doit();
    }
    return;
  }
}


							
Posted in poj | Leave a comment

Poj Solution 2449

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

 
							
Posted in poj | Leave a comment

Poj Solution 2447

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

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef unsigned __int64 u64;
#define MAX 30
#define MAXN 5
u64 len, dig, limit;
u64 factor[MAXN];
u64 mod(u64 a, u64 b, u64 n){
        if(!a)return 0;
        else return ( ((a&dig)*b)%n + (mod(a>>len,b,n)<<len)%n )%n;
}
u64 by(u64 a, u64 b, u64 n){
            u64 p;
            p = 8, len = 61;
        while(p<n){
                    p<<=4;
                    len -=4;
        }
            dig = ((limit/p)<<1) - 1; 
        return mod(a,b,n);
}
u64 random(){//产生随机数 
            u64 a;
            a = rand();
            a *= rand();
            a *= rand();
            a *= rand();
        return a;
}
u64 square_multiply(u64 x, u64 c, u64 n){//(x^c)%n快速算法 
            u64 z=1;
        while(c){
                if(c%2==1)z = by(z,x,n);
                    x = by(x,x,n);
                    c=(c>>1);
        }
        return z;
}
bool Miller_Rabin(u64 n){//Miller_Rabin素数测试 
    if(n<2)return false;
        if(n==2)return true;
        if(!(n&1))return false;
            u64 k = 0, i, j, m, a;
            m = n - 1;
        while(m%2==0)m=(m>>1),k++;
        for(i=0;i<MAX;i++){
                    a = square_multiply(random()%(n-1)+1, m, n);//平方乘
                if(a==1)continue;
                for(j=0;j<k;j++){
                        if(a==n-1)break;
                            a = by(a,a,n);
                }
                if(j<k)continue;
                return false ;
        }
        return true;
}
u64 gcd(u64 a,u64 b){
        if(a==0) return b;
        else return gcd(b%a,a);
}
u64 f(u64 x, u64 n ){
        return (by(x,x,n)+1)%n;
}
u64 Pollard(u64 n){
                if(n<=2)return 0;
                if(!(n&1))return 2; 
            u64 i, p, x,xx;
        for(i=0;i<MAX;i++){                                 
                    x = random()%n; //或者直接用 x = i
                    xx = f(x,n);
                    p = gcd((xx+n-x)%n , n);
                while(p==1){
                            x = f(x,n);
                            xx = f(f(xx,n),n);
                            p = gcd((xx+n-x)%n,n)%n;
                }
                if(p)return p;
        }
        return 0;
}
u64 prime(u64 a){
        if(Miller_Rabin(a))return a;
            u64 t = Pollard(a);
        if(t!=0)
        {return prime(t);}
           
}
u64 Euclid(u64 a,u64 b,__int64 &x,__int64 &y)
{
 if(b==0)
 { 
     x=1,y=0;
  return a;
 } 
 u64 t,d;
 if(b!=0)
 d=Euclid(b,a%b,x,y);
 t=x;
 x=y;
 if(b!=0)
 y=t-a/b*y;
 return d;
}
int main(){
            u64 c,e,n,i,j,m,t,n0,T,ans,l;
        __int64 T0,x,y,d;
            limit = 1;
            limit = limit<<63; 
        while(scanf("%I64u%I64u%I64u",&c,&e,&n)!=EOF){
              m=0;n0=n;
               while(n%2==0){n/=2;factor[m++]=2;}
               while(n>1){
                        if(Miller_Rabin(n))break;
                            t = prime(n);
                            factor[m++] = t;
                        if(t!=0)
                            n/=t;
                }
               if(n>1)factor[m++]=n;
               //for(l=0;l<m;l++)printf("%I64un",factor[l]);
                   T0=(__int64)(factor[0]-1)*(factor[1]-1);
          T=(u64)T0;
               Euclid(e,T,x,y);
                   d=x;
      //printf("%I64dn",d);
      //while(d<0)d+=T0;
                   d=(d%T0+T0)%T0;
               //d=(__int64)d;
     // printf("%I64d %I64dn",d,T0);
                   ans=square_multiply(c,(u64)d,n0);
               printf("%I64un",ans);
                   
}
}
							
Posted in poj | Leave a comment

Poj Solution 2446

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

#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN=1050;

int link[MAXN];
int total;

bool vist[33][33];                                                                                                //用来标记那些位置有洞
bool usedif[MAXN];
bool mat[MAXN][MAXN];

bool Can(int t)
{
     int i;
     for(i=0;i<total;i++)
      if(mat[t][i]&&!usedif[i])
      {
          usedif[i]=true;
          if(link[i]==-1||Can(link[i]))
          {
             link[i]=t;
             return true;
          }
      }
     return false;
}

int maxMatch()
{
    int i,num=0;
    memset(link,-1,sizeof(link));
    for(i=0;i<total;i++)
    {
        memset(usedif,0,sizeof(usedif));
        if(Can(i))
           num++;
    }
    return num;
}

int main()
{
    int i,j,x,y,u,t,r,c,num,ans;
    while(scanf("%d%d%d",&r,&c,&t)!=EOF)
    {
        memset(vist,0,sizeof(vist));
        for(i=0;i<t;i++)
        {
            scanf("%d%d",&y,&x);
            vist[x-1][y-1]=true;
        }
        total=r*c;
        num=total-t;
        if(num&1)                                                                                          //需要覆盖的格子数是奇数,那么不会存在可行方案
        {
             printf("NOn");
             continue;
        }
        memset(mat,0,sizeof(mat));
        for(i=0;i<r;i++)
         for(j=0;j<c;j++)
          if(!vist[i][j])
          {
               u=i*c+j;
               if((i-1)>=0&&!vist[i-1][j])
                   mat[u][(i-1)*c+j]=true;
               if((i+1)<r&&!vist[i+1][j])
                   mat[u][(i+1)*c+j]=true;
               if((j-1)>=0&&!vist[i][j-1])
                   mat[u][i*c+(j-1)]=true;
               if((j+1)<c&&!vist[i][j+1])
                   mat[u][i*c+(j+1)]=true;
          }
        ans=maxMatch();
        if(ans==num)
            printf("YESn");
        else
            printf("NOn");
    }

    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2445

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

#include"stdio.h"
#include"algorithm"
using namespace std;
int l[1500][1500],r[1500][1500],u[1500][1500],d[1500][1500],lu[1500][1500],rd[1500][1500];
int n;
inline int max(int a,int b)
{
    return a>b?a:b;
}
inline int min(int a,int b)
{
    return a<b?a:b;
}
bool init()
{
    int i,j,m,k,a,b;
    char c;
    if(scanf("%d",&n)!=1)
        return 0;
    scanf("%d",&m);
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
        r[i][j]=d[i][j]=0;
    while(m--)
    {
        getchar();
        scanf("%c%d%d%d",&c,&i,&j,&k);
        i--,j--;
        if(c=='H')
        {
            r[i][j]=max(r[i][j],k);
        }
        else
        {
            d[i][j]=max(d[i][j],k);
        }
    }    
    for(i=0;i<n;i++)
    {
        a=0,b=0;
        for(j=0;j<n;j++)
        {
            if(j>b)b=r[i][j]+j,a=j;
            else if(r[i][j]+j>b)b=r[i][j]+j;
            l[i][j]=a;
        }
        a=n-1,b=n-1;
        for(j=n-1;j>=0;j--)
        {
            if(j<b)b=l[i][j],a=j;
            else if(l[i][j]<b)b=l[i][j];
            r[i][j]=a;
        }
    }
    for(j=0;j<n;j++)
    {
        a=0,b=0;
        for(i=0;i<n;i++)
        {
            if(i>b)b=d[i][j]+i,a=i;
            else if(d[i][j]+i>b)b=d[i][j]+i;
            u[i][j]=a;
        }
        a=n-1,b=n-1;
        for(i=n-1;i>=0;i--)
        {
            if(i<b)b=u[i][j],a=i;
            else if(u[i][j]<b)b=u[i][j];
            d[i][j]=a;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            lu[i][j]=min(j-l[i][j],i-u[i][j]);
            rd[i][j]=min(r[i][j]-j,d[i][j]-i);
        }
    }

    return 1;
}
int m,c[1500],id[1500],x[1500],y[1500];
inline int lowbit(int x)
{
    return x&(x^(x-1));
}

bool cmp(int a,int b)
{
    return rd[x[a]][y[a]]+a<rd[x[b]][y[b]]+b;
}
int count(int bx,int by)
{
    int i,j,k,ans=0;
    m=min(n-bx,n-by);
    for(i=0;i<m;i++)
        id[i]=i,x[i]=bx+i,y[i]=by+i,c[i]=0;    
    sort(id,id+m,cmp);    
    for(i=0,j=0;i<m;i++)
    {
        k=i+1;
        while(k)
        {
            ans+=c[k];
            k-=lowbit(k);
        }        
        k=i-lu[bx+i][by+i]/*-1+1*/;
        while(k)
        {
            ans-=c[k];
            k-=lowbit(k);
        }        
        k=i+1;
        while(k<=m)
        {
            c[k]++;
            k+=lowbit(k);
        }
        while(j<m&&(id[j]+rd[x[id[j]]][y[id[j]]])<=i)
        {
            k=id[j++]+1;
            while(k<=m)
            {
                c[k]--;
                k+=lowbit(k);
            }
        }
    }

    return ans;
}
void doit()
{
    int x,y,ans=0;
    for(x=0;x<n;x++)
        ans+=count(x,0);
    for(y=1;y<n;y++)
        ans+=count(0,y);
    printf("%dn",ans);
}
int main()
{
    while(init())
    {
        doit();
    }
    return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 2443

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

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;
class Set{
    void init(){
        for(int i=0;i< 35;++i) state[i] = 0;
    }
    void get_int(int cnt){
        int k = cnt/30;
        int posi = cnt%30;
        state[k]|=dir[posi];
    }
    int dir[] = {1,1<<1,1<<2,1<<3,1<<4,1<<5,1<<6,1<<7,1<<8,1<<9,1<<10,1<<11,1<<12,1<<13,1<<14,
1<<15,1<<16,1<<17,1<<18,1<<19,1<<20,1<<21,1<<22,1<<23,1<<24,1<<25,1<<26,1<<27,
1<<28,1<<29,1<<30,1<<31};
    int state[] = new int[35];
}

public class Main {
    
  static final int N = 10000+2;
  public static int get_num(StreamTokenizer in) throws Exception{
        
   in.nextToken();
   return (int) in.nval;
  }

  public static void main(String []args) throws Exception{
        
   int i,j,n,m,u,v;
   Set set[] = new Set[N];
   for(i=0;i< N;++i){
    set[i] = new Set();
    set[i].init();
   }
   //Scanner cin = new Scanner(new FileInputStream("input.txt"));
   //Scanner cin = new Scanner(System.in);
   StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
                
   n = get_num(in);
   //n = cin.nextInt();
   for(i=1;i<=n;++i){
    //m = cin.nextInt();
    m = get_num(in);
    for(j=0;j< m;++j){
        //set[cin.nextInt()].get_int(i);
        set[get_num(in)].get_int(i);
    }
    }
    //m = cin.nextInt();
    m = get_num(in);
    for(i=1;i<=m;++i){
        //u = cin.nextInt();
        //v = cin.nextInt();
        u = get_num(in);
        v = get_num(in);
        for(j=0;j< 35;++j){
            if((set[u].state[j]&set[v].state[j])>0){
                System.out.println("Yes");
                break;
            }
        }
        if(j>=35) System.out.println("No");
    }
        
   }

}

							
Posted in poj | Leave a comment

Poj Solution 2441

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

//* @author: 
/*
  ����: 
   N��ţ��M�������(barn)��ÿ��ţ���м����Լ�ϲ����barn��Ҫ��Ϊÿ��ţ����һ��barn��
   ʹ��ÿ��ţ��ֵ���barn�����Լ�ϲ���ģ���ÿ��barn�v�ֻ������һ��ţ����Ϸ��ķ��䷽��������N,M<=20��

����:
  ��һ����N��M(1<=N<=20, 1<=M<=20);
  ����4��N��:
  ��i�еĵ�һ�������ǵ�i��ţϲ����barn��,���������ǵ�i��ţϲ����barn���(1<=i<=N)
���:
   ���䷽��������
*/
import java.util.*;
public class Main
{
 static final int N=20+2;
 static final int M=1<< 20+1;

 public static void main(String[] args){
   Scanner sc=new Scanner(System.in);
  int n, m, p;
   int mat[][]=new int[N][N];
   int dp[][]=new int[2][M];
   int i, k, j, next;
   n=sc.nextInt();
   m=sc.nextInt();
   for (i = 1; i <= n; i++)
   {
     p=sc.nextInt();
     while ((p--)!=0)
    {
     k=sc.nextInt();
     mat[i][k] = 1;
    }
  }

  int s = 0;
  dp[s][0] = 1;
  for (i = 1; i <= n; i++)
 {
   for (j = 0; j < (1 << m); j++) 
   {
     if (dp[s][j]!=0)
     { 
      for (k = 1; k <= m; k++)
      {
       if (mat[i][k] == 1)
       {
         next = j | (1 << (k - 1));
         if (next != j)      // ��k��barnû��ռ�� 
            dp[(s + 1) % 2][next] += dp[s][j];
       }
                                             
     }
    dp[s][j] = 0;
  }
 }

 s = (s + 1) % 2;
}
 int sum = 0;
 for (i = 0; i < (1 << m); i++) sum += dp[s][i];
      System.out.printf("%dn", sum);
}
}
							
Posted in poj | Leave a comment

Poj Solution 2440

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

import java.util.Scanner;

public class Main {
public static int[][] org;
public static int[][] m;
public static int[][] f = new int[4][1];
public static void main(String[] args) {
   Scanner cin = new Scanner(System.in);
   while(cin.hasNext()) {
    int n = cin.nextInt();
    if(test(n))
     continue;
    reset();
    n = n-6;
    while(n != 0) {
     if(n % 2 != 0) {
      org = mul(org, m);
     }
     n = n / 2;
     m = mul(m, m);
    }
   
    f = mul(org, f);
    System.out.println(f[0][0]%2005);
   }
}

private static int[][] mul(int[][] a, int[][] b) {
   int row = a.length;
   int col = b[0].length;
   int[][] test = new int[row][col];
   for (int i = 0; i< row; i++) {
    for (int j = 0; j< col; j++) {
     for (int k = 0; k< a[0].length; k++) {
      test[i][j]=test[i][j]+a[i][k]*b[k][j];
     }
     test[i][j] = test[i][j] % 2005;
    }
   }
   return test;
}


private static void reset() {
   org = new int[4][4];
   int size = org.length;
   for(int i=0; i< size; i++) {
    org[i][i] = 1;
   }
   m = new int[4][4];
   m[0][0] = 1;
   m[0][1] = 0;
   m[0][2] = 1;
   m[0][3] = 1;
   for(int i=1; i< size; i++) {
    m[i][i-1] = 1;
   }
   f[0][0] = 25;
   f[1][0] = 15;
   f[2][0] = 9;
   f[3][0] = 6;
}

private static boolean test(int n) {
   if(n == 3) {
    System.out.println(6);
    return true;
   }
   else if(n == 4) {
    System.out.println(9);
    return true;
   }
   else if(n == 5) {
    System.out.println(15);
    return true;
   }
   else if(n == 6) {
    System.out.println(25);
    return true;
   }else if(n == 1) {
    System.out.println(2);
    return true;
   }else if(n == 2) {
    System.out.println(4);
    return true;
   }else if(n == 0) {
    System.out.println(1);
    return true;
   }
   return false;
}

}


							
Posted in poj | Leave a comment

Poj Solution 2437

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

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;
class POOL implements Comparable{
    int left,right;
    public int compareTo(Object oo){
        POOL temp = (POOL) oo;
        if(temp.left< this.left) return 1;
        return -1;
    }
}

 public class Main {
  static final int N = 10000+100;
  static int n,L;
  static POOL pool[] = new POOL[N];
  static void start(){
    for(int i=0;i< N;++i) pool[i] = new POOL();
  }

  public static void main(String[]args) throws Exception{
   int i;
   start();
   StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

   n = Get_Num(cin);
   L = Get_Num(cin);
   for(i=0;i< n;++i){
    pool[i].left = Get_Num(cin);
    pool[i].right = Get_Num(cin);
   }
   Arrays.sort(pool,0,n);
   System.out.println(solve());
        
  }

  static int solve(){
   int ans=0,cnt=0,i=0,j;
   while(i< n){
    if(pool[i].right>cnt-1){
        j = (Min(pool[i].right-pool[i].left,pool[i].right-cnt)+L-1)/L;
        cnt = Max(pool[i].left,cnt)+j*L;
        ans+=j;
    }
    ++i;
   }
   return ans;
  }

  static int Max(int a,int b){
    return a>b?a:b;
  }

  static int Min(int a,int b){
        return a< b?a:b;
    }
    static int Get_Num(StreamTokenizer cin) throws Exception{
        cin.nextToken();
        return (int) cin.nval;
    }
    
}

							
Posted in poj | Leave a comment

Poj Solution 2436

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

//* @author: 82638882@163.com
import java.io.*;
public class Main
{
 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 n=Integer.parseInt(ss[0]);
  int d=Integer.parseInt(ss[1]);
  int k=Integer.parseInt(ss[2]);
  int[] q=new int[n];
  for(int i=0;i< n;i++)
  {
   ss=in.readLine().split(" ");
   int a=Integer.parseInt(ss[0]);
   for(int j=0;j< a;j++){
    int f=Integer.parseInt(ss[j+1]);
    q[i]+=Math.pow(2, f-1);
   }    
  }
  System.out.println(f(d,k,q,0,0,0));
 }

 static int f(int d,int k,int[] q,int c,int s,int l)
 {    
  if(c==k)
  {
    int ans=0;
    s=~s;
    for(int i=0;i< q.length;i++)    
        if((s&q[i])==0)ans++;
    return ans;
   }
   if(l+k>d+c) return 0;
   int w=(int) Math.pow(2, l);
   return Math.max(f(d,k,q,c,s,l+1), f(d,k,q,c+1,s+w,l+1));
  }
}
							
Posted in poj | Leave a comment

Poj Solution 2435

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

#include <stdio.h>
#include <queue>
using namespace std;
typedef pair<int,int> point;
int dx[] = { 0, 1,-1, 0};
int dy[] = { 1, 0, 0,-1};
char way[] = { '-','|','|','-'};
char to[] = { 'E','S','N','W'};
int n,m,bx,by,ex,ey;
bool sign[100][100];
char map[100][100];
int from[100][100];
queue<point> q;
inline bool inmap( int x, int y )
{
    return 0<=x && x<n && 0<=y && y<m;
}
void findpath()
{
    point p;
    int xx,yy,i,x,y;
    while( !q.empty() )
        q.pop();
    q.push( point(bx,by) );
    sign[bx][by] = true;
    while( !q.empty() )
    {
        p = q.front();
        q.pop();
        x = p.first, y = p.second;

        for( i=0; i<4; i++ )
        {
            if( inmap( xx=x+dx[i]*2, yy=y+dy[i]*2 ) && !sign[xx][yy] && map[x+dx[i]][y+dy[i]] == way[i] )
            {
                from[xx][yy] = 3 - i;
                sign[xx][yy] = true;
                if( xx == ex && yy == ey ) return;
                q.push( point(xx, yy) );
            }
        }
    }
        
}
void init()
{
    int i, j;
    scanf( "%d%d", &n, &m );
    n = n*2-1, m = m*2-1;

    for( i=0; i<n; i++ )
    for( j=0; j<m; j++ )
    {
        scanf( "%1s", map[i]+j );
        if( map[i][j] == 'S' )
            ex = i, ey = j;
        if( map[i][j] == 'E' )
            bx = i, by = j;
    }

}
void doit()
{
    int i, s, x, y, j;

    findpath();

    x = ex, y = ey;j = -1;
    while( x != bx || y != by )
    {
        i = from[x][y];
        if( i == j ) s++;
        else
        {
            if( j != -1 ) printf( "%dn", s );
            s = 1;
            printf( "%c ", to[i] );
            j = i;
        }
        x += dx[i]*2, y += dy[i]*2;
    }
    printf( "%dn", s );
}
int main()
{
    init();
    doit();

    return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 2431

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

#include<iostream>
#include"algorithm"
using namespace std;
const int size=10010;

struct stop
{
    int f,x;
}p[size];
int n,l,h;

bool cmp(stop a,stop b)
{
    return a.x>b.x;
}

void init()
{
    int i;

    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>p[i].x>>p[i].f;
    }
    cin>>l>>h;
    
    p[n+1].x=l;p[n+1].f=0;
    p[n].x=0;p[n].f=0;
    n+=2;
    std::sort(p,p+n,cmp);
    
    if(p[0].x!=l)
        while(1)
            cout<<"faint"<<endl;
}

int mem[2][size],down;
int *best=mem[0],*temp=mem[1];

void doit()
{
    int i,j,t;
    
    down=0;
    
    best[0]=h;
    for(i=1;i<n;i++)
    {
        for(j=down;j<=i;j++)
            temp[j]=-1;
        for(j=down;j<=i;j++)
        {
            if(j<i&&best[j]-(p[i-1].x-p[i].x)>temp[j])
                temp[j]=best[j]-(p[i-1].x-p[i].x);
            
            if(j>down && (t=best[j-1]-(p[i-1].x-p[i].x))>=0 && (t+=p[i].f) >temp[j])
                temp[j]=t;
            
            if(temp[j]<0)
                down=j+1;
        }
        std::swap(best,temp);
    }
}

int main()
{
    int i;
    init();
    doit();

    for(i=down;i<n-1;i++)
        if(best[i]>=0)break;
    if(i<n-1)
        cout<<i<<endl;
    else cout<<-1<<endl;

    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2430

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

#include<iostream>
#include"algorithm"
using namespace std;

const int size=1010;
int ans[size][size][4];
int best[size][size];

int n,m,b,nn;
enum{both,up,down,sep};
const int inf=99999999;

struct point
{
    int y;
    int x;
    int s;
}pt[size],p[size];

bool cmp(point a,point b)
{
    return a.x<b.x;
}

void init()
{
    int i,j;
    cin>>n>>m>>b;
    
    for(i=0;i<n;i++)
    {
        cin>>pt[i].y>>pt[i].x;
        pt[i].s=0;
    }

    std::sort(pt,pt+n,cmp);
    
    p[0].x=p[0].y=0;
    p[1]=pt[0];
    for(i=1,j=2;i<n;i++)    
    {
        if(pt[i].x==pt[i-1].x)
            p[j-1].s=1;
        else p[j++]=pt[i];
    }
    nn=n;
    n=j;
    
}

void doit()
{
    int i,j,t,k;
    
    for(j=0;j<=m;j++)
    {
        for(k=0;k<4;k++)
            ans[0][0][k]=0;
        best[0][j]=0;
    }

    for(i=1;i<n;i++)
    {
        for(k=0;k<4;k++)
            ans[i][0][k]=inf;
        best[i][0]=inf;

        for(j=1;j<=m;j++)
        {
            ans[i][j][up]=ans[i][j][down]=best[i-1][j-1]+1;
            if(p[i].y==p[i-1].y && (t=ans[i-1][j][p[i].y]+p[i].x-p[i-1].x) < ans[i][j][p[i].y] )
                ans[i][j][p[i].y] = t;
            if( (t=ans[i-1][j][sep]+p[i].x-p[i-1].x) < ans[i][j][p[i].y] )
                ans[i][j][p[i].y] = t;

            ans[i][j][both]=best[i-1][j-1]+2;
            if( (t=ans[i-1][j][both]+(p[i].x-p[i-1].x)*2) <ans[i][j][both] )
                ans[i][j][both] = t;

            if(j==1)ans[i][j][sep]=inf;
            else
            {
                ans[i][j][sep]=best[i-1][j-2]+2;
                if( (t=ans[i-1][j-1][up]+p[i].x-p[i-1].x+1) < ans[i][j][sep] )
                    ans[i][j][sep] = t;
                if( (t=ans[i-1][j-1][down]+p[i].x-p[i-1].x+1) < ans[i][j][sep] )
                    ans[i][j][sep] = t;
                if( (t=ans[i-1][j][sep]+(p[i].x-p[i-1].x)*2 ) < ans[i][j][sep] )
                    ans[i][j][sep] = t;
            }
    
            if(p[i].s)
                ans[i][j][up]=ans[i][j][down]=inf;
            else
                ans[i][j][3-p[i].y]=inf;

            best[i][j]=inf;
            for(k=0;k<4;k++)
                if(ans[i][j][k]<best[i][j])
                    best[i][j]=ans[i][j][k];
        }
    }
}
int main()
{
    init();
    doit();
    
    cout<<best[n-1][m]<<endl;

    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2428

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

#include<iostream>
#include"stdio.h"
#include"math.h"
using namespace std;


struct point
{
    double x,y,z;
}p1[110],p2[110],p3[110];
int n,m;
double best[110][110];

double cross(point a,point b,point c)
{
    b.x-=a.x;b.y-=a.y;b.z-=a.z;
    c.x-=a.x;c.y-=a.y;c.z-=a.z;

    double s1,s2,s3;
    s1=b.y*c.z-b.z*c.y;
    s2=b.z*c.x-b.x*c.z;
    s3=b.x*c.y-b.y*c.x;
    return sqrt(s1*s1+s2*s2+s3*s3);
}

int init(void)
{
    int i;double s1,s2;
    cin>>n;
    if(cin.fail())return 0;
    
    for(i=0;i<n;i++)
    {
        cin>>p1[i].x>>p1[i].y;
        p1[i].z=0;
    }
    p1[n]=p1[0];
        s1=0;
    for(i=0;i<n;i++)
        s1+=p1[i].x*p1[i+1].y-p1[i].y*p1[i+1].x;

    cin>>m;
    for(i=0;i<m;i++)
    {
        cin>>p2[i].x>>p2[i].y;
        p2[i].z=10;
    }
    
    p2[m]=p2[0];
    s2=0;
    
    for(i=0;i<m;i++)
    s2+=p2[i].x*p2[i+1].y-p2[i].y*p2[i+1].x;
    
    if(s1*s2<0)
    {
        for(i=0;i<=m;i++)
        p3[i]=p2[m-i];
        for(i=0;i<=m;i++)
        p2[i]=p3[i];
     }

    return 1;
}
double answer;
void doit()
{
    int i,j,k;
    double temp,t;
    answer=1e100;

    for(i=0;i<m;i++)
    {
        for(j=0;j<=m;j++)
            p3[j]=p2[(j+i)%m];
        
        for(j=0;j<=n;j++)
        for(k=0;k<=m;k++)
            best[j][k]=1e100;

        best[0][0]=0;

        for(j=0;j<=n;j++)
        for(k=0;k<=m;k++)
        {
    //        temp=1e+100;
            
//            if(k!=0)
            {
                t=best[j][k]+cross(p3[k+1],p1[j],p3[k]);
                if(t<best[j][k+1])
                    best[j][k+1]=t;
            }
//            if(j!=0)
            {
                t=best[j][k]+cross(p3[k],p1[j],p1[j+1]);
                if(t<best[j+1][k])
                    best[j+1][k]=t;
            }
            
        }

        if(answer>best[n][m])
            answer=best[n][m];
    }
    printf("%.0lfn",answer/2);
}

int main()
{
    while(init())
        doit();
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2427

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

//* @author 
import java.util.*;
import java.math.*;
public class Main {
 static BigInteger p1,p2,p3,q1,q2,q3,a1,a2,a0,h1,h2,g1,g2,p,q;
 static int nn;
 static void solve(){
  p2=BigInteger.ONE; p1=BigInteger.ZERO;
  q2=BigInteger.ZERO; q1=BigInteger.ONE;
  a1=BigInteger.valueOf((long)Math.sqrt(nn));
  a0=BigInteger.valueOf((long)Math.sqrt(nn));
  g1=BigInteger.ZERO;h1=BigInteger.ONE;
  BigInteger n0=BigInteger.valueOf(nn);
  while(true){
   g2=a1.multiply(h1).subtract(g1);
   h2=(n0.subtract(g2.multiply(g2))).divide(h1);
   a2=g2.add(a0).divide(h2);
   p=p2.multiply(a1).add(p1);
   q=q2.multiply(a1).add(q1);
   if(p.multiply(p).subtract(n0.multiply(q.multiply(q))).compareTo(BigInteger.ONE)==0)
     return ;
   a1=a2;
   g1=g2;
   h1=h2;
   p1=p2;p2=p;
   q1=q2;q2=q;
   }
 }

 public static void main(String[] args) {
   Scanner in=new Scanner(System.in);
   while(in.hasNext()){
    nn=in.nextInt();
    int t=(int)Math.sqrt(nn);
    if(t*t==nn)
      System.out.println("No solution!");
    else{
      solve();
      System.out.println(p+" "+q);
     }
  }
 }


}
							
Posted in poj | Leave a comment

Poj Solution 2425

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

#include <vector> 
#include <iostream>
using namespace std;
vector<int> edges[1000];
int N,M,GS[1000],ans;
 
int DFS(int n) /* 典型求 SG 函数的办法 */
{
    if(GS[n]!=-1) return GS[n];
    bool used[1000];
    memset(used,0,sizeof(used));
    for(int i=0;i<edges[n].size();i++)
    {
        used[DFS(edges[n][i])]=true;
    }
    int i=0;
    while(used[i]) i++;
    return GS[n]=i;
}
 
int main()
{
    int num,next;
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=0;i<N;i++)
        {
            edges[i].clear();
            scanf("%d",&num);
            for(int j=0;j<num;j++)
            {
                scanf("%d",&next);
                edges[i].push_back(next);
            }
        }
        memset(GS,-1,sizeof(GS));
        while(scanf("%d",&M)&&M)
        {
            ans=0;
            for(int i=0;i<M;i++) scanf("%d",&next),ans^=DFS(next);
            if(ans) printf("WINn");
            else printf("LOSEn");
        }
    }
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2424

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

#include"queue"
#include"stdio.h"
using namespace std;

struct node
{
    int time;
    int dinners;
};

int a[3],ans;
queue<node> have[3];


bool init()
{
    char c,t;
    int u[3],t1,t2,i;
    node nd,nd1;

    //cin>>a[0]>>a[1]>>a[2];
    scanf("%d%d%d",&a[0],&a[1],&a[2]);

    if(a[0]==0&&a[1]==0&&a[2]==0)
        return 0;
    
    for(i=0;i<3;i++)
    {
        while(!have[i].empty())
            have[i].pop();
    }
    
    ans=0;
    u[0]=0,u[1]=0,u[2]=0;

    while(1)
    {
        
        do{
            scanf("%c",&c);
        }while(! (c=='#'||(c<='9'&&c>='0')) );
    
        if(c=='#')break;
    
        scanf("%d:%d %d",&t1,&t2,&nd.dinners);

        nd.time=((c-'0')*10+t1)*60+t2;
        i=(nd.dinners-1)/2;
        
    //    if(i>=3||i<0)continue;


        while(!have[i].empty())
        {
            nd1=have[i].front();
                
            if(nd1.time>nd.time)
                break;
            
            ans+=nd1.dinners;
            have[i].pop();
            u[i]--;
        }

        if(u[i]==a[i]&&!have[i].empty())
        {
            nd1=have[i].front();
            if(nd1.time<=nd.time+30)
            {
                ans+=nd1.dinners;
                nd.time=nd1.time;

                have[i].pop();
                u[i]--;
            }
        }

        if(u[i]<a[i])
        {
            u[i]++;
            nd.time+=30;
            if(nd.time<=23*60)
                have[i].push(nd);
        }
    }
    
    for(i=0;i<3;i++)
    while(!have[i].empty())
    {
        nd1=have[i].front();
        ans+=nd1.dinners;
        have[i].pop();
        u[i]--;
    }
    
    return 1;

}    

int main()
{
    while(init())
    {
        printf("%dn",ans);
    }
    return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 2421

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

//* @author: 82638882@163.com
import java.util.Scanner;
public class Main
{
 static int[] bin;
 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();
  bin=new int[a];
  for(int i=0;i< a;i++)
    bin[i]=i;
  int n=in.nextInt();
  while((n--)!=0)
  {
   int w1=in.nextInt();
   int w2=in.nextInt();
   union(w1-1,w2-1);
  }
  int ans=0,min,tag,tag2;
  lv: while(true)
      {
    min=9999999;
    tag=-1;tag2=-1;
    boolean bb=false;
    for(int i=0;i< a-1;i++)
    {        
      for(int j=i+1;j< a;j++)
      {
       if(root(j)!=root(i))
       {
        if(p[i][j]< min)
         {
        min=p[i][j];
        tag=j;tag2=i;
          }
        bb=true;
       }    
      }
    if(!bb) break lv;    
    }
    union(tag2,tag);
    ans+=min;
      }
      System.out.println(ans);
    }
    
  static int  root(int b)
  {
   int a=b;
   while(bin[a]!=a)
    a=bin[a];
   bin[b]=a;
   return a;
  }

 static void union(int a,int b)
 {
    int a1=root(a);
    int b1=root(b);
    if(a1==b1) return;
    if(a1>b1) bin[a1]=b1;
    else bin[b1]=a1;
  }
}
							
Posted in poj | Leave a comment

Poj Solution 2420

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

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;
import java.math.*;
class Point{
    double x,y;
}
public class Main {
    static int n;
    static final int N = 100+10;
    static Point point[] = new Point[N];
    
    static void start(){
        for(int i=0;i< N;++i)
            point[i] = new Point();
    }
    
public static void main(String[]args) throws Exception{
 int i;
 double min;
        
 StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    start();
    while(true){
         cin.nextToken();
     n = (int)cin.nval;
     if(n<=0) break;
      for(i=0;i< n;++i){
        cin.nextToken();
        point[i].x = cin.nval;
        cin.nextToken();
        point[i].y = cin.nval;
      }
    min = best(point[n/2].x,point[n/2].y,n);
    System.out.printf("%.0fn", min);
       }
  }

 static double f(double a,double b,double n){
  int i;
  double t = 0;
  for(i=0;i< n;++i){
     t+=Math.sqrt((point[i].x-a)*(point[i].x-a)+(point[i].y-b)*(point[i].y-b));
  }
  return t;
 }

  static double best(double a,double b,int n){
    int i,j=0,w=0;
    double T[] = new double[4],min;
    T[0] = f(a,b+1,n);
    T[1] = f(a+1,b,n);
    T[2] = f(a-1,b,n);
    T[3] = f(a,b-1,n);
    min = f(a,b,n);
    for(i=0;i< 4;++i){
        if(T[i]< min){
            w = 1;
            j = i;
            min = T[i];
        }
    }
    if(w!=0){
        if(j==0) min = best(a,b+1,n);
        else if(j==1) min = best(a+1,b,n);
        else if(j==2) min = best(a-1,b,n);
        else if(j==3) min = best(a,b-1,n);
    }
    return min;
  }
    
}

							
Posted in poj | Leave a comment

Poj Solution 2419

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

//* @author: 82638882@163.com
import java.io.*;
import java.util.Arrays;
public class Main
{
 public static void main(String[] args) throws NumberFormatException, 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]);
  boolean[][] p=new boolean[a][b];
  my[] p2=new my[a];
  for(int i=0;i< a;i++)
    p2[i]=new my();
  while(in.ready())
   {
    ss=in.readLine().split(" ");
    int x=Integer.parseInt(ss[0]);
    int y=Integer.parseInt(ss[1]);
    p[x-1][y-1]=true;
   }
  for(int i=0;i< a;i++)
    for(int j=0;j< b;j++)
    if(p[i][j]){
      p2[i].cnt++;
      p2[i].n+=(j+1)*(j+1)*(j+1)*(j+1);
    }
                
  Arrays.sort(p2);
  int sum=1;
  for(int i=1;i< a;i++)
  {
   if(p2[i].n==p2[i-1].n&&p2[i].cnt==p2[i-1].cnt) continue;
   sum++;
  }
  System.out.println(sum);
 }
}

class my implements Comparable< my>
{
    int n=0,cnt=0;

    public int compareTo(my o) {
     if(cnt==o.cnt)
     return n-o.n;
     return cnt-o.cnt;
    }
}

							
Posted in poj | Leave a comment

Poj Solution 2418

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

import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStreamReader;   
  
public class Main {   
       
    public static class TreeNode{   
        private String species;   
        private TreeNode left;   
        private TreeNode right;   
        private int count;   
           
    }   
       
    public static class Tree{   
        private int total;   
        private TreeNode root=new TreeNode();   
  
        public void insert(String newSpecies,TreeNode root){   
            if(root.count==0){   
                   
                root.species=newSpecies;   
                root.count++;   
                total++;   
                return;   
                   
            }   
            else{   
                if(root.species.compareTo(newSpecies)>0){   
                       
                    if(root.left==null){   
                           
                        root.left=new TreeNode();   
                           
                    }   
                    insert(newSpecies,root.left);   
                       
                }else{   
                       
                    if(root.species.compareTo(newSpecies)<0){   
                           
                        if(root.right==null){   
                               
                            root.right=new TreeNode();   
                               
                        }   
                        insert(newSpecies,root.right);     
                           
                    }else{   
                           
                        root.count++;   
                        total++;   
                           
                    }   
                       
                }   
                   
            }   
               
        }   
           
        public void travelTree(TreeNode root){   
               
            if(root==null){   
                   
                return;   
                   
            }   
            travelTree(root.left);   
            System.out.print(root.species+" ");   
            double p=(double)root.count*100/total;   
            System.out.printf("%.4f", p);   
            System.out.println();   
            travelTree(root.right);   
               
        }   
           
    }   
       
    public static void main(String[] args){   
           
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));   
        Tree t=new Tree();   
        String s;   
           
        try {   
            while((s=reader.readLine())!=null){   
  
                t.insert(s,t.root);   
  
            }   
  
        } catch (IOException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        }   
        t.travelTree(t.root);   
           
    }   
  
}  

							
Posted in poj | Leave a comment

Poj Solution 2415

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

#include<iostream>
#include"queue"
#include"algorithm"

using namespace std;

char edge[51][51];
int sign[51][51][51],n,p1,p2,p3;
struct node
{
    int p[3];
    int steps;
};
queue<node> q;

bool init()
{
    int i,j,k;
    cin>>n>>p1>>p2>>p3;
    if(n==0)return 0;
    
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
        cin>>edge[i][j];

    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    for(k=0;k<n;k++)
        sign[i][j][k]=0;
    
    while(!q.empty())
        q.pop();

    return 1;
}
inline void set(int a,int b,int c,int v)
{
    sign[a][b][c]=sign[a][c][b]=sign[b][a][c]=sign[b][c][a]=sign[c][a][b]=sign[c][b][a]=v;
}

int doit()
{
    int i,j;
    node nd,ndt;
    
    nd.p[0]=p1-1;    nd.p[1]=p2-1;    nd.p[2]=p3-1;
    nd.steps=1;

    set(nd.p[0],nd.p[1],nd.p[2],1);
    q.push(nd);
    
    while(!q.empty())
    {
        nd=q.front();q.pop();
        if(nd.p[0]==nd.p[1]&&nd.p[1]==nd.p[2])
            return sign[nd.p[0]][nd.p[1]][nd.p[2]];
        
        for(i=0;i<n;i++)
        {
            for(j=0;j<3;j++)
            if(!sign[i][nd.p[(j+1)%3]][nd.p[(j+2)%3]]&&
                edge[nd.p[j]][i]==edge[nd.p[(j+1)%3]][nd.p[(j+2)%3]])
            {
                set(i,nd.p[(j+1)%3],nd.p[(j+2)%3],nd.steps+1);
                ndt=nd;
                ndt.steps++;
                ndt.p[j]=i;
                q.push(ndt);
            }
        }
    }
    return 0;
}

int main()
{
    int ans;
    while(init())
    {
        ans=doit();
        if(!ans)cout<<"impossible"<<endl;
        else cout<<ans-1<<endl;
    }
    return 0;
}


							
Posted in poj | Leave a comment

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)
{
    int i,j;

    for(i=n/2-1;i<n-1;i++)
    for(j=0;j<26;j++)
        best[i][j]=2;


    for(i=0;i<n;i++)
    {
        best[(i+n-2)/2][m[i][k]-'A']--;
        min [(i+n-2)/2]=m[i][k]-'A';
    }
    
    for(i=n/2-2;i>=0;i--)
    {
        min[i]=0;
        for(j=0;j<26;j++)
        {
            best[i][j]=min_int(best[i*2+1][min[i*2+1]]+1,best[i*2+1][j])
                      +min_int(best[i*2+2][min[i*2+2]]+1,best[i*2+2][j]);
            if(best[i][j]<best[i][min[i]])min[i]=j;
        }
    }
}            
int main()
{
    int i,ans;
    while(init())
    {
        ans=0;
        if(n==1)strcpy(answer,m[0]);
        else for(i=0;i<l;i++)
        {
            doit(i);
            ans+=best[0][min[0]];
            answer[i]=min[0]+'A';
        }
        answer[l]='';
        printf("%s %dn",answer,ans);
    }
    return 0;
}
							
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 = scan.nextBigInteger();   
            if (a.compareTo(BigInteger.ONE) == -1 && b.compareTo(BigInteger.ONE) == -1) {   
                break;   
            }   
            int count = 0;   
            BigInteger[] f = new BigInteger[1000000];   
            f[0] = BigInteger.ONE;   
            f[1] = BigInteger.ONE.add(BigInteger.ONE);   
            for (int i = 0; i < f.length; i++) {   
                if (i >= 2) {   
                    f[i] = f[i - 1].add(f[i - 2]);   
                }   
                if (f[i].compareTo(b)==1) {   
                    break;   
                }   
                if (!(f[i].compareTo(a)==-1)) {   
                    count++;   
                }   
            }   
            System.out.println(count);   
        }   
    }   
}   


							
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 if ((jj & (1 << s)) == 0)//表示第J列能放1/0 
    {
        dfs(i, j, jj | (1 << s), s + 1);//放1 
        if (s < m - 1 && (jj & (1 << (s + 1))) == 0) dfs(i, j, jj, s + 2);//放0(横占2格) 
    }
    else//表示此处只能放0 
       dfs(i, j, jj & ~(1 << s), s + 1);//(jj & (1 << s)这个位置已经被占 
}
    
 public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  while (sc.hasNext())
  {
      n=sc.nextInt();
      m=sc.nextInt();
      if(n+m==0) break;
      for(int i=0;i< f.length;i++)
        Arrays.fill(f[i],0);

      f[0][0] = 1;
      for (int i = 0; i < n; i++)
        for (int j = 0; j < (1 << m); j++)
          if (f[i][j]!=0) //剪枝(为0没有必要考虑) 
            dfs(i, j, j, 0);
      System.out.printf("%dn", f[n][0]);//不占n行也即0~n-1放满的方法数 
   }
 }
}
							
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 *= (i - 1);
            while (n % i == 0)
            {
                n /= i;
                ret *= i;
            }
        }
    if (n > 1)
        ret *= (n - 1);
    return ret;
}


 public static void main(String[] args) {   
    Scanner sc = new Scanner(System.in);   
   
    while(sc.hasNext()){
     int n=sc.nextInt();
     if(n==0) break;
     System.out.println(eular(n));
   }
  }
}  

							
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 <= sublen; ++i) {
        while (j > 0 && substr[j + 1] != substr[i])
            j = next[j];
        if (substr[j + 1] == substr[i])
            ++j;
        next[i] = j;
    }
}
 
int main() {

    while (gets(substr + 1)) {
        if (substr[1] == '.')
            break;
        sublen = strlen(substr + 1);
        getNext();
        if (sublen % (sublen - next[sublen]) == 0)
            printf("%dn", sublen / (sublen - next[sublen]));
        else
            puts("1");
    }
    return 0;
}
							
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 * d * d * Math.PI / 4 - v);
        double d1 = Math.pow(D*D*D-6*V/Math.PI,1.0/3);
        System.out.printf("%.3f%n", d1);
    }
  }
}

							
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; i < kwNum; i++)   
        {   
            String[] str = cin.nextLine().split(" ");   
            hm.put(str[0], str[1]);   
        }   
           
        for(int i = 0; i < descNum; i++)   
        {   
            StringBuffer sb = new StringBuffer();   
            String job = null;   
            int salary = 0;   
               
            while(true)   
            {   
                job = cin.nextLine();   
                if(job.equals("."))   
                    break;   
                else  
                    sb.append(job + " ");   
            }   
            job = sb.toString().trim();   
               
            if(job.length() == 0)   
            {   
                System.out.println("0");   
                continue;   
            }   
            int eIndex = 0;   
            String word = null;   
            while(true)   
            {   
                eIndex = job.indexOf(" ");   
                if(eIndex == -1 || eIndex == 0)   
                {   
                    if(hm.containsKey(job))   
                        salary += Integer.valueOf((String)hm.get(job));   
                    break;   
                }   
                word = job.substring(0, eIndex);   
                job = job.substring(eIndex + 1).trim();   
                   
                if(hm.containsKey(word))   
                    salary += Integer.valueOf((String)hm.get(word));   
            }   
            System.out.println(salary);   
        }   
  
    }   
  
}  


							
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;//前半部分和后半部分对称
    l/=10;
   }
   printf("%I64d",k);//输出前半部分
   for(i=0;i<j;i++)//输出后半部分
    printf("%d",c[i]);
   printf("n");
}
return 0;
}
							
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)
    {
        for(j=0;j<m;j++)sign[j]=0;
                
        s=1;link=-1;

        from[0]=-1;
        q[0]=size-1;
        p_m[size-1]=i;
        
        for(t=0;t<s;t++)
        {
            now=q[t];
            for(j=0;j<m;j++)
            {
                if(w[p_m[now]][j]!=null&&sign[j]==0)
                {
                    sign[j]=1;
                    q[s]=j;
                    from[s++]=t;
                    
                    if(p_m[j]==-1)
                    {
                        link=s-1;
                        break;
                    }
                }
            }
            
            if(j<m)break;
        }

        if(t<s)
        {
            while(from[link]!=-1)
            {
                h=from[link];
                p_m[q[link]]=p_m[q[h]];
                
                p_n[p_m[q[h]]]=q[link];
                link=h;
            }

        }
        else
        {
            for(j=0;j<n;j++)setn[j]=0;
            
            for(j=0;j<m;j++)setm[j]=0;

            for(j=0;j<s;j++)
            {
                setn[p_m[q[j]]]=1;
                if(j)setm[q[j]]=1;
            }
    
            return 0;
        }
    }


    for(i=0;i<n;i++)
    {
        if(p)p[i]=p_n[i];
        
    }
    
    return 1;
}


valuetype maxvaluematch(int l,valuetype w[][size])
{
    valuetype x[size],y[size],af;
    bool edge[size][size],setn[size],setm[size];
    int i,j,p[size];
    
    for(i=0;i<l;i++)y[i]=0;

    for(i=0;i<l;i++)
    {
        x[i]=0;
        for(j=0;j<l;j++)
            if(w[i][j]>x[i])x[i]=w[i][j];
    }

    do
    {    
        for(i=0;i<l;i++)
        for(j=0;j<l;j++)
            if(w[i][j]==x[i]+y[j])edge[i][j]=1;
            else edge[i][j]=0;

        if(maxmatch(l,l,edge,p,setn,setm))
        {
            valuetype answer=0;
            for(i=0;i<l;i++)
                answer+=w[i][p[i]];
            
            return answer;
        }
        
        af=0;
        for(i=0;i<l;i++)
        {
            if(setn[i])
                for(j=0;j<l;j++)
                    if(!setm[j])
                    {
                        if(af==0||x[i]+y[j]-w[i][j]<af)af=x[i]+y[j]-w[i][j];
                    }
        }

        for(i=0;i<l;i++)if(setn[i])x[i]-=af;

        for(j=0;j<l;j++)if(setm[j])y[j]+=af;
    }while(1);

    return 0;
}

//////////////////////////////////////////////////////////////


int v;
int e[20][20],n,cas;
int p[20];

void del_copy(int a[20][20],int b[20][20],int c,int d,int sn)
{
    int i,j;
    
    for(i=0;i<c;i++)
    {
        for(j=0;j<d;j++)
            a[i][j]=b[i][j];
        for(j=d+1;j<sn;j++)
            a[i][j-1]=b[i][j];
    }
    
    for(i=c+1;i<sn;i++)
    {
        for(j=0;j<d;j++)
            a[i-1][j]=b[i][j];
        for(j=d+1;j<sn;j++)
            a[i-1][j-1]=b[i][j];
    }
}

void print()
{
    int sign[20],i,j,k;
    cas++;
    printf("Best Pairing %dn",cas);
    
    
    for(i=0;i<n;i++)
        sign[i]=1;
    
    for(i=0;i<n;i++)
    {
        for(j=0,k=0;j<n;j++)
        {
            if(sign[j])k++;
            if(k==p[i]+1)break;
        }
        
        printf("Supervisor %d with Employee %dn",i+1,j+1);
        sign[j]=0;
    }
}



void search(int b,int sn,int e0[20][20],int value)
{
    int temp,ee[20][20];
    if(sn==0)print();
    
    for(p[b]=0;p[b]<sn;p[b]++)
    {
        del_copy(ee,e0,0,p[b],sn);
        temp=value+maxvaluematch(sn-1,ee)+e0[0][p[b]];
        if(temp==v)
            search(b+1,sn-1,ee,value+e0[0][p[b]]);

    }
    return;
}



int main()
{
    int t,i,j,s,k;
    cin>>t;
//    freopen("txt.txt","w",stdout);
    for(k=0;k<t;k++)
    {
        cin>>n;
        for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            e[i][j]=0;

        for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        {
            cin>>s;
        //    e[i][s-1]+=-j;
            e[s-1][i]+=-j;
        }

        for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        {
            cin>>s;
        //    e[s-1][i]+=-j;
            e[i][s-1]+=-j;
        }

        v=maxvaluematch(n,e);
        
        
        printf("Data Set %d, Best average difference: %.6lfn",k+1,(double)-v/2/n);
        
        cas=0;
        search(0,n,e,0);
        printf("n");
    }
    return 0;
}
							
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;
   while((s=br.readLine())!=null&&!s.equals("0")){
        ss = s.split(" ",6);
        n=parseInt(ss[0]);
        m=parseInt(ss[1]);
        y1=parseInt(ss[3]);
        y2=parseInt(ss[5]);
        LineSet ls = new LineSet(n);
        Statistics st = new Statistics();
        for(i=0;i< n;i++){
          s=br.readLine();
       ss=s.split(" ",2);
       t=parseInt(ss[0]);
       p=new Point(t,y1);
       p2=new Point(parseInt(ss[1]),y2);
       ls.add(t,new Line(p,p2));
     }
     for(i=0;i< m;i++){
       s=br.readLine();
       ss=s.split(" ",2);
       t=ls.binFind(new Point(parseInt(ss[0]),parseInt(ss[1])));
       st.register(t);
    }
        st.printFre();
        }
        br.close();
    }

  static int parseInt(String s){
   if(s.startsWith("-"))
      return -parseInt(s.substring(1));
   int t = 0;
   for(char ch: s.toCharArray()){
      t *= 10;
      t += ch-'0';
   }
   return t;
  }
}

class Statistics{
 Map< Integer,Integer> map=new TreeMap< Integer,Integer>();
 Map< Integer,Integer> fre=new TreeMap< Integer,Integer>();
 void register(int x){
   Integer t = map.get(x);
   if(t==null){
    map.put(x, 1);
    Integer t2 = fre.get(1);
    if(t2==null){
         fre.put(1, 1);
    }
    else{
      fre.put(1, t2+1);
    }
    }
    else{
    map.put(x, t+1);
    Integer t2 = fre.get(t);
    fre.put(t, t2-1);
    t2 = fre.get(t+1);
    if(t2==null){
      fre.put(t+1, 1);
    }
    else{
      fre.put(t+1, t2+1);
    }
    }
  }

void printFre(){
  System.out.println("Box");
  int t;
  for(Map.Entry< Integer, Integer> me: fre.entrySet()){
    if((t=me.getValue())>0)
         System.out.println(me.getKey()+": "+t);
  }
 }
}
class LineSet{
  Map< Integer,Line> mil;
  Object[] lines;
  LineSet(int n){
    mil = new TreeMap< Integer,Line>();
  }

  void add(int key, Line line){
    mil.put(key, line);
  }
    //Binary search method
  int binFind(Point p){
    lines=mil.values().toArray();
    int i=binFind0(p,0,lines.length-1);
    return i;
  }

 private int binFind0(Point p, int left, int right){
   if(p.compareTo((Line)lines[left])< 0)
    return left;
   if(p.compareTo((Line)lines[right])>0)
    return right+1;
  int mid = (left+right)/2;
  if(p.compareTo((Line)lines[mid])< 0)
    return binFind0(p,left,mid-1);
  else return binFind0(p,mid+1,right);
 }
}

class Point implements Comparable< Line>{

 int x;
 int y;
 Point(int x,int y){
   this.x=x;
   this.y=y;
  }
  @Override
  public int compareTo(Line line) {
    return x*line.x0+y*line.y0+line.c;
  }
}

class Line{
  //x0 must be positive
  int x0;
  int y0;
  int c;
  //p must be up, p2 must be down
  Line(Point p, Point p2){
    x0=p.y-p2.y;
    y0=p2.x-p.x;
    c=-(p.x*x0+p.y*y0);
  }

  @Override
   public String toString(){
    return "Line: "+x0+"x+"+y0+"y+"+c+"=0.";

  }
}

							
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;
  for(i=0;i< m;i++)
  {
     u=sc.nextInt();
     v=sc.nextInt();
     l=sc.nextInt();
     u--;
     v--;
     if(p[u][v]>l)p[u][v]=p[v][u]=l;
  }
  for(i=0;i< n;i++)
    {
    dis[i]=p[0][i];
    ne[i]=0;
     }
  ne[0]=-1;
  for(i=0;i< n-1;i++)
  {
    int min=1000000001,tag=-1;
    for(j=0;j< n;j++)
    if(ne[j]!=-1&&dis[j]< min)
    {
       min=dis[j];
       tag=j;
     }
     ne[tag]=-1;
     if(dis[tag]>max)max=dis[tag];
     for(j=0;j< n;j++)
       if(ne[j]!=-1&&p[tag][j]< dis[j])
       {
        dis[j]=p[tag][j];
        ne[j]=tag;
       }
    }
   System.out.printf("%d",max);
   }
}
							
Posted in poj | Leave a comment