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;
}


/** ***************************************************************************** @example ADC1_DMA.c @brief This example shows how to initialize the ADC1 for DMA operation - It implements the RTD demo similar to RTD_DEMO project except it uses DMA operation - This also sets up the SINC2 filter DMA output to measure across the RTD - ADC1 is used to measure an RTD input connected across AIN0 and AIN1. - AIN6 is used as the excitation current source for the RTD - REFIN+/REFIN- are the ADC external reference inputs - 100ohm PT100 RTD expected connected to AIN0 and AIN1 - To eliminate drift elements the following is the measurement sequence: - 1) Measure AIN1/AGND v Internal 1.2V reference to derive exact Excitation current value - 2) Measure AIN0/AGND as a diagnostic - should equal sum of steps 1 and 2. - 3) Measure AIN0/AIN1 v Internal Vref to determine voltage across RTD (vRTD) - 4) RRTD is determined and final RTD temeprature calculated. - The RTD reading is linearized and sent to the UART in a string format. - Default Baud rate is 9600 - EVAL-ADuCM360MKZ or similar hardware is assumed - Results will be more accurate if System calibration is added. @version V0.2 @author ADI @date February 2013 @par Revision History: - V0.1, October 2012: initial version. - V0.2, February 2013: Fixed a bug in SendString(). All files for ADuCM360/361 provided by ADI, including this file, are provided as is without warranty of any kind, either expressed or implied. The user assumes any and all risk from the use of this code. It is the responsibility of the person integrating this code into an application to ensure that the resulting application performs as required and is safe. **/解释上述代码注释的意思
03-28
This is a MySQL query that selects the student ID (s_id), assigns a sequential number to each row (i), and calculates the rank of each student based on their sum of scores (sum_score). The query uses a subquery to first group the scores by student ID and calculate the sum of scores for each student. This subquery is then joined with a variable initialization subquery that sets the initial values of @k, @i, and @score to 0. The variable @k is used to keep track of the current rank while iterating over the rows. The variable @i is used to assign a sequential number to each row. The variable @score is used to compare the sum_score of the current row with the sum_score of the previous row. The CASE statement is used to check if the sum_score of the current row is equal to the sum_score of the previous row. If they are equal, then the rank remains the same. If they are not equal, then the rank is updated to the current sequential number. Here is a breakdown of the query: 复制 SELECT a.s_id, -- Select the student ID @i:=@i+1 AS i, -- Assign a sequential number to each row @k:=(case when @score=a.sum_score then @k else @i end) as rank, -- Calculate the rank a.sum_score AS score -- Select the sum of scores for each student FROM (SELECT s_id,SUM(s_score) AS sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC) a, -- Subquery to calculate sum of scores for each student (SELECT @k:=0,@i:=0,@score:=0) s -- Subquery to initialize variables Note that the use of variables in this query is not recommended, as it can lead to unexpected results if the variables are not reset properly. It is better to use a subquery or a window function to calculate the rank. 翻译
02-27
Theta = {'A', 'B', 'C'}; % 辨识框架:假设三个可能故障 % 传感器1的BPA m1 = struct(); m1.A = 0.6; % 支持A的信任度 m1.BC = 0.3; % 支持B或C的信任度 m1.Theta = 0.1; % 不确定部分 % 传感器2的BPA m2 = struct(); m2.B = 0.5; m2.AC = 0.4; m2.Theta = 0.1; function m_combined = dempster_rule(m1, m2, Theta) K = 0; % 冲突系数初始化 % 计算所有非冲突组合的乘积和 for i = 1:length(Theta) for j = 1:length(Theta) if isempty(intersect(m1(i).set, m2(j).set)) K = K + m1(i).value * m2(j).value; end end end % 归一化并计算新mass函数 m_combined = struct(); for k = 1:length(Theta) sum_non_conflict = 0; for i = 1:length(Theta) for j = 1:length(Theta) if isequal(union(m1(i).set, m2(j).set), Theta{k}) sum_non_conflict = sum_non_conflict + m1(i).value * m2(j).value; end end end m_combined(k).value = sum_non_conflict / (1 - K); end end 请以上面计算m函数为基础,实现以下目标9)基于D-S证据理论的信息融合 ① 由每个传感器获得关于各个命题的观测证据 ② 依据人的经验和感觉,给出各个命题的基本概 率分配 ③ 计算置信度和似然度,得到命题的证据区间 ④ 根据D-S证据组合规则计算基本概率分配值、 置信度和似然度 ⑤ 根据给定的判决准则得到最终融合结果 编写D-S融合函数和D-S判决函数,完成D-S main文件的编译,并通过计算给出融合 结果。 3、初始设定 传感器数 nSensor=3;组合数 nObject=5;测量周期数 nPeriod=3。 命题集合为:{{A},{B},{C},{A,B},{A,B,C}} 3个不同周期上3个传感器对5个命题的基本概率分配分别为:略
最新发布
11-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值