Light School Competition-zzuli 2266: number [Hash processing in binary]

zzuli 2266: number

Given n, ask how many pairs <x, y> satisfy:
x, y∈[1, n], x < y
           The numbers of [0, 9] appearing in x, y are the same
enter
an integer n (n <= 107)
output
output a number as the answer
Sample input 30 
Sample output 3

The general idea: The value of N is not very large. You can consider running a loop to separate each value once, and then use the 0-9th bit of the binary number '1' to represent the number 0-9, and to Labeling with an array prevents adding it twice! For example, a decimal number y is split bit by bit in turn. If there is a digital x, add 2 to the power of x -- and mark it, if not, don't add it! N=10^7, a number is not very big! The time complexity is about 10^8, and a single instance can pass!

AC problem solution:
 1 #define N 2005
 2 
 3 int main(){
 4     int n;
 5     int hash2[2001];
 6 
 7     while(scanf("%d",&n)!=EOF){
 8         memset(hash2,0,sizeof(hash2));
 9         for(int i=1;i<=n;i++){
10             int x=i;
11 
12             int vis[10]={0};
13             int val=0;
14             while(x>0){
15                 int y=x%10;
16                 if(!vis[y]){
17                     val+= 1<<y;
18                     vis[y]=1;
19                 }
20                 x/=10;
21             }
22             hash2[val]++;
23         }
24 
25         ll ans=0;
26         for(int i=1;i<=2000;i++){
27             if(hash2[i]>=2){
28               //  printf("%d %d\n",i,hash2[i]);
29                 ans+=(ll)hash2[i]*(ll)(hash2[i]-1)/(ll)2;
30             }
31 
32         }
33         printf("%lld\n",ans);
34 
35     }
36 
37     return 0;
38 }
View Code (the header files all eloped!)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324820885&siteId=291194637