2017 hdu 新生赛1008 友好整数 (状态压缩)

题意:就是问你有多少对的数

思路:状态压缩,把每个数拆解然后用类似于二进制的方法保存,一共有10位也就队形1024种情况,然后写一个n^2的暴力就可以了

/*

都大二了写hdu新生赛还这么水,真的是太弱了,之前没写过状态压缩,只知道有状压dp,但不会写状压dp(其实什么都都不会,就连那个数塔也要想很久呢,QAQ)

请队友说插头dp更神奇,期待ing

*/

下面附上代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

char a[20];
ll cnt[2005];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<n;i++){
            ll as=0;
//            memset(a,0,sizeof(a));
            scanf("%s",a);
            int len=strlen(a);
            for(int j=0;j<len;j++){
                as|=(1<<(a[j]-'0'));
            }
            cnt[as]++;
        }
        ll sum=0;
        for(int i=1;i<=1024;i++){
            for(int j=i;j<=1024;j++){
                if(i==j) sum+=(cnt[i]*(cnt[i]-1)/2);
                else if(i&j){
                    sum +=cnt[i]*cnt[j];
                }
            }
        }
        printf("%lld\n",sum);
    }
    return 0;
}
/*
3
4
20
44
4
32
51
123
282
*/

 

posted @ 2017-11-27 21:29  啦啦啦天啦噜  阅读(175)  评论(0编辑  收藏  举报