B - Equal Sum Sets

Let us consider sets of positive integers less than or equal to n. Note that all elements of a set aredifferent. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} meanthe same set.Specifying the number of set elements and their sum to be k and s, respectively, sets satisfying theconditions are limited. When n = 9, k = 3 and s = 23, {6, 8, 9} is the only such set. There may bemore than one such set, in general, however. When n = 9, k = 3 and s = 22, both {5, 8, 9} and {6, 7, 9}are possible.You have to write a program that calculates the number of the sets that satisfy the given conditions.InputThe input consists of multiple datasets. The number of datasets does not exceed 100.Each of the datasets has three integers n, k and s in one line, separated by a space. You may assume1 ≤ n ≤ 20, 1 ≤ k ≤ 10 and 1 ≤ s ≤ 155.The end of the input is indicated by a line containing three zeros.OutputThe output for each dataset should be a line containing a single integer that gives the number of thesets that satisfy the conditions. No other characters should appear in the output.You can assume that the number of sets does not exceed 231 − 1.

Sample Input

9 3 23

9 3 22

10 3 28

16 10 107

 20 8 102

 20 10 105 

20 10 155

 3 4 3

4 2 11

0 0 0 

Sample Output

1

2

0

20

1542

5448

1

0

0




#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
       int n,k,s;
       while(~scanf("%d%d%d",&n,&k,&s))
       {
             if(k+s+n==0)
                break;
             int sum1=0,i,j;
             for(i=2;i<(1<<(n+1));i++)
             {
                  if(i%2==0)
                    continue;
                  int sum=0,ans=0;
                  for(j=1;j<=n;j++)
                    if(i&(1<<j))
                  {
                       sum+=j;ans++;
                  }
                  if(sum==s&&ans==k)
                    sum1++;
             }
             printf("%d\n",sum1);
       }
}
#include<iostream>
#include<cstdio>
using namespace std;
int n,k,s;
int ans;
int dfs(int nn,int kk,int ss)
{
        if(kk==k&&ss==s)
        {
            ans++;
            return ans;
        }
        for(int i=nn-1;i>=1;i--)
        {
              if(ss+i<=s)
                dfs(i,kk+1,ss+i);
        }
}
int main()
{
      while(~scanf("%d%d%d",&n,&k,&s))
      {
           if(s+k+n==0)
            break;
            ans=0;
           dfs(n+1,0,0);
           printf("%d\n",ans);
      }
      return 0;
}
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,k,s;
int i,j;
int dp[156][21][11];
int main()
{
       memset(dp,0,sizeof(dp));
       for(i=1;i<=155;i++)
        for(j=1;j<=20;j++)
        for(k=1;k<=10;k++)
       {
             if(k==1)
             {
                   if(i<=j)
                  dp[i][j][k]=1;
             }
             else
               {
                    dp[i][j][k]=dp[i][j-1][k];
                    if(i>j)
                        dp[i][j][k]+=dp[i-j][j-1][k-1];
               }
       }
       while(cin>>n>>k>>s)
       {
            if(n+k+s==0)
                break;
            cout<<dp[s][n][k]<<endl;
       }
       return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值