Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void swap(int* a, int* b)
- {
- int t = *a;
- *a = *b;
- *b = t;
- }
- int partition(int *arr, int l, int r) {
- int x = arr[r];
- int i = l-1;
- for (int j = l; j <= r; j++)
- if (arr[j] <= x)
- swap(&arr[++i], &arr[j]);
- return i;
- }
- int nth(int *arr, int n, int len) {
- int l = 0, r = len - 1;
- while(1) {
- int pos = partition(arr,l,r);
- if (pos < n)
- l = pos + 1;
- else if (pos > n)
- r = pos - 1;
- else
- return arr[n-1];
- }
- }
- int main()
- {
- int Q,V,P,N,K;
- FILE *input,*output;
- input = fopen("input.txt", "r");
- output = fopen("output.txt", "w");
- fscanf(input, "%i%i%i%i%i",&Q,&V,&P,&N,&K);
- int A[N];
- A[0]=P;
- for (int i=1; i<N;i++) {
- A[i]=(A[i-1]*Q )%V;
- }
- fprintf(output,"%i",nth(A,K, N));
- fclose(output);
- fclose(input);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement