UvaLive 6661 Equal Sum Sets 二进制枚举/DP

链接:http://vjudge.net/problem/viewProblem.action?id=49406

题意:根据给出的n,k,s求出n个数每个数都不大于k,和为s的序列(n个数每个都不同)的总情况数。

思路:

1.二进制枚举枚举出所有可能排列,并求和若和为s,则符合,否则不符合。

代码:

#include<iostream>
#include<set>
#include<map>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;

int lim[25];
int init()
{
    lim[0]=1;
    lim[1]=2;
    for(int i=2;i<=22;i++)
        lim[i]=lim[i-1]*2;
}
int main()
{
    int n,k,s;
    init();
    while(scanf("%d%d%d",&n,&k,&s)&&!(!n&&!k&&!s))
    {
        int res=0;
        for(int i=0;i<lim[n];i++)
        {
            int ii=i;
            int all=0,kk=0;
            int ans=0;
            while(ii)
            {
                all++;
                if(ii%2==1)
                {
                    kk++;
                    ans+=all;
                }

                ii/=2;
            }
            if(ans==s&&kk==k)
                    res++;
        }
        printf("%d\n",res);
    }
    return 0;
}

2.DP

状态转移方程:dp[i][j][k]=dp[i-1][j][k]+dp[i-1][j-i][k-1]

其中dp[i][j][k]表示不超过i的k个和为j的情况数。

dp[i-1][j][k]是指第i个不选择i的情况,dp[i-1][j-i][k-1]表示第个选择i,即前k-1个的和为j-i。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<cstdlib>
#include<queue>
#include<stack>
#include<vector>
#include<ctype.h>
#include<algorithm>
#include<string>
#define PI acos(-1.0)
#define maxn 10005
#define INF 0x7fffffff
typedef long long ll;
using namespace std;
int dp[25][160][15];
int init()
{
    dp[0][0][0]=1;
    for(int i=1; i<=20; i++)
    {
        for(int j=0;j<=155;j++)
        {
            for(int k=0;k<=10;k++)
            {
                dp[i][j][k]=dp[i-1][j][k];
                if(j>=i&&k>0)
                dp[i][j][k]+=dp[i-1][j-i][k-1];
            }
        }
    }
}
int main()
{
    int n,k,s;
    init();
    while(scanf("%d%d%d",&n,&k,&s))
    {
        if(n==0&&k==0&&s==0)
            break;
        printf("%d\n",dp[n][s][k]);
    }
    return 0;
}
时间: 2024-12-28 21:29:29

UvaLive 6661 Equal Sum Sets 二进制枚举/DP的相关文章

[UVALive 6661 Equal Sum Sets] (dfs 或 dp)

题意: 求从不超过 N 的正整数其中选取 K 个不同的数字,组成和为 S 的方法数. 1 <= N <= 20  1 <= K<= 10  1 <= S <= 155 解题思路: DFS: 因为N,K.S的范围非常小.直接DFS就可以. /* ID: [email protected] PROG: LANG: C++ */ #include<map> #include<set> #include<queue> #include<

UVALive 6661 - Equal Sum Sets

Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of se

UvaLive 6661 Equal Sum Sets (DFS)

Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of se

UvaLive6661 Equal Sum Sets dfs或dp

UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cs

D。6661 - Equal Sum Sets

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

6661 Equal Sum Sets(DP)

Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of se

Aizu 1335 Equal Sum Sets

Description Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesn't matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the

Equal Sum Sets

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=49406 题意: 给出三个数 n,k,s;在不小于n的找到k个使其的和等于s的可能性有多少种. 案例: 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 output 1 2 0 20 1542 5448 1 0 0 思路分析: 利用dfs

POJ 1681 Painter&#39;s Problem 【高斯消元 二进制枚举】

任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7667   Accepted: 3624 Description There is a square wall which is made of n*n small square bricks. Some bricks are white while some bric