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

    http://acm.hdu.edu.cn/showproblem.php?pid=5119

    Happy Matt Friends

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


    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

    解题思路:DP

      1 #include <stdio.h>

     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 
     6 const int MAX = (1<<20);
     7 
     8 long long dp[41][MAX];
     9 int n, w, a[41];
    10 
    11 int main(){
    12     int T, i, j;
    13     int Case = 1;
    14     scanf("%d", &T);
    15     while(T--){
    16         scanf("%d %d", &n, &w);
    17         for(i = 0; i < n; i++){
    18             scanf("%d", &a[i]);
    19         }
    20         memset(dp, 0sizeof(dp));
    21         dp[0][0] = 1;
    22         long long ans = 0;
    23         if(w == 0){
    24             ans++;
    25         }
    26         for(i = 0; i < n; i++){
    27             for(j = 0; j < MAX; j++){
    28                 if(dp[i][j] == 0){
    29                     continue;
    30                 }
    31                 dp[i + 1][j] += dp[i][j];
    32                 int nj = j^a[i];
    33                 dp[i + 1][nj] += dp[i][j];
    34                 if(nj >= w){
    35                     ans += dp[i][j];
    36                 }
    37             }
    38         }
    39         printf("Case #%d: %I64d ", Case++, ans);
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    2020_java面试
    Centos7.2 安装docker、mysql和redis
    【源码讲解】Spring事务是如何应用到你的业务场景中的?
    奇葩说今晚聊前任|扒一扒你的前任企业邮箱
    一封一封邮件发送太累?个人邮箱快速群发解决烦恼
    企业邮箱客户端收发服务器如何设置?
    163VIP邮箱怎么设置邮件签名?如何群发邮件?
    企业版邮箱哪个适合学校邮箱?企业邮箱托管服务
    企业版邮箱购买哪个?公司邮箱如何申请?
    公司邮箱登录入口是?公司邮箱申请入口在?
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4135898.html
Copyright © 2011-2022 走看看