http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31329#problem/D
做出前6组的情况;
当n = 1 ,{1};
当n = 2 , {1} , {2};
当n = 3, , {1 , 3 } , { 2 } ;
当n = 4 , { 1 , 3 } , { 2 , 4 } , { 1 , 4 };
当n = 5, { 1 , 3 , 5 } , { 2 , 4 } { 1 , 4 } , { 2, 5
......................
由此可知,当n与其前一个数相差一个位置的等于分f[ n - 2 ] ,相差两个位置的等于f[ n - 3 ] ;
所以f[ n ] = f[ n - 2 ] + f[ n -3 ] ;
可以理解为DP的思想,假设当n= 5 时,因为{1,3}已经出现过,相隔4后{1,3,5}不改变原有数目,但是2————>5,又有一条路,所以改变f[n]的值仅有f[ n -2 ] 和f[ n - 3 ]有影响;
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<ctime>
#include<deque>
#include<stack>
#include<bitset>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<numeric>
#include<sstream>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std ;
const int maxn = 77 ;
long long f[ maxn ] = { 0 , 1 , 2 , 2} ;
void Union()
{
for( int i = 4 ; i < maxn ; ++i )
{
f[ i ] = f[ i - 2 ] + f[ i - 3 ] ;
}
}
int main()
{
int n ;
Union();
while( scanf( "%d" , &n ) != EOF )
{
printf( "%lld\n" , f[ n ] ) ;
}
return 0 ;
}