HDU - 1297 - Children’s Queue

题目链接

点此跳转

思路


f[n] :总人数为 n 时的方案总数。

s 为从第一个人开始,在遇到男生前,女生出现的次数。

考虑 f[n]

s = 0 时,方案数为 f[n - 1]

s = 1 时,此方案不合法。

s = 2 时,方案数为 f[n - 3]

以此类推。

所以 f[n] = f[n - 1] + f[n - 3] + f[n - 4] + ... + f[0]

那么 f[n - 2] = f[n - 3] + f[n - 5] + f[n - 6] + ... + f[0]

上面两式合并得到:f[n] = f[n - 1] + f[n - 2] + f[n - 4]

代码
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

#define LL long long
#define mem(a, b) memset(a, b, sizeof a)
#define lowbit(x) (-x&x)
#define IOS ios::sync_with_stdio(false),cin.tie(0)
#define endl '\n'

using namespace std;

int n;
vector<vector<int> > f(1010);

void init();
vector<int> add(vector<int> &A, vector<int> &B, vector<int> &C);

int main() {
	IOS;
	
	init();
	
	while (cin >> n) {
		reverse(f[n].begin(), f[n].end());
		for (auto e : f[n]) cout << e;
		cout << endl;
		reverse(f[n].begin(), f[n].end());
	}
	
	return 0;
}

void init() {
	int a[5] = {1, 1, 2, 4, 7};
	for (int i = 0; i < 5; i ++ ) f[i].push_back(a[i]);
	
	for (int i = 5; i <= 1000; i ++ ) {
		f[i] = add(f[i - 1], f[i - 2], f[i - 4]);
	}
}

vector<int> add(vector<int> &A, vector<int> &B, vector<int> &C) {
	vector<int> R;
	int la = A.size(), lb = B.size(), lc = C.size();
	
	for (int i = 0, t = 0; t || i < la || i < lb || i < lc; i ++ ) {
		t += (i < la ? A[i] : 0) + (i < lb ? B[i] : 0) + (i < lc ? C[i] : 0);
		R.push_back(t % 10);
		t /= 10;
	}
	
	return R;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值