Advertisement
Guest User

Untitled

a guest
May 27th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void swap(int* a, int* b)
  5. {
  6. int t = *a;
  7. *a = *b;
  8. *b = t;
  9. }
  10.  
  11. int partition(int *arr, int l, int r) {
  12. int x = arr[r];
  13. int i = l-1;
  14. for (int j = l; j <= r; j++)
  15. if (arr[j] <= x)
  16. swap(&arr[++i], &arr[j]);
  17. return i;
  18. }
  19.  
  20. int nth(int *arr, int n, int len) {
  21. int l = 0, r = len - 1;
  22. while(1) {
  23. int pos = partition(arr,l,r);
  24. if (pos < n)
  25. l = pos + 1;
  26. else if (pos > n)
  27. r = pos - 1;
  28. else
  29. return arr[n-1];
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. int Q,V,P,N,K;
  36. FILE *input,*output;
  37. input = fopen("input.txt", "r");
  38. output = fopen("output.txt", "w");
  39. fscanf(input, "%i%i%i%i%i",&Q,&V,&P,&N,&K);
  40. int A[N];
  41. A[0]=P;
  42. for (int i=1; i<N;i++) {
  43. A[i]=(A[i-1]*Q )%V;
  44. }
  45. fprintf(output,"%i",nth(A,K, N));
  46.  
  47. fclose(output);
  48. fclose(input);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement