NYOJ 491 幸运三角形(bitset)
1 #include<iostream>
2 #include<cmath>
3 #include<cstdio>
4 #include<bitset>
5 using namespace std;
6 int a[22],n;
7 int b[] = {0,0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757};
8
9 void slove()
10 {
11 freopen("out.txt","w",stdout);
12 int i, j, s1, s2, n,sum, T;
13 n = 1;
14 while(n < 20)
15 {
16 T = pow(2.0, n);
17 sum = 0;
18 while(T--)
19 {
20 bitset<20> b(T);
21 s1 = s2 = 0;
22 for(i=0; i < n; ++i)
23 {
24 a[i] = b[i];
25 if(a[i]) ++s1;
26 else ++s2;
27 }
28 for(j = 0; j < n; ++j)
29 {
30 for(i = 0; i < n-j-1; ++i)
31 {
32 if(a[i] == a[i+1])
33 { a[i] = 1; ++s1; }
34 else
35 { a[i] = 0; ++s2; }
36 }
37 }
38 if(s1 == s2)
39 ++sum;
40 }
41 b[n++] = sum;
42 cout<<","<<b[n-1];
43 }
44 cout<<endl;
45 }
46
47 int main()
48 {
49 // slove();
50 while(~scanf("%d",&n))
51 printf("%d\n",b[n]);
52 return 0;
53 }
2 #include<cmath>
3 #include<cstdio>
4 #include<bitset>
5 using namespace std;
6 int a[22],n;
7 int b[] = {0,0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757};
8
9 void slove()
10 {
11 freopen("out.txt","w",stdout);
12 int i, j, s1, s2, n,sum, T;
13 n = 1;
14 while(n < 20)
15 {
16 T = pow(2.0, n);
17 sum = 0;
18 while(T--)
19 {
20 bitset<20> b(T);
21 s1 = s2 = 0;
22 for(i=0; i < n; ++i)
23 {
24 a[i] = b[i];
25 if(a[i]) ++s1;
26 else ++s2;
27 }
28 for(j = 0; j < n; ++j)
29 {
30 for(i = 0; i < n-j-1; ++i)
31 {
32 if(a[i] == a[i+1])
33 { a[i] = 1; ++s1; }
34 else
35 { a[i] = 0; ++s2; }
36 }
37 }
38 if(s1 == s2)
39 ++sum;
40 }
41 b[n++] = sum;
42 cout<<","<<b[n-1];
43 }
44 cout<<endl;
45 }
46
47 int main()
48 {
49 // slove();
50 while(~scanf("%d",&n))
51 printf("%d\n",b[n]);
52 return 0;
53 }
先暴力解开答案,然后存入数组打表即可。。。做的时候突然想到可以用bitset<>...,很方便的哦。。。