题目链接: http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=584
A.
思路:题目可以看成这样一个模型,把n个物品分成若干组,使得每组只有1个或两个物品的方法总数。令dp[n]表示n个物品的答案,则有:
dp[n] = dp[n-1] + (n - 1) * dp[n - 2]
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#pragma comment(linker, "/STACK:10240000,10240000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility>
using
namespace
std;
#define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
typedef
unsigned
int
uint;
typedef
long
long
LL;
typedef
pair<
int
,
int
> pii;
typedef
vector<
int
> vi;
const
int
dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
const
int
dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
const
int
maxn = 1e6 + 7;
const
int
md = 1000000007;
const
int
inf = 1e9 + 7;
const
LL inf_L = 1e18 + 7;
const
double
pi =
acos
(-1.0);
const
double
eps = 1e-6;
template
<
class
T>T gcd(T a, T b){
return
b==0?a:gcd(b,a%b);}
template
<
class
T>
bool
max_update(T &a,
const
T &b){
if
(b>a){a = b;
return
true
;}
return
false
;}
template
<
class
T>
bool
min_update(T &a,
const
T &b){
if
(b<a){a = b;
return
true
;}
return
false
;}
template
<
class
T>T condition(
bool
f, T a, T b){
return
f?a:b;}
template
<
class
T>
void
copy_arr(T a[], T b[],
int
n){rep_up0(i,n)a[i]=b[i];}
int
make_id(
int
x,
int
y,
int
n) {
return
x * n + y; }
int
ans[maxn];
void
init() {
ans[1] = 1;
ans[2] = 2;
for
(
int
i = 3; i <= 1000000; i ++) {
ans[i] = (ans[i - 1] + (LL)(i - 1) * ans[i - 2]) % md;
}
}
int
main() {
//freopen("in.txt", "r", stdin);
init();
int
T, n;
cin >> T;
rep_up1(cas, T) {
cin >> n;
printf
(
"Case #%d:\n%d\n"
, cas, ans[n]);
}
return
0;
}
|
B.
思路:直接还原原矩阵或者找输入输出的对应关系即可 。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#pragma comment(linker, "/STACK:10240000,10240000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility>
using
namespace
std;
#define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
typedef
unsigned
int
uint;
typedef
long
long
LL;
typedef
pair<
int
,
int
> pii;
typedef
vector<
int
> vi;
const
int
dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
const
int
dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
const
int
maxn = 120000;
const
int
md = 1000000007;
const
int
inf = 1e9 + 7;
const
LL inf_L = 1e18 + 7;
const
double
pi =
acos
(-1.0);
const
double
eps = 1e-6;
template
<
class
T>T gcd(T a, T b){
return
b==0?a:gcd(b,a%b);}
template
<
class
T>
bool
max_update(T &a,
const
T &b){
if
(b>a){a = b;
return
true
;}
return
false
;}
template
<
class
T>
bool
min_update(T &a,
const
T &b){
if
(b<a){a = b;
return
true
;}
return
false
;}
template
<
class
T>T condition(
bool
f, T a, T b){
return
f?a:b;}
template
<
class
T>
void
copy_arr(T a[], T b[],
int
n){rep_up0(i,n)a[i]=b[i];}
int
make_id(
int
x,
int
y,
int
n) {
return
x * n + y; }
vector<vector<
char
> > ch;
char
s[maxn];
int
main() {
//freopen("in.txt", "r", stdin);
int
T, k;
cin >> T;
scanf
(
"%*c"
);
rep_up1(cas, T) {
gets
(s);
scanf
(
"%d%*c"
, &k);
printf
(
"Case #%d:\n"
, cas);
int
n =
strlen
(s);
ch.resize(n / k + 2);
rep_up0(i, n / k + 2) ch[i].resize(k);
int
cur = 0;
rep_up0(i, n % k) {
rep_up0(j, n / k + 1) {
ch[j][i] = s[cur ++];
}
}
for
(
int
i = n % k; i < k; i ++) {
rep_up0(j, n / k) {
ch[j][i] = s[cur ++];
}
}
rep_up0(i, n / k) {
rep_up0(j, k) {
pchr(ch[i][j]);
}
}
rep_up0(i, n % k) {
pchr(ch[n / k][i]);
}
cout << endl;
}
return
0;
}
|
C.
思路:二进制运算和map判重。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#pragma comment(linker, "/STACK:10240000,10240000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility>
using
namespace
std;
#define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
typedef
unsigned
int
uint;
typedef
long
long
LL;
typedef
pair<
int
,
int
> pii;
typedef
vector<
int
> vi;
const
int
dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
const
int
dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
const
int
maxn = 120000;
const
int
md = 1000000007;
const
int
inf = 1e9 + 7;
const
LL inf_L = 1e18 + 7;
const
double
pi =
acos
(-1.0);
const
double
eps = 1e-6;
template
<
class
T>T gcd(T a, T b){
return
b==0?a:gcd(b,a%b);}
template
<
class
T>
bool
max_update(T &a,
const
T &b){
if
(b>a){a = b;
return
true
;}
return
false
;}
template
<
class
T>
bool
min_update(T &a,
const
T &b){
if
(b<a){a = b;
return
true
;}
return
false
;}
template
<
class
T>T condition(
bool
f, T a, T b){
return
f?a:b;}
template
<
class
T>
void
copy_arr(T a[], T b[],
int
n){rep_up0(i,n)a[i]=b[i];}
int
make_id(
int
x,
int
y,
int
n) {
return
x * n + y; }
map<uint,
bool
> mp;
uint a[1010];
int
main() {
//freopen("in.txt", "r", stdin);
int
T, w, x, y, z, n, m;
cin >> T;
rep_up1(cas, T) {
printf
(
"Case #%d:\n"
, cas);
cin >> n >> m;
rep_up0(i, n) {
scanf
(
"%d.%d.%d.%d"
, &w, &x, &y, &z);
a[i] = (uint)w << 24 | x << 16 | y << 8 | z;
}
rep_up0(i, m) {
scanf
(
"%d.%d.%d.%d"
, &w, &x, &y, &z);
uint p = (uint)w << 24 | x << 16 | y << 8 | z;
mp.clear();
int
ans = 0;
rep_up0(i, n) {
uint res = p & a[i];
if
(!mp[res]) {
ans ++;
mp[res] = 1;
}
}
printf
(
"%d\n"
, ans);
}
}
return
0;
}
|
D.
思路:只要先手可以走第一步,那么它一定会走完最后一步,因为之后只需走后手的对称位置。
代码如下: