Problem B - Run Step 2015 ACM/ICPC Greater New York Region

大半年没刷题直接去打校内赛竟然晋级了囧rz,不过这一题当时一堆人过了也木有做出来。

Set l2=number of two step strides taken by left leg, l1=number of one step strides taken by left leg, r2=number of two step strides takenby right leg, r1=number of one step strides taken by right leg。

l2=r2, l1=r1, l2+r2>=l1+r1,l1+r1+2*(l2+r2)=s, thus l1+l2=s/2 and l2>=l1。于是只要枚举可能的整数解就行了。因为left and right是交替走的,所以对于任意一组可行的l1,l2,对应的组合数为C(s/2,l1)*C(s/2,r1)。

我开始写成了两个组合数相加,难怪结果算的那么小。还以为是组合数算错了>< 果真应该先动脑子><

这一题我最开始竟然逗比地写了个dfs。。。最后写完第四个样例跑不出来。。才发现那么大的数明明就不是dfs好嘛/(ㄒoㄒ)/~~

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>

using namespace std;
int P;
int K;
int s;
const int maxn=110;
long long ans;
long long l1;
long long l2;
long long r1;
long long r2;
long long n2;
long long n1;
long long comb[maxn][maxn];
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];
            //cout<<i<<" "<<j<<" "<<comb[i][j]<<endl;
        }
    }
//    for(int i=0;i<maxn;i++)
//    {
//        for(int j=0;j<=i;j++)
//        {
//            cout<<i<<" "<<j<<" "<<comb[i][j]<<endl;
//        }
//    }

}
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;
                //n=n/2;
                ans+=comb[n/2][x]*comb[n/2][x];
            }

        }

    }
}
void dfs(int pos, int step, int sum)
{
    if(sum>s)
    {
        return;
    }
    if(sum==s)
    {
        if(l1!=r1) return;
        if(l2!=r2) return;
        if(n2<n1) return;
        ans++;
        return;
    }
    if(n1>s/2) return;
    if(pos%2==0)//next is right
    {
        n1++;
        r1++;
        dfs(pos+1,1,sum+1);
        n1--;
        r1--;
        n2++;
        r2++;
        dfs(pos+1,2,sum+2);
        n2--;
        r2--;
    }
    if(pos%2==1)//next is left
    {
        n1++;
        l1++;
        dfs(pos+1,1,sum+1);
        n1--;
        l1--;
        n2++;
        l2++;
        dfs(pos+1,2,sum+2);
        n2--;
        l2--;
    }

}
int main()
{
    freopen("b.in","r",stdin);
    freopen("myb.out","w",stdout);
    init();
    scanf("%d",&P);
    for(int ca=0;ca<P;ca++)
    {
        scanf("%d %d",&K,&s);
        ans=0;
//        l1=0;
//        l2=0;
//        r1=0;
//        r2=0;
//        n1=0;
//        n2=0;
//        dfs(0,s,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、付费专栏及课程。

余额充值