Hdu oj 3306 The Number of set

Source: Internet
Author: User

Train of Thought: binary, bitwise operation |, to mark the status. For example, if 2, 4, and 6 appear in a set, use the binary number 101010 = (decimal) 2 + 8 + 32 = 42 to record this set !!, Because the m value is <= 14, a maximum of 1 <14 States are available. Use these binary | operations to generate a new set. Then the statistics are used to obtain the answer .. After reading the question solution, it seems very simple, but it is too difficult to think about it .. I really admire the children I think ..

AC code:


[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <iostream>
# Include <cstring>
# Include <queue>
# Include <map>
# Include <algorithm>
Using namespace std;
Int a [1 <15];
Int main ()
{
Int I, j, n, m;
While (~ Scanf ("% d", & n, & m ))
{
Memset (a, 0, sizeof ());
Int k, x, y;
Int ans = 0;
While (n --)
{
Scanf ("% d", & k );
Y = 0;
While (k --)
{
Scanf ("% d", & x );
Y = y | (1 <x-1 );
}
A [y] = 1;
For (I = 0; I <= 1 <14; I ++)
{
If (a [I])
A [I | y] = 1;
}
}
For (I = 1; I <= 1 <14; I ++)
If (a [I]) ans ++;
Printf ("% d \ n", ans );
}
}

# Include <stdio. h>
# Include <stdlib. h>
# Include <iostream>
# Include <cstring>
# Include <queue>
# Include <map>
# Include <algorithm>
Using namespace std;
Int a [1 <15];
Int main ()
{
Int I, j, n, m;
While (~ Scanf ("% d", & n, & m ))
{
Memset (a, 0, sizeof ());
Int k, x, y;
Int ans = 0;
While (n --)
{
Scanf ("% d", & k );
Y = 0;
While (k --)
{
Scanf ("% d", & x );
Y = y | (1 <x-1 );
}
A [y] = 1;
For (I = 0; I <= 1 <14; I ++)
{
If (a [I])
A [I | y] = 1;
}
}
For (I = 1; I <= 1 <14; I ++)
If (a [I]) ans ++;
Printf ("% d \ n", ans );
}
}


 

Related Article

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.