URAL 1932 The Secret of Identifier 题解

http://acm.timus.ru/problem.aspx?space=1&num=1932

B - The Secret of Identifier
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice URAL 1932

Description
Davy Jones: You‘ve been captain of the Black Pearl for 13 years. That was our agreement.
Jack: Technically I was only captain for two years, then I was mutinied upon.
Davy Jones: Then you were a poor captain, but a captain nonetheless. Have you not introduced yourself as Captain Jack Sparrow?
According to the Pirate Code, each of the pirates of the Caribbean at the beginning of their professional career (hereditary pirates –– at birth) is assigned by a unique identifier. Pirate‘s identifier is a string of four hexadecimal digits. However, it is not a usual row of numbers, it is said that personal qualities and life path of its owner are encoded in it by a mysterious way. But no one still could guess this mystical connection.
Once Captain Jack Sparrow, while sitting in captain’s cabin, decided to try to find the way to derive some data about a pirate using the identifier. Memories about how he lost the Black Pearl last time gave him the idea that more similar identifiers of two pirates are, bigger chances for these pirates to unite against the Captain, and, as a result, to make a mutiny. The Captain Jack Sparrow, of course, doesn’t want to have the mutiny on his ship, but he chose the new team this time and it is going to be a long voyage. Now Jack needs to estimate the opportunities of raising the mutiny on his ship, based on the conclusions. For this aim he first wants to know for each pair of pirates a number of positions in their identifiers in which they are different.

Input
The first line contains an integer n –– the number of pirates aboard the Black Pearl (2 ≤ n ≤ 65536). Each of the following n lines contains four-digit identifier of the respective pirate. Only decimal digits and lowercase Latin letters from “a” to “f” inclusive are used in writing identifiers. Identifiers of all pirates are different.

Output
Output four space separated integers –– the amount of pairs of pirates, which have different identifiers exactly in one, two, three and four positions respectively.

Sample Input
input output

3
dead
beef
f00d

0 0 2 1

题目(格式混乱,请点击上面链接查看原题)

给n个不同的4位十六进制数,两两按位比较,输出有1位不同的、两位不同的、3位不同的、4位不同的组合的个数。(输出4个数)

先弄15个4位十六进制数,像掩码之类的一样,虽然我也不懂掩码具体是怎么弄的。比如现在用的是0F0F,和输入的那些数按位与(&)一下,得到数x,把a[x]++,最后统计a[i]>1的就是0F0F这两个F的位置相同的数的个数,然后这有2个F,就把代表2个相同的t[2]+=a[i]*(a[i]-1)/2;

好像有点说不清楚,不过就是这样!

掩码和F的数量可以开始先打好表,也可以每次算一下,我是先打好表的。

 1 //最终版本,我哭了
2 #include<cstdio>
3 #include<cstring>
4 typedef long long ll;
5 const ll MAXN=66666;
6 const ll psn=15;
7 const ll ps[psn]= {0x000f,0x00f0,0x00ff,0x0f00,0x0f0f,0x0ff0,0x0fff,0xf000,
8 0xf00f,0xf0f0,0xf0ff,0xff00,0xff0f,0xfff0,0xffff};
9 const ll pa[psn]= {1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
10 ll t[MAXN],a[MAXN],ans[5];
11 ll C(ll a,ll b){
12 ll re=1;
13 for(ll i=0;i<b;i++){
14 re=re*(a-i)/(i+1);
15 }
16 return re;
17 }
18
19 int main()
20 {
21 int i,j,n;
22 while(scanf("%d",&n)!=EOF)
23 {
24 memset(ans,0,sizeof(ans));
25 for(i=0; i<n; i++)
26 scanf("%x",&a[i]);
27 for(i=0; i<psn; i++)
28 {
29 memset(t,0,sizeof(t));
30 for(j=0; j<n; j++)
31 t[a[j]&ps[i]]++;
32 for(j=0; j<=0xffff; j++)
33 ans[pa[i]]+=t[j]*(t[j]-1)/2;
34 }
35 for(i=3; i>0; i--)
36 for(j=1;j<i;j++)
37 ans[j]-=C(i,j)*ans[i];
38 ans[0]=C(n,2)-ans[1]-ans[2]-ans[3];
39 printf("%I64d %I64d %I64d %I64d\n",ans[3],ans[2],ans[1],ans[0]);
40 }
41 return 0;
42 }

时间: 2024-10-29 04:51:22

URAL 1932 The Secret of Identifier 题解的相关文章

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

题目大意: 求出给的n个串中. 精确到只有一个字符不同,两个字符不同,三个字符不同,四个字符不同的对数. 思路分析: 枚举状态. dp[i] [j] ...表示当前串取出 i 状态下的所有字符转化成十进制数为 j 的出现的次数. 这样的话,就记录了所有串的子串的状态. 然后计数就得到了所有的状态. 然后我们要得到精确不同的,可以用补集的思想,如果要精确到三个不相同,意味着要精确到1 个是相同的. 注意的问题是 在最后要运用容斥去重. #include <cstdio> #include <

ural 1932 The Secret of Identifier 容斥

题目链接:点击打开链接 stl+容斥 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <set> using namespace std; #define N 65540 #define ll __int64 ll n; ll a[N][4], mul[4]={1,16,25

ural 1960 Palindromes and Super Abilities 题解

题意:给一个串s,按顺序一个个加入到序列里面.输出每次加入之后序列中的本质不同的回文串个数. 回文自动机模板题- -extend函数里面,如果那个if进去了,就代表多了一个本质不同的回文串. 1 #include<cstdio> 2 #include<cstring> 3 const int MAXN=100000+5; 4 const int SIGMA_SIZE=26; 5 struct Node{ 6 int num,len; 7 Node* go[SIGMA_SIZE],*

URAL 1707. Hypnotoad&#39;s Secret(树状数组)

URAL 1707. Hypnotoad's Secret 题目链接 题意:这题设置的恶心不能多说,构造点和矩形,大概就是问每个矩形里面是否包含点 思路:树状数组,把点排序,按y轴,在按x轴,在按询问,这样每次遇到一个点就在相应的扫描线上加,遇到查询就询问出左边到这个点位置的,就能预处理出每个点左下角包含的点的个数,然后每个矩形再利用容斥原理去搞一下即可 代码: #include <cstdio> #include <cstring> #include <algorithm&

ural 1707. Hypnotoad&#39;s Secret(线段树)

题目链接:ural 1707. Hypnotoad's Secret 题目大意:给定N和M,然后N组s0, t0, Δs, Δt, k,每组可以计算出k个星星的坐标:M组a0, b0, c0, d0, Δa, Δb, Δc, Δd, q,每组要求算出q个矩形,判断矩形内是否包含星星,对于q≥20的情况要根据公式计算一个值即可. 解题思路:计算出所有的星星坐标和矩阵,这个每的说了,将矩阵差分成两点,通过计算出每个点左下角有多少个星 星,然后用容斥计算出矩阵内是否有点.这个属于线段树的一个应用. #

URAL 1707. Hypnotoad&amp;#39;s Secret(树阵)

URAL 1707. Hypnotoad's Secret space=1&num=1707" target="_blank" style="">题目链接 题意:这题设置的恶心不能多说.构造点和矩形.大概就是问每一个矩形里面是否包括点 思路:树状数组.把点排序,按y轴,在按x轴.在按询问,这样每次遇到一个点就在对应的扫描线上加.遇到查询就询问出左边到这个点位置的,就能预处理出每一个点左下角包括的点的个数,然后每一个矩形再利用容斥原理去搞一下就

URAL 1936 Roshambo 题解

http://acm.timus.ru/problem.aspx?space=1&num=1936 F - Roshambo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1936 Description Bootstrap: Wondering how it's played? Will: It's a game of deception.

ural 1057 Amount of degrees 题解

题目大意:统计区间[x,y]中在b进制下含k个1的数字个数. 数位dp. 具体见2009刘聪论文<浅谈数位类统计问题>... 1 #include<cstdio> 2 const int MAXN=32; 3 int f[MAXN][MAXN]; 4 void init() 5 { 6 f[0][0]=1; 7 for(int i=1;i<MAXN;++i) 8 { 9 f[i][0]=f[i-1][0]; 10 for(int j=1;j<=i;++j) 11 f[i

URAL 2011. Long Statement题解

传送门 题意:有N个为1或2或3的数,问用这N个数的排列方式是不是有6中以上. 思路:降智题,显然六个数以上无论这六个数是怎么组成,只要有两种数字就一定能组成6种,5种及以下我就懒得找规律了,直接全排列统计. AC程序 using namespace std; const int maxn=105; int a[maxn],n,ans; set<int> se; map<string,bool> ma; int main() { cin>>n; for(int i=0;