Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct elem {
  5. int key,value;
  6. struct elem * next;
  7. };
  8. int modl(int x) {
  9. return x>0 ? x : -1*x;
  10. }
  11. struct elem * Initelem() {
  12. struct elem * a = (struct elem *)malloc(sizeof(struct elem));
  13. a->next = NULL;
  14. a->key = 0;
  15. a->value = 0;
  16. return a;
  17. }
  18. struct elem ** Inittable(int size) {
  19. struct elem ** a = (struct elem**)malloc(size*sizeof(struct elem*));
  20. for(int i = 0;i<size;i++) a[i] = Initelem();
  21. return a;
  22. }
  23. void Insertelem(int value,int key,struct elem ** table,int size) {
  24. struct elem * a = Initelem();
  25. a->key = key;
  26. a->value = value;
  27. a->next = table[modl(key % size)];
  28. table[modl(key % size)] = a;
  29. }
  30. int Searchelem(struct elem ** table,int size,int key) {
  31. struct elem * a = table[modl(key % size)];
  32. while((a != NULL) && (a->key != key)) a = a->next;
  33. return a;
  34. }
  35. int freeallsum(struct elem ** table,int size) {
  36. int sum = 0;
  37. for(int i = 0;i<size;i++) {
  38. struct elem * a = table[i];
  39. struct elem *b;
  40. while(a != NULL) {
  41. sum += (a->value + 1) * a->value / 2;
  42. b = a->next;
  43. free(a);
  44. a = b;
  45.  
  46. }
  47. }
  48. free(table);
  49. return sum;
  50. }
  51. int main()
  52. {
  53. int size,x,key = 0;
  54. scanf("%d" , &size);
  55. struct elem ** table = Inittable(size);
  56. for(int i = 0;i < size;i++) {
  57. scanf("%d" , &x);
  58. key ^= x;
  59. struct elem * a = Searchelem(table,size,key);
  60. if (a) ++a->value;
  61. else if (key == 0)
  62. Insertelem(1,key,table,size);
  63. else Insertelem(0,key,table,size);
  64. }
  65. printf("%d" , freeallsum(table,size));
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement