// Counting (數數)// PC/UVa IDs: 110603/10198, Popularity: B, Success rate: high Level: 2// Verdict: Accepted// Submission Date: 2011-06-02// UVa Run Time: 0.032s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net//// 假設 F(n) 表示使用 1,2,3,4 構建的和為 n 的序列總數,則這些序列中,以 1 為開始的序列種數// 為 F(n - 1),以2為開始的為 F(n - 2),以此類推,以 3、4 開始的序列種數為 F(n - 3)、// F(n - 4),由於 Gustavo 把 4 當作 1,則有 F(n - 4) = F(n - 1),故 F(n) = F(n - 1)// + F(n - 2) + F(n - 3) + F(n - 4) = 2 * F(n - 1) + F(n - 2) + F(n - 3),// F(1) = 2, F(2) = 5, F(3) = 13。#include <iostream>#include <vector>#include <iterator>#include <algorithm>using namespace std;#define MAXN 1000// 由前三項計算下一項。string next(string a, string b, string c){string d, e;int carry = 0;for (int i = 0; i < a.length(); i++){int v = carry + 2 * (a[i] - '0');carry = v / 10;d.append(1, char('0' + v % 10));}if (carry)d.append(1, char('0' + carry));// 為數b和c添加前置0,使得數位和d的數位相同,便於計算。while (b.length() < d.length())b.append(1, '0');while (c.length() < d.length())c.append(1, '0');carry = 0;for (int i = 0; i < d.length(); i++){int v = carry + (b[i] - '0') + (c[i] - '0') + (d[i] - '0');carry = v / 10;e.append(1, char('0' + v % 10));}if (carry)e.append(1, char('0' + carry));return e;}// 初始化數組,直到計算出 MAXN 指定的數序列總數,數位逆序存放。void init(vector < string > &counting){counting.push_back("2");counting.push_back("5");counting.push_back("31");for (int i = 3; i <= MAXN; i++)counting.push_back(next(counting[i - 1], counting[i - 2],counting[i - 3]));}int main(int ac, char *av[]){int n;vector < string > counting;init(counting);while (cin >> n){reverse_copy(counting[n - 1].begin(), counting[n - 1].end(),ostream_iterator < char >(cout, ""));cout << endl;}return 0;}