TOJ 4483: Common Digit Pairs

来源:互联网 发布:游戏王gx结局知乎 编辑:程序博客网 时间:2024/05/21 12:30

4483: Common Digit Pairs
Description

Given N integers, output the number of different pairs that two numbers have common digits. For example, given 3 integers: 1 11 21, there are 3 common digit pairs: <1, 11>, <1, 21>, <11, 21>.

Input

The first line has a positive integer N (1 ≤ N ≤ 1,000,000), then follow N different positive integers in the next N lines, each integer is no more than 1018.

Output

Output the numbers of different common digit pairs.

Sample Input
3
1
11
21
Sample Output
3
刚看是没有思路的,因为他讲数字对不是很明白,倘若是每位一定要超时的,毕竟有那个多个数,也想到了hash,但是发现复杂程度并没有有效得到减小,所以二进位hash就显示出他的力量的了,分别用2的n次幂去存一下每一个数拥有的数字,然后又一步很重要,就是异或,异或有相同位就成立,所以这样处理还是蛮厉害的。因为最多2的9次,相加为2的10次-1,所以数组1024足够

#include <stdio.h>__int64 s[1024];int a[10]; int main(){int n;a[0]=1;for(int i=1;i<10;i++)a[i]=a[i-1]*2;scanf("%d",&n);getchar();while(n--){char c;bool b[10]={0};while(c=getchar(),c!='\n'){b[c-48]=1;}int e=0;for(int k=0;k<10;k++)if(b[k])    e+=a[k];s[e]++;}__int64 f=0;for(int i=1;i<1024;i++){    f+=s[i]*(s[i]-1)/2;    for(int j=1;j<i;j++)    if(i&j)    f+=s[i]*s[j];}printf("%I64d",f);    return 0;}
0 0