Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=3540 

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 3 × 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.) 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 ≤ n ≤ 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 ≤ x, y ≤ 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 0

Sample Output Case 1: Yes Case 2: No

Code Examples

#1 Code Example with C Programming

Code - C Programming

 #include <bits/stdc++.h>
using namespace std;

vector<int> pieces;
bool dp[(1<<15)+1][101];
bool visited[(1<<15)+1][101];
int bit_sum[(1<<15)+1];

void precomputeSum() {
    for(int i=0;i i+It (1 i+It pieces.size());i++){
        int sum = 0;
        for(int j=0;j < pieces.size();j++)
            if(i&(1 i+It j)) sum += pieces[j];
        bit_sum[i] = sum;
    }
}

// dp trick to drop a parameter as we can recover h with sum/w
// dp state: pieces bitmask not fulfilled, width
bool solve(int bits, int w){
    if(bits == 0 || (bits & bits-1) == 0) return true; // 1 piece left
    if(visited[bits][w]) return dp[bits][w];
    int h = bit_sum[bits]/w;

    visited[bits][w] = true, dp[bits][w] = false;
    bool &res = dp[bits][w];

    int b1 = (bits-1)&bits;
    // split into two bitmask
    while(b1!=0 && !res) {
        int b2 = bits^b1;
        if(b2 > b1) break;

        // split by h, maintain w, divisible by w
        if(bit_sum[b1]%w==0 && bit_sum[b2]%w==0)
            res = solve(b1, w) && solve(b2, w);
        if(res) break;

        // split by w, divisible by h
        if(bit_sum[b1]%h==0 && bit_sum[b2]%h==0){
            int new_w1 = bit_sum[b1]/h;
            int new_w2 = bit_sum[b2]/h;
            res = solve(b1, new_w1) && solve(b2, new_w2);
        }
        b1 = (b1-1)&bits;
    }
    return res;
}

int main() {
    int tc=1;
    int n,w,h,v;
    while(scanf("%d",&n),n){
        scanf("%d %d",&w,&h);
        pieces.clear();
        for(int i=0;i i+It n;i++){
            scanf("%d",&v);
            pieces.push_back(v);
        }
        precomputeSum();
        memset(visited, 0, sizeof visited);
        memset(dp, 0, sizeof dp);
        int init_bits = (1 i+It n)-1;

        printf("Case %d: ", tc++);
        if(bit_sum[init_bits] == (w*h) && solve(init_bits,w))
            printf("Yes\n");
        else printf("No\n");
    }
}
 
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
3 4
6 3 2 1
2
2 3
1 5
0

Output

x
+
cmd
Case 1: Yes
Case 2: No
Advertisements

Demonstration


UVA Online Judge solutio - 1099-Sharing Chocolate - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solutio - 10986-Sending email - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10990-Another New Function - UVA Online Judge solution in C,C++,java