zoukankan      html  css  js  c++  java
  • UVALive 4794 Sharing Chocolate

                      Sharing Chocolate
    Chocolate in its many forms is enjoyed by millions of people around the world every day. It is a truly universal candy available in virtually every country around the world.

    You find that the only thing better than eating chocolate is to share it with friends. Unfortunately your friends are very picky and have different appetites: some would like more and others less of the chocolate that you offer them. You have found it increasingly difficult to determine whether their demands can be met. It is time to writte a program that solves the problem once and for all!


    Your chocolate comes as a rectangular bar. The bar consists of same-sized rectangular pieces. To share the chocolate you may break one bar into two pieces along a division between rows or columns of the bar. You or the may then repeatedly break the resulting pieces in the same manner. Each of your friends insists on a getting a single rectangular portion of the chocolate that has a specified number of pieces. You are a little bit insistent as well: you will break up your bar only if all of it can be distributed to your friends, with none left over.


    For exampla, Figure 9 shows one way that a chocolate bar consisting of x 4 pieces can be split into 4 parts that contain 6, 3, 2, and 1 pieces respectively, by breanking it 3 times (This corresponds to the first sample input.)

    epsfbox{p4794.eps}

    Input

    The input consists of multiple test cases each describing a chocolate bar to share. Each description starts with a line containing a single integer n(1$ le$n$ le$15), the number of parts in which the bar is supposed to be split. This is followed by a line containing two integers x and y(1$ le$xy$ le$100), the dimensions of the chocolate bar. The next line contains n positive integers, giving the number of pieces that are supposed to be in each of the n parts.

    The input is terminated by a line containing the integer zero.

    Output

    For each test case, first display its case number. Then display whether it is possible to break the chocolate in the desired way: display ``Yes" if it is possible, and ``No" otherwise. Follow the format of the sample output.

    Sample Input

    4

    3 4

    6 3 2 1

    2

    2 3

    1 5

    Sample Output

     

    Case 1: Yes

    Case 2: No

     

    因为 n 只有15嘛 。。 那么很容易想到一个状态就是 dp[a][b][st] .. 表示 a * b 这么大的一个矩阵

    能否组成集合 st 里面的元素..这样.. bool型的 dp[a][b][st] = 1 表示可以 0 表示不可以。

    状态就有 x*y* 2^n 个 ,, 转移要用 O(x + y ) .... 花费过大..

     

    那么先处理一个sum[st] 表示 。这个st这个集合的巧克力的面积和。

    那么转移的时候两个子集合的面积和已是确定的...

    那么在切一刀的情况下:

    sum[st0] %x == 0 才能竖切..

    sum[st0] %y == 0 才能横切..

    既然 sum[st0]满足上述条件 ....sum[st^st0]也必然满足...因为已有sum[st] %x == 0 && sum[st] % y == 0.

    那么就可先降一维..用dp[a][st]表示 ....集合st是否可由宽为a的矩阵构成 .. 要转移的状态数也变为O(1)lor~

    那里递归bitcount 也要学学~

    初始条件就是 x * y == sum[(1<<n)- 1]...

    然后进行一个记忆话搜索... 

     

     超时:

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 105 ;
    const int M = (1<<16);
    bool dp[N][N][M] , vis[N][N][M];
    int sum[M] , n , x , y ,area[N];
    
    bool DP( int x , int y , int st )
    {
        if( x > y )swap( x , y );
        if( vis[x][y][st] ) return dp[x][y][st];
        vis[x][y][st] = true;
        for( int st0 = st ; st0 ; st0 = (st0-1)&st ){
            for( int i = 1 ; i <= ( x - 1 )/ 2 ; ++i ) {
                if( sum[st0] == i * y && sum[st^st0] == (x-i) * y && DP(i,y,st0) && DP(x-i,y,st^st0) ) return dp[x][y][st] = true;
                if( sum[st^st0] == i * y && sum[st0] == (x-i) * y && DP(i,y,st^st0) && DP(x-i,y,st0) ) return dp[x][y][st] = true;
            }
            for( int j = 1 ; j <= ( y -1 ) / 2 ; ++j ){
                if( sum[st0] == x * j && sum[st^st0] == x*(y-j) && DP(x,j,st0) && DP(x,y-j,st^st0)) return dp[x][y][st] = true;
                if( sum[st^st0] == x * j && sum[st0] == x*(y-j)&& DP(x,j,st^st0) && DP(x,y-j,st0)) return dp[x][y][st] = true;
            }
        }
        return dp[x][y][st] = false;
    }
    
    void init()
    {
        memset( dp , false , sizeof dp );
        memset( vis , false , sizeof vis );
        memset( sum , 0 ,sizeof sum );
    }
    
    void run()
    {
        cin >> x >> y ;
        init();
        for( int i = 0 ; i < n ; ++i ) {
            cin >> area[i] ;
            if( area[i] > x * y ) { cout << "No" << endl; return ;}
            for( int  a = 1 ; a * a <= area[i] ; ++a ){
                if( area[i] % a == 0 ){ int b = area[i] / a; dp[a][b][(1<<i)] = vis[a][b][(1<<i)] = true ;  }
            }
        }
        for( int st = 0 ; st < (1<<n); ++st ){
            for( int i = 0 ; i < 16 ; ++i ){
                if( st & (1<<i) ) sum[st] += area[i] ;
            }
        }
        int ALL = ( 1 << n ) - 1 ;
        if( sum[ALL] != x * y || !DP( x , y , ALL ) )cout << "No" << endl;
        else cout << "Yes" <<endl ;
    
    }
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif
        int cas = 1 ;
        while( cin >> n && n ) { cout << "Case "<< cas++ <<": "; run(); }
    }
    View Code

    正解:
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 105 ;
    const int M = (1<<16);
    bool dp[N][M] , vis[N][M];
    int sum[M] , n , x , y ,area[N];
    int bitcnt( int st ){ return st == 0 ? 0 : bitcnt(st/2) + (st&1); }
    bool DP( int x ,  int st )
    {
        if( vis[x][st] ) return dp[x][st];
        vis[x][st] = true;
        int y = sum[st] / x ;
        if( bitcnt(st) == 1 ) return dp[x][st] = true;
        for( int st0 = st ; st0 ; st0 = (st0-1)&st ){
            if( sum[st0] % x == 0 && DP( min( x , sum[st0]/x ),st0) && DP(min(x,sum[st^st0]/x),st^st0)) return dp[x][st] = true;
            if( sum[st0] % y == 0 && DP( min( y , sum[st0]/y ),st0) && DP(min(y,sum[st^st0]/y),st^st0)) return dp[x][st] = true;
        }
        return dp[x][st] = false;
    }
    
    void init()
    {
        memset( dp , false , sizeof dp );
        memset( vis , false , sizeof vis );
        memset( sum , 0 ,sizeof sum );
    }
    
    void run()
    {
        cin >> x >> y ;
        init();
        for( int i = 0 ; i < n ; ++i ) cin >> area[i] ;
        for( int st = 0 ; st < (1<<n); ++st ){
            for( int i = 0 ; i < 16 ; ++i ){
                if( st & (1<<i) ) sum[st] += area[i] ;
            }
        }
        int ALL = ( 1 << n ) - 1 ;
        if( sum[ALL] != x * y || !DP( x , ALL ) )cout << "No" << endl;
        else cout << "Yes" <<endl ;
    }
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif
        int cas = 1 ;
        while( cin >> n && n ) { cout << "Case "<< cas++ <<": "; run(); }
    }
    View Code


    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    NPM (node package manager) 入门
    win10 环境 gitbash 显示中文乱码问题处理
    javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈
    Javascript 的执行环境(execution context)和作用域(scope)及垃圾回收
    Centos 下 mysql root 密码重置
    执行 $Gulp 时发生了什么 —— 基于 Gulp 的前端集成解决方案(二)
    Java I/O输入输出流详解
    反射---Java高级开发必须懂的
    细说Java多线程之内存可见性
    全面解析java注解
  • 原文地址:https://www.cnblogs.com/hlmark/p/4059654.html
Copyright © 2011-2022 走看看