思路:dp(i)表示第i天的猪的数量,g(i)表示第i天新出生的猪的数量,d(i) = d(i-1) * 2 - g(i-2), g(i) = d(i-1)
AC代码
#include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include <map> #include <set> #include <vector> #include <queue> #include <stack> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10 #define inf 0x3f3f3f3f #define PI pair<int, int> typedef long long LL; const int maxn = 20 + 5; int ans[maxn], g[maxn]; void init() { ans[0] = g[0] = 0; ans[1] = g[1] = 1; for(int i = 2; i <= 20; ++i) { g[i] = ans[i-1]; ans[i] = g[i] * 2; if(i-2 >= 0) ans[i] -= g[i-2]; } } int main() { init(); int T, n; scanf("%d", &T); while(T--) { scanf("%d", &n); printf("%d\n", ans[n]); } return 0; }
如有不当之处欢迎指出!