Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Created April 8, 2015 10:34
Show Gist options
  • Save henrybear327/2bfef2d3eb181714708e to your computer and use it in GitHub Desktop.
Save henrybear327/2bfef2d3eb181714708e to your computer and use it in GitHub Desktop.
ITSA Problem 5.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main()
{
int cases;
while(scanf("%d", &cases) != EOF) {
while(cases--) {
long long int a, c, m, x_0;
scanf("%lld %lld %lld %lld", &a, &c, &m, &x_0);
//printf("%lld %lld %lld %lld\n", a, c, m, x_0);
/*Xn+1 = ( aXn + c ) mod m */
int record[m];
memset(record, 0, sizeof(record));
int count = 1;
while(1) {
x_0 = (a * x_0 + c) % m;
//printf("x_0 = %lld\n", x_0);
if(record[x_0] == 0) {
record[x_0] = count;
count++;
continue;
} else {
printf("%d\n", count - record[x_0]);
break;
}
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment