题目:click here
:这个题可以先dfs深搜下,规律dp
dfs:
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define S second 4 typedef long long ll; 5 const int INF = 0x3f3f3f3f; 6 const int M = 1e5+3; 7 8 int s; 9 int cnt = 0; 10 bool vis[60]; 11 void dfs( int last, int num ) { 12 if( num == s ) { 13 cnt++; 14 return ; 15 } 16 for( int i=2; i<s+1; i++ ) 17 if( abs(last-i)<=2 && !vis[i] ) { 18 vis[i] = true; 19 dfs( i, num+1 ); 20 vis[i] = false; 21 } 22 } 23 int main() { 24 while( ~scanf("%d", &s ) ) { 25 memset( vis, false, sizeof(vis) ); 26 cnt = 0; 27 vis[1] = true; 28 dfs( 1, 1 ); 29 printf("%d\n", cnt ); 30 } 31 return 0; 32 }
DP:
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define F first 4 #define S second 5 typedef unsigned long long ll; 6 const int INF = 0x3f3f3f3f; 7 const int M = 1e5+3; 8 9 int s; 10 ll dp[60]; 11 void pre() { 12 memset( dp, 0, sizeof(dp) ); 13 dp[1] = 1; 14 dp[2] = 1; 15 dp[3] = 2; 16 dp[4] = 4; 17 for( int i=5; i<=55; i++ ) 18 dp[i] = dp[i-1]+dp[i-3]+1; 19 } 20 int main() { 21 pre(); 22 while( ~scanf("%d", &s ) ) { 23 printf("%I64d\n", dp[s] ); 24 } 25 return 0; 26 }
时间: 2024-10-19 16:08:01