Codevs 1005 Birthday Present

Source: Internet
Author: User

1005 Birthday Presenttime limit: 1 sspace limit: 128000 KBtitle level: Golden GoldTitle Description Description

September 12 is Komatsu's friend Xiaohan's birthday. Komatsu knew Xiaohan particularly like butterflies, so decided to fold the butterfly as a birthday gift for Xiaohan. He came to PK University, the largest underground supermarket, in the supermarket, Komatsu found n kinds of paper can be used to origami book. Each type of book has a number of different colors of paper, of course, the same type of book must be exactly the same , and the different types of the book is not necessarily the same. He counted, there are a total of n different types of books that can be used for origami, each of which has bi , and all the paper has a color of m which is Xiaohan's favorite. Komatsu hopes that the number of butterflies in each color he folds is the same. In other words, Komatsu must fold the m*k Butterfly, where K represents the number of butterflies per color, and this number is determined by Komatsu itself. But Komatsu can not waste paper, that is to say, he bought the book, as long as the color of the paper Xiaohan like to be folded into a butterfly. So the question came, each type of book should buy how many copies, in order to fold out the m*k butterfly it? Of course, because Komatsu is a very lazy person, he would like to fold the number of butterflies the less the better, as long as the expression of the mind can be (that is, not 1 or fold). And if Komatsu had to fold more than 1000 butterflies in order to meet the requirements, then he would rather change a gift scheme.

Enter a description Input Description

The first line of input contains 2 integers n(1≤n8),m(1≤m10). Indicates that there are n different types of books and m -type Xiaohan like colors. Next a matrix of N*m . The integer AIJ in column J of Line I contains the Xiaohan favorite color J in the Book of type i is AIJ(1 ≤aij 100) Zhang. The next row of n integers b1 to bn, indicating how many copies of each color's book are in the supermarket (1≤bi5).

Output description Output Description

The output contains an integer that indicates the minimum number of butterflies that need to be folded, and if the number exceeds 1000, the output is "alternative!". (because there may be many options for buying a book, so there is no need to export a specific plan)

Sample input Sample Input

2 3

2 1 2

4 8 4

5 5

Sample output Sample Output

36

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 inta[Ten][ A],f[ A],sum[ A],lim=1001;//limit the number of boundaries the topic requires no more than5 intn,m;6 voidDfsints) {7      for(intI=0; i<=f[s];i++){8          for(intk=1; k<=m;k++)9Sum[k]+=a[s][k]*i;//The number of books for each color;Ten         if(s<n) DFS (s+1); One         Else{ A             intflag=0; -              for(intk=2; k<=m;k++){ -                 if(sum[k]!=sum[k-1]){ theflag=1; Break; -                 } -             } -             if(!flag)//The number of books per color is the same SUM[1..N] can be +                 if(sum[1]*m<lim&&sum[1]>0) -Lim=min (lim,sum[1]*m); +         } A          for(intk=1; k<=m;k++)//Backtracking atsum[k]-=a[s][k]*i; -     } - } - intMain () { -scanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++)//The integer AIJ in column J of line I contains the Xiaohan favorite color J paper with AIJ (1≤AIJ≤100) sheet in the first type of book in          for(intj=1; j<=m;j++) -scanf"%d",&a[i][j]); to      for(intI=1; i<=n;i++)//Number of Notebooks +scanf"%d", f+i); -Dfs1);//start with the first book . the     if(lim<= +) printf ("%d\n", Lim); *     Elseprintf"alternative!\n"); $     return 0;Panax Notoginseng}

Codevs 1005 Birthday Present

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.