zoukankan      html  css  js  c++  java
  • C. Greg and Friends 夜

    http://codeforces.com/contest/295/problem/C

    简单DP

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cmath>
    
    #define ll long long
    using namespace std;
    
    const int N=55;
    const ll MOD = 1000000007;
    ll dp[N*4][N][N];
    ll c[N][N];
    int main()
    {
    	//freopen("data.in","r",stdin);
    	for(int i=0;i<N;++i)
    		for(int j=0;j<=i;++j)
    		{
    			if(i==j||j==0) c[i][j]=1;
    			else c[i][j]=(c[i-1][j-1]+c[i-1][j])%MOD;
    		}
    	int n,m;
    	while(cin>>n>>m)
    	{
    		int a=0;
    		int b=0;
    		for(int i=1;i<=n;++i)
    		{
    			int tmp;
    			cin>>tmp;
    			if(tmp==50)
    				++a;
    			if(tmp==100)
    				++b;
    		}
    		memset(dp,0,sizeof(dp));
    		dp[0][a][b]=1;
    		ll ans=-1;
    		int i;
    		for(i=0;i<n*4;++i)
    		{
    			for(int l=0;l<=a;++l)
    			{
    				for(int r=0;r<=b;++r)
    				if(dp[i][l][r]>0)
    				{//cout<<i<<" "<<l<<" "<<r<<" "<<dp[i][l][r]<<endl;
    					if(l==0&&r==0) {ans=dp[i][l][r];break;}
    					int l2=((i&1)==0)?l:a-l;
    					int r2=((i&1)==0)?r:b-r;
    					for(int l1=0;l1<=l2;++l1)
    					for(int r1=0;r1<=r2;++r1)
    					{
    						if(l1+r1==0||(l1*50+r1*100)>m)continue;
    						int w=((i&1)==0)?l-l1:l+l1;
    						int h=((i&1)==0)?r-r1:r+r1;
    						dp[i+1][w][h]=(dp[i+1][w][h]+((c[l2][l1]*c[r2][r1]%MOD)*dp[i][l][r])%MOD)%MOD;
    					}
    
    				}
    				if(ans!=-1) break;
    			}
    			if(ans!=-1) break;
    		}
    		if(ans==-1)
    		{cout<<"-1"<<endl;cout<<"0"<<endl;}
    		else
    		{cout<<i<<endl;cout<<ans<<endl;}
    	}
    
    	return 0;
    }
    

      

  • 相关阅读:
    MySQL Workbench的安全更新模式
    IEnumerable<T>和IQueryable<T>区分
    Google 网站打不开
    使用 MVVMLight 命令绑定(转)
    使用 MVVMLight 绑定数据(转)
    安装/使用 MVVMLight(转)
    ?? 运算符(C# 参考)
    REST风格URL
    node+mysql 数据库连接池
    理解mysql执行多表联合查询
  • 原文地址:https://www.cnblogs.com/liulangye/p/3027786.html
Copyright © 2011-2022 走看看