华为oj 之 整数分隔
/*
(1)如果是奇数:
dp[i] = dp[i-1]
相当于在(i-1)每个分隔的前面添了一个1
(2)如果是偶数:
dp[i] = dp[i-1] + dp[i/2]
相当于在(i-1)每个分隔的前面添了一个1,同时(i/2)的每个分隔乘以2
eg:
2:
1 1
2
3:
1 1 1
1 2
4:
1 1 1 1
1 1 2
2 2
4
*/
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <cstddef> 7 #include <iterator> 8 #include <algorithm> 9 #include <string> 10 #include <locale> 11 #include <cmath> 12 #include <vector> 13 #include <cstring> 14 #include <map> 15 #include <utility> 16 #include <queue> 17 #include <stack> 18 #include <set> 19 #include <functional> 20 using namespace std; 21 typedef pair<int, int> PII; 22 typedef long long int64; 23 const int INF = 0x3f3f3f3f; 24 const int modPrime = 3046721; 25 const double eps = 1e-9; 26 const int MaxN = 1000010; 27 const int MaxM = 30; 28 const int Mod = 1000000000; 29 const char Opt[4] = { '+', '-', '*', '/' }; 30 31 32 int dp[MaxN]; 33 34 void Solve(int n) 35 { 36 dp[0] = 0; 37 dp[1] = 1; 38 for (int i = 2; i < n; ++i) 39 { 40 if (i & 1) 41 { 42 dp[i] = dp[i - 1]; 43 } 44 else 45 { 46 dp[i] = (dp[i - 1] + dp[i / 2]) % Mod; 47 } 48 } 49 } 50 51 52 int main() 53 { 54 #ifdef HOME 55 freopen("in", "r", stdin); 56 //freopen("out", "w", stdout); 57 #endif 58 59 Solve(MaxN); 60 int n; 61 while (cin >> n) 62 { 63 if (n > 1000000 || n < 0) 64 { 65 cout << -1 << endl; 66 continue; 67 } 68 cout << dp[n] << endl; 69 } 70 71 #ifdef HOME 72 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 73 _CrtDumpMemoryLeaks(); 74 #endif 75 return 0; 76 }