Oleg and Little Ponies

Source: Internet
Author: User
Tags bitset gcd

Oleg and Little Poniestime limit:0.9 second
Memory limit:64 mblittle Boy Oleg loves the cartoon My Little Pony. How it cannot was loved, because there is friendship, magic and colored horses! For the past several months Oleg have been begging from he parents a real pony, but they is just ready-buy him only co Llectible figures of cartoon characters. With these figures Oleg recreates the best episodes of My Little Pony on his desk. Sometimes he realizes that he had already all the key characters for the next episode, and begins to feel The DesireTo immediately buy the missing figures for this episode. For example, if Oleg have on hand Twilight Sparkle and Spike, he life won't be sweet without princess Celestia. It may happen the new figures would cause new desires: Having three above-mentioned figures, Oleg would want nightmare moon.for convenience, let's number all the figures with I Ntegers from 1 to N. Then the Oleg ' s DesireWould be described by sets of numbers { a1,..., a k} and { b1,..., b T}, which means that if he already have figures with numbers a1,..., a k, he also wants figures with numbers b1,..., b T. Oleg ' s parents in order to distract him from his desires of real pony is ready to buy him as many figures as he wants. But they want to buy a set of figures that'll satisfy all The desiresof Oleg, in a single purchase. Of course, parents would not buy the extra figures. What figures would Oleg have after purchase? Inputthe First line contains integers Nand mThat is the number of figures and the number of Oleg ' s desires (1≤ N≤1000; 0≤ m≤4000). The following mLines describe the desires. Each desire was given by and sets, separated by a space. A set is a string of Ncharacters, each of which is "0" or "1". The figure with number Iis in the set I-th character of the string is "1". The last line contains the set of figures, which Oleg already have, in the same format. Outputin a single line output a set of figures, which Oleg'll has after purchase. In other words, it's the Union of the set of the existing figures and the set of figures bought by parents. Output format is the same as in the input. Sample
input Output
6 4111000 101000110000 111000010000 100000000010 000001010100
111100
Notesin The example Oleg has already the figures 2 and 4. First, he wants the figure 1 (the third desire). If He gets it, he'll want the figure 3 (the second desire). If you just buy him the figures 1 and 3, Oleg won't want anything more (the right part of the first desire consists of Already existing figures, and the left side of the last desire are not fulfilled). Thus, after purchase Oleg'll has figures with the numbers 1, 2, 3 and 4.Analysis: basic usage of bitset;Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<bitset>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineVI vector<int>#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)#definePII pair<int,int>#defineLson L, Mid, Ls[rt]#defineRson mid+1, R, Rs[rt]#defineSYS system ("pause")Const intmaxn=1e3+Ten;using namespacestd;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p%mod;p=p*p%mod;q>>=1;}returnF;} InlinevoidUmaxint&p,intQ) {if(P&LT;Q) p=Q;} InlinevoidUmin (int&p,intQ) {if(P&GT;Q) p=Q;} inline ll read () {ll x=0;intf=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}intn,m,k,t;BOOLFlag;Set<int>OK;VI tmp;CharB[maxn];bitset<maxn>a[maxn<<2],c[maxn<<2],ans;inlinevoidGaointp) {    if((Ans&a[p])!=a[p])return; Ans|=C[p];    TMP.PB (P); Flag=true;}intMain () {inti,j; scanf ("%d%d",&n,&m); Rep (I,1, M) {scanf ("%s", B);        Ok.insert (i);  for(j=0; b[j];j++)if(b[j]=='1') A[i].Set(j); scanf ("%s", B);  for(j=0; b[j];j++)if(b[j]=='1') C[i].Set(j); } scanf ("%s", B);  for(j=0; b[j];j++)if(b[j]=='1') ans.Set(j); Rep (I,1, M) Gao (i);  for(intx:tmp) ok.erase (x);  while(1) {flag=false;  for(intX:ok)        {Gao (x); }        if(!flag) Break;  for(intx:tmp) ok.erase (x);    Tmp.clear (); }     for(i=0; i<n;i++) cout<<Ans[i]; printf ("\ n"); return 0;}

Oleg and Little Ponies

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.