ural 1932 The Secret of Identifier (容斥原理)

题目大意:

求出给的n个串中。

精确到只有一个字符不同,两个字符不同,三个字符不同,四个字符不同的对数。


思路分析:

枚举状态。

dp[i] [j] ...表示当前串取出 i 状态下的所有字符转化成十进制数为 j 的出现的次数。

这样的话,就记录了所有串的子串的状态。

然后计数就得到了所有的状态。

然后我们要得到精确不同的,可以用补集的思想,如果要精确到三个不相同,意味着要精确到1 个是相同的。


注意的问题是

在最后要运用容斥去重。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
typedef long long ll;

char str[6];
ll dp[16][1<<16];

int trans(char ch)
{
    if(isdigit(ch))return ch-'0';
    return ch-'a'+10;
}
int Count(int x)
{
    int ret=0;
    while(x){
        ret+=(x&1);
        x>>=1;
    }
    return ret;
}
ll tmp[5],ans[5];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int maxm=-1;
        memset(dp,0,sizeof dp);
        memset(tmp,0,sizeof tmp);

        for(int i=1;i<=n;i++)
        {
            scanf("%s",str);
            for(int s=1;s<16;s++)
            {
                int t=0;
                if(s&8) t += trans(str[0])*(1<<12);
                if(s&4) t += trans(str[1])*(1<<8);
                if(s&2) t += trans(str[2])*(1<<4);
                if(s&1) t += trans(str[3]);
                dp[s][t]++;
                maxm=max(t,maxm);
            }
        }
        for(int s=1;s<16;s++)
        {
            int x=Count(s);
            for(int i=0;i<=maxm;i++)
            {
                tmp[x]+=dp[s][i]*(dp[s][i]-1)/2;
            }
        }

        ans[1]=tmp[3];
        ans[2]=tmp[2]-3*tmp[3];
        ans[3]=tmp[1]-2*tmp[2]+3*tmp[3];
        printf("%I64d %I64d %I64d %I64d\n",ans[1],ans[2],ans[3],(ll)n*(n-1)/2-ans[1]-ans[2]-ans[3]);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
最新发布
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值