2007-02-24

【Q350】Pseudo-Random Numbers

題目: http://acm.uva.es/p/v3/350.html

程式下載: http://yaushung.googlepages.com/2007022401.c

程式內容:

#include <stdio.h>
#include
<stdlib.h>

int main() {
int Z, I, M, L ;
int temp, count ;
int next ; /* But be careful: the cycle might not begin with the seed! */

int index = 1 ; /* 計算第幾個case */
while(1) {
scanf("%d %d %d %d", &Z, &I, &M, &L) ;
if(Z==0 && I==0 && M==0 && L==0)
break ;
count = 1 ;
Z = Z % M ;
I = I % M ;
next = temp = (Z*L+I)%M ;
while(temp != L) {
temp = (Z*temp+I)%M ;
if(temp == next)
break ;
count++ ;
}
printf("Case %d: %d\n", index++, count) ;
}
return 0 ;
}

沒有留言: