Guest User

Untitled

a guest
Jan 27th, 2013
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <mpir.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6.  
  7. struct KeyValue{
  8.     uint64_t value;
  9.     unsigned int key;
  10. };
  11.  
  12. int compare(const void* a, const void* b){
  13.     //uint64_t f = ((KeyValue*)a)->value;
  14.     //uint64_t s = ((KeyValue*)b)->value;
  15.     //if(f == s)
  16.     //  return 0;
  17.     //if(f < s)
  18.     //  return -1;
  19.     //return 1;
  20.     return ( ((KeyValue*)a)->value - ((KeyValue*)b)->value );
  21. }
  22. bool find_in_arr(KeyValue* arr, size_t len, uint64_t x, unsigned int &key){
  23.     for(size_t a = 0; a<len; ++a){
  24.         if((arr+a)->value == x){
  25.             key = a;
  26.             return true;
  27.         }
  28.     }
  29.     return false;
  30.  
  31. }
  32. bool find_in_array(KeyValue* arr, size_t len, uint64_t x, unsigned int &key){
  33.  
  34.     size_t first = 0;
  35.     size_t last = len;
  36.  
  37.     size_t mid;
  38.     if(last == 0)
  39.         return false;
  40.     if(arr[0].value > x)
  41.         return false;
  42.     if (arr[len - 1].value < x)
  43.         return false;
  44.     while (first < last){
  45.         mid = first + (last - first) / 2;
  46.         if (x <= arr[mid].value){
  47.             last = mid;
  48.         }else{
  49.             first = mid + 1;
  50.         }
  51.     }
  52.     if(arr[last].value == x){
  53.         key = arr[last].key;
  54.         return true;
  55.     }
  56.     return false;
  57. }
  58.  
  59.  
  60. int main(){
  61.         mpz_t a, b, m;
  62.     /*  mpz_init_set_str(a,"3",10);
  63.         mpz_init_set_str(b,"13",10);
  64.         mpz_init_set_str(m,"17",10);
  65.         const unsigned long long  n = 16;*/
  66.  
  67.  
  68.         mpz_init_set_str(a,"6511709995802996252529428210009069141516455100602512742258547866292839663070281213795462953359084389612327175129512390242393774885156190907043183764572298",10);
  69.         mpz_init_set_str(b,"4016751268155181878780762958485595786864539552267966173503519872828650364102948010668074636189225260885467109630266705699778224615794326630829490560742794",10);
  70.         mpz_init_set_str(m,"9913033868557133667485171584043308894354447793802229576147965224579616862439637051468618269920939124273132778910108308880842351094700094598897767100778663",10);
  71.         const unsigned long long  n = 1048576;
  72.  
  73.  
  74.  
  75.         KeyValue* mem = (KeyValue*)calloc(n+1, sizeof(KeyValue));
  76.        
  77.  
  78.         KeyValue* memp = mem;
  79.         mpz_t tmp; mpz_init_set(tmp,b);
  80.         mpz_t tmp2; mpz_init(tmp2);
  81.        
  82.         uint64_t* export_buffer = new uint64_t[8];
  83.  
  84.  
  85.         for(unsigned int x1=0;x1<=n;++x1){
  86.             mpz_export(export_buffer,NULL,0,sizeof(uint64_t),0,0,tmp);
  87.             memp->key = x1;
  88.             memp->value = export_buffer[0];
  89.             mpz_mul(tmp, tmp, a);
  90.             mpz_mod(tmp,tmp,m);
  91.             memp++;
  92.         }
  93.         printf("Table is build!\n");
  94.         qsort(mem,n+1,sizeof(KeyValue),compare);
  95.  
  96.         printf("Table is sorted!\n");
  97.         FILE* f = fopen("test13.txt","w");
  98.         for(unsigned int i=0;i<n;++i){
  99.             fprintf(f,"%u\n",mem[i].value);
  100.         }
  101.         fclose(f);
  102.         FILE* f2 = fopen("test2.txt","w");
  103.         mpz_set_ui(tmp, 1);
  104.         mpz_powm_ui(tmp2,a,n,m);
  105.         unsigned int x1;
  106.         mpz_t tmp3; mpz_init(tmp3);
  107.         for(unsigned int x0=0;x0<=n;++x0){
  108.             mpz_export(export_buffer,NULL,0,sizeof(uint64_t),0,0,tmp);
  109.             fprintf(f2,"%u\n",export_buffer[0]);
  110.             if(find_in_array(mem, n+1, export_buffer[0], x1)){
  111.                 mpz_set_ui(tmp3, x0*n - x1);
  112.                 if(mpz_cmp(tmp3, m) == -1){
  113.                     printf("Answer: %u\n", x0*n - x1);
  114.                     break;
  115.                 }
  116.             }
  117.             mpz_mul(tmp, tmp, tmp2);
  118.             mpz_mod(tmp,tmp,m);
  119.         }
  120.         fclose(f2);
  121.         free(mem);
  122.         delete[] export_buffer;
  123.         return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment