cdoj-1423【思维&&排列组合】

题目链接:点击打开链接

大意:知道一个人跑的总步数,然后要求每次只能跨 1 或 2 步;而且每只脚(左、右脚)跨 1 步 或 2 步的次数必须相同;跨 2 步的次数要不少于跨 1 步的次数。问有多少种合法的跑步方案

#include<cstdio>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
int x,n;
/*
LL C(int a,int b)
{
	b=min(b,a-b);
	double ans=1.0;
	int cnt=1;
	while(cnt<=b)
	{
		ans=ans*(a--)/(cnt++);
	}
	return 1LL*(ans+0.5);
}*/
LL C(int a,int b) 
{ 
    double ans=1.0; 
    b=min(b,a-b); 
    while(b) 
    { 
        ans=ans*(a--)/(b--); 
    } 
    return 1LL*(ans+0.5);
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int text=1;text<=t;text++)
	{
		scanf("%d%d",&x,&n);
		n/=2; // 一只脚要跑的步数 
		int p=n/3; // 一只脚跑 1步的最大次数 
		if( (n%2==0)&&(p&1) )
			p--;
		if( (n&1)&&(p%2==0) )
			p--;
		LL ans=0;
		for(int i=p;i>=0;i-=2)
		{
			int q=(n-i)/2;
			ans+=C(i+q,i)*C(i+q,i); // 奇数位挑空位置排,偶数位挑空位置排 
		}
		printf("%d %lld\n",text,ans);
	}
	return 0;
}
/*
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
int P;
int K;
int s;
const int maxn=110;
long long n2,n1,r2,r1,l2,l1,ans;
int comb[maxn+10][maxn+10];
void init() // 杨辉三角求解组合数 
{
    memset(comb,0,sizeof(comb));
    comb[0][0]=1;
    for(int i=0;i<maxn;i++)
    {
        comb[i][0]=1;
        comb[i][i]=1;

    }
    for(int i=1;i<maxn;i++)
    {
        for(int j=1;j<i;j++)
        {
            comb[i][j]=comb[i-1][j]+comb[i-1][j-1];
        }
    }
}
void solve()
{
    for(int y=0;y<=s/2;y++)
    {
        for(int x=y;x<=s/2;x++)
        {
            if((2*x+y)==s/2)
            {
                int n=2*x+2*y;
                ans+=comb[n/2][x]*comb[n/2][x];
            }
        }

    }
}
int main()
{
    init();
    scanf("%d",&P);
    for(int ca=0;ca<P;ca++)
    {
        scanf("%d %d",&K,&s);
        ans=0;
        solve();
        printf("%d %lld\n",K,ans);
    }
}
*/




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值