zoukankan      html  css  js  c++  java
  • Happy Matt Friends(dp)

    Happy Matt Friends

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


    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.
     题解:N范围为40,权值范围为10^6 约等于2的20次方,可以考虑类似dp+位运算的做法。因为异或值一定不会大于2^20,
    所以可以枚举这个值,所以可以列出方程f[i][j]=f[i][j]+f[i][j^a[i]];
    另外我用暴力也写了一发,虽然知道10000%超时;
    dp代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 const int INF=0x3f3f3f3f;
     7 const int MAXN=1<<20;
     8 int dp[2][MAXN<<1];//刚开始开到MAXN+10 RE,改成这才对; 
     9 int main(){
    10     int T,N,M,flot=0;
    11     int m[41];
    12     scanf("%d",&T);
    13     while(T--){
    14         scanf("%d%d",&N,&M);
    15         memset(dp,0,sizeof(dp));
    16         dp[0][0]=1;
    17         int x=0;
    18         for(int i=0;i<N;i++){
    19             x^=1;
    20             scanf("%d",m+i);
    21             for(int j=0;j<=MAXN;j++){
    22                 dp[x][j]=dp[x^1][j]+dp[x^1][j^m[i]];
    23                 //代表上一次值为j^m[i]现在再^m[i]等于j了再加上上次j的个数; 
    24             }
    25         }
    26         long long ans=0;
    27         for(int i=M;i<=MAXN;i++)ans+=dp[x][i];
    28         printf("Case #%d: %lld
    ",++flot,ans);
    29     }
    30     return 0;
    31 }

    暴力超时:

     1 #include<stdio.h>
     2 int ans,dt[41];
     3 int N,M;
     4 void dfs(int top,int sox,int num,int t){
     5     if(num==t){
     6         if(sox>=M)ans++;
     7         return;
     8     }
     9     for(int i=top;i<N;i++){
    10         dfs(i+1,sox^dt[i],num+1,t);
    11     }
    12 }
    13 int main(){
    14     int T;
    15     scanf("%d",&T);
    16     while(T--){
    17         scanf("%d%d",&N,&M);
    18         for(int i=0;i<N;i++)scanf("%d",dt+i);
    19         ans=0;
    20         for(int i=1;i<=N;i++){
    21             dfs(0,0,0,i);
    22         }
    23         printf("%d
    ",ans);
    24     }
    25     return 0;
    26 }
  • 相关阅读:
    perl 实现ascall 码转换
    perl 利用管道读取压缩文件内容
    perl 字符串比较操作符
    perl chomp 函数的真正作用
    RSQLite 操作sqlite数据库
    R 中的do.call 函数
    JavaMail发送和接收邮件API(详解)
    POP3_使用SSL链接邮箱并获取邮件
    MySql_delete同时删除多表相关联记录
    mybatis_mybatis写mapper文件注意事项
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4905304.html
Copyright © 2011-2022 走看看