HDU 5366 The mook jong (排列组合 或 找规律)

The mook jong

Accepts: 506
 
 Submissions: 1281
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
Problem Description

ZJiaQ want to become a strong man, so he decided to play the mook jong。ZJiaQ want to put some mook jongs in his backyard. His backyard consist of n bricks that is 1*1,so it is 1*n。ZJiaQ want to put a mook jong in a brick. because of the hands of the mook jong, the distance of two mook jongs should be equal or more than 2 bricks. Now ZJiaQ want to know how many ways can ZJiaQ put mook jongs legally(at least one mook jong).

Input

There ar multiply cases. For each case, there is a single integer n( 1 < = n < = 60)

Output

Print the ways in a single line for each case.

Sample Input
1	
2
3
4
5
6
Sample Output
1
2
3
5
8
12



问题描述
ZJiaQ为了强身健体,决定通过木人桩练习武术。ZJiaQ希望把木人桩摆在自家的那个由1*1的地砖铺成的1*n的院子里。由于ZJiaQ是个强迫症,所以他要把一个木人桩正好摆在一个地砖上,由于木人桩手比较长,所以两个木人桩之间地砖必须大于等于两个,现在ZJiaQ想知道在至少摆放一个木人桩的情况下,有多少种摆法。
输入描述
输入有多组数据,每组数据第一行为一个整数n(1 < = n < = 60)
输出描述
对于每组数据输出一行表示摆放方案数
输入样例
1	
2
3
4
5
6
输出样例
1
2
3
5
8
12

解题思路:

1、排列组合:

对于n个地砖,我们可以摆放1,2,……,i(3*(i-1)+1<=n)个木人桩,由于相邻的两个木人之间距离大于等于2,如果假设现在要插入i个木人桩,那么每相邻的两个木人桩之间都至少有两个地砖,对前(i-1)个木人桩,我们把一个木人桩和两个地砖放在一起看做一个整体,那么剩余的地砖总数为m=n-2*(i-1)。那么对于当前的i个木人桩,就应该有C(m,i)种方案。枚举所有的i,加和即可。(注意:排列组合的结果会超int)


代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;

ll c[65][65];
//排列组合
void cinit()
{
    int i,j;
    for(i=0;i<=60;i++)
    {
        c[i][0]=c[i][i]=1;
        for(j=1;j<i;j++)
            c[i][j]=c[i-1][j]+c[i-1][j-1];
    }
}
int main()
{
    int i,n;
    ll sum;
    cinit();
    while(~scanf("%d",&n))
    {
        //i的范围
        for(i=1,sum=0;3*(i-1)+1<=n;i++)
        {
            int m=n-2*(i-1);
            sum+=c[m][i];
        }
        printf("%I64d\n",sum);
    }
    return 0;
}



2、找规律

    n=1 2 3 4 5  6   7   8   9  10 11  12   13  14   15   16   17    18      19    20

ans=1,2,3,5,8,12,18,27,40,59,87,128,188,276,405,594,871,1277,1872,2744

根据上面的结果ans以及n,可以发现,ans[i]=ans[i-1]+ans[i-3]+1;


代码如下:

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
    int i,n;
    ll a[65];
    a[1]=1;
    a[2]=2;
    a[3]=3;
    for(i=4;i<=60;i++)
        a[i]=a[i-1]+a[i-3]+1;
    while(~scanf("%d",&n))
    {
        printf("%I64d\n",a[n]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值