您的位置:首页 > 其它

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

2016-05-18 17:29 274 查看
大半年没刷题直接去打校内赛竟然晋级了囧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);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: