UVA 350-Pseudo-Random Numbers

来源:互联网 发布:智慧足迹大数据 编辑:程序博客网 时间:2024/05/16 12:39

UVA 350-Pseudo-Random Numbers

题目大意:L = (L*Z+I)%M,求循环长度

解题思路:直接模拟,用数组记录出现的数和这个数是第几个出现的

#include <stdio.h>#include <iostream>#include <string.h>using namespace std;int main() {    int Z, I, M, L;    int m = 0;    int a[10010];    int b[10010];    while(cin >> Z >> I >> M >> L) {        memset(a, 0, sizeof(a));        memset(b, 0, sizeof(b));        m++;        if(Z == 0 && I == 0 && M == 0 && L == 0)            break;        int x = L;        int n = 0;        while(!a[L]) {            a[L] = 1;            b[L] = n++;            L = (Z * L + I) % M;        }        printf("Case %d: %d\n", m, n - b[L]);    }    return 0;}
0 0
原创粉丝点击