zoukankan      html  css  js  c++  java
  • HDU 5119 Happy Matt Friends

    Happy Matt Friends

    Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Others)
    Total Submission(s): 5248    Accepted Submission(s): 2014


    Problem Description
    Matt has N friends. They are playing a game together.

    Each of Matt’s friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends’magic numbers is no less than M , Matt wins.

    Matt wants to know the number of ways to win.
     
    Input
    The first line contains only one integer T , which indicates the number of test cases.

    For each test case, the first line contains two integers N, M (1 ≤ N ≤ 40, 0 ≤ M ≤ 106).

    In the second line, there are N integers ki (0 ≤ ki ≤ 106), indicating the i-th friend’s magic number.
     
    Output
    For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y indicates the number of ways where Matt can win.
     
    Sample Input
    2 3 2 1 2 3 3 3 1 2 3
     
    Sample Output
    Case #1: 4 Case #2: 2
    Hint
    In the first sample, Matt can win by selecting: friend with number 1 and friend with number 2. The xor sum is 3. friend with number 1 and friend with number 3. The xor sum is 2. friend with number 2. The xor sum is 2. friend with number 3. The xor sum is 3. Hence, the answer is 4.
     
    Source
    Recommend
    liuyiding
    题意:有N个人,每个人有一个权值,挑选一些人并将他们的权值异或,求最后得到的值大于M的取法有多少种;
    题解:对于每个值都有取和不取两种状态,用dp[i]表示亦或结果为i的方案数;int x = a[i] ^ j;dp[now][x] = dp[pre][x] + dp[pre][j];
    参考代码为:
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 typedef long long LL;
     5 const int maxn = 45;
     6 const int maxm = (1<<20);
     7 int a[maxn];
     8 LL dp[2][maxm];
     9 
    10 int main()
    11 {
    12     int t;
    13     scanf("%d", &t);
    14     for(int k = 1; k <= t; k++)
    15     {
    16         int n, m;
    17         scanf("%d%d", &n, &m);
    18         for(int i = 1; i <= n; i++)
    19         {
    20             scanf("%d", &a[i]);
    21         }
    22         memset(dp, 0, sizeof(dp));
    23         dp[0][0] = 1;
    24         int pre = 0, now = 1;
    25         for(int i = 1; i <= n; i++)
    26         {
    27             for(int j = 0; j < maxm; j++)
    28             {
    29                 int x = a[i] ^ j;
    30                 dp[now][x] = dp[pre][x] + dp[pre][j];
    31             }
    32             swap(now, pre);
    33         }
    34         LL ans = 0;
    35         for(int i = m; i < maxm; i++)
    36         {
    37             ans += dp[pre][i];
    38         }
    39         printf("Case #%d: %lld
    ", k, ans);
    40     }
    41     return 0;
    42 }
    43   
    View Code
  • 相关阅读:
    正则表达式邮箱验证
    C# TCP应用编程三 异步TCP应用编程
    C# EventWaitHandle类解析
    Git关联远程仓库
    cqyz oj | 表亲结点 | 树上搜索
    cqyz oj | 健美操 | 树形DP | 二分猜答案
    cqyz oj | 树的分治 | 树形DP | 树的重心
    cqyz oj | 化装晚会加强版 | 二分搜索
    cqyz oj | 化装晚会 | 二分搜索 | 贪心
    POJ 1694 古老的游戏 | 贪心 | 树形DP
  • 原文地址:https://www.cnblogs.com/csushl/p/9710614.html
Copyright © 2011-2022 走看看