Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5.  
  6. namespace ADS.Week8
  7. {
  8.     public class HashTable
  9.     {
  10.         private int tableSize;
  11.         private long[] table;
  12.        
  13.         public HashTable(int size)
  14.         {
  15.             tableSize = size;
  16.             table = new long[size];
  17.             for (int i = 0; i < size; i++)
  18.                 table[i] = -1;
  19.         }
  20.  
  21.         public bool TryInsert(long key)
  22.         {
  23.             int hash = GetHash(key);
  24.             int hash2 = GetHash2(key);
  25.             while (table[hash] != -1 && table[hash] != key)
  26.             {
  27.                 hash = (hash + hash2) % tableSize;
  28.                 hash2++;
  29.             }
  30.             if (table[hash] == key)
  31.                 return false;  
  32.             table[hash] = key;
  33.             return true;
  34.         }
  35.  
  36.         private int GetHash(long key)
  37.         {
  38.             return Math.Abs(unchecked((int)((long)(key * 47))) ^ (int)((key * 31) >> 32)) % tableSize;
  39.         }
  40.         private int GetHash2(long key)
  41.         {
  42.             return Math.Abs(unchecked((int)((long)(key * 113))) ^ (int)((key * 97) >> 32)) % (tableSize - 1) + 1;
  43.         }
  44.     }
  45.     public class Task3
  46.     {
  47.         public static void Main(string[] args)
  48.         {
  49.             using (StreamReader streamReader = new StreamReader("input.txt"))
  50.             using (StreamWriter streamWriter = new StreamWriter("output.txt"))
  51.             {
  52.                 string[] str = streamReader.ReadLine().Split(' ');
  53.                 int n = int.Parse(str[0]);
  54.                 long x = long.Parse(str[1]);
  55.                 int a = int.Parse(str[2]);
  56.                 long b = long.Parse(str[3]);
  57.                 str = streamReader.ReadLine().Split(' ');
  58.                 int ac = int.Parse(str[0]);
  59.                 long bc = long.Parse(str[1]);
  60.                 int ad = int.Parse(str[2]);
  61.                 long bd = long.Parse(str[3]);
  62.  
  63.                 HashTable hashTable = new HashTable(n * 2);
  64.                 for (int i = 0; i < n; i++)
  65.                 {
  66.                     if (hashTable.TryInsert(x))
  67.                     {  
  68.                         a = (a + ad) % 1000;
  69.                         b = (b + bd) % 1000000000000000;
  70.                     }
  71.                     else
  72.                     {
  73.                         a = (a + ac) % 1000;
  74.                         b = (b + bc) % 1000000000000000;
  75.                     }
  76.                     x = (x * a + b) % 1000000000000000;
  77.                 }
  78.                 streamWriter.WriteLine("{0} {1} {2}", x, a, b);
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement