Pseudo-Random Numbers 

Computers normally cannot generate really random numbers, but frequently are used to generate sequences of pseudo-random numbers. These are generated by some algorithm, but appear for all practical purposes to be really random. Random numbers are used in many applications, including simulation.

A common pseudo-random number generation technique is called the linear congruential method. If the last pseudo-random number generated was L, then the next number is generated by evaluating (  , where Z is a constant multiplier, I is a constant increment, and M is a constant modulus. For example, suppose Z is 7, I is 5, and M is 12. If the first random number (usually called the seed) is 4, then we can determine the next few pseudo-random numbers are follows:

As you can see, the sequence of pseudo-random numbers generated by this technique repeats after six numbers. It should be clear that the longest sequence that can be generated using this technique is limited by the modulus, M.

In this problem you will be given sets of values for ZIM, and the seed, L. Each of these will have no more than four digits. For each such set of values you are to determine the length of the cycle of pseudo-random numbers that will be generated. But be careful: the cycle might not begin with the seed!

Input

Each input line will contain four integer values, in order, for ZIM, and L. The last line will contain four zeroes, and marks the end of the input data. L will be less than M.

Output

For each input line, display the case number (they are sequentially numbered, starting with 1) and the length of the sequence of pseudo-random numbers before the sequence is repeated.

Sample Input

7 5 12 4
5173 3849 3279 1511
9111 5309 6000 1234
1079 2136 9999 1237
0 0 0 0

Sample Output

Case 1: 6
Case 2: 546
Case 3: 500
Case 4: 220
#include<stdio.h>
int main(void)
{
int z,i,m,l[10005],count=0,f;
while(scanf("%d%d%d%d",&z,&i,&m,&l[0])==4)
{
if(!z&&!i&&!m&&!l[0]) break;
count++;
int t=0,j,k;
while(1)
{
t++;
l[t]=(z*l[t-1]+i)%m;
for(j=0;j<t;j++)
if(l[t]==l[j])
goto print;
}
print: printf("Case %d: %d\n",count,t-j);
}
return 0;
}

350 - Pseudo-Random Numbers的更多相关文章

  1. Pseudo Random Nubmer Sampling

    Pseudo Random Nubmer Sampling https://en.wikipedia.org/wiki/Inverse\_transform\_sampling given a dis ...

  2. C++ Standard-Library Random Numbers

    Extracted from Section 17.4 Random Numbers, C++ Primer 5th. Ed. The random-number library generates ...

  3. Random Numbers Gym - 101466K dfs序+线段树

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  4. Generating Gaussian Random Numbers(转)

    Generating Gaussian Random Numbers http://www.taygeta.com/random/gaussian.html This note is about th ...

  5. 2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest K - Random Numbers (dfs序 线段树+数论)

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  6. [Swift] 随机数 | Random numbers

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Random numbers

    Most computer programs do the same thing every time they execute, given the same inputs, so they are ...

  8. [Functional Programming] Pull Many Random Numbers in a Single State ADT Transaction

    We have the ability to select a single random card from a pile of twelve cards, but we would like to ...

  9. [Python] Generating random numbers using numpy lib

    import numpy as np def test_run(): data=np.random.random((3,4)) """ [[ 0.80150549 0.9 ...

  10. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

随机推荐

  1. IS2009制作Oracle 静默安装包(一)感谢空白先生特许授权

    原文:IS2009制作Oracle 静默安装包(一)感谢空白先生特许授权 上一篇: MyEclipse中消除frame引起的“the file XXX can not be found.Please ...

  2. thinkphp 支付宝错误 Class 'Think' not found

    Class 'Think' not found D:\www\DonatePlatform\ThinkPHP\Extend\Vendor\alipay\lib\alipay_submit.class. ...

  3. 使用Vim或Codeblocks格式化代码

    在网上的代码,有很多的代码都是丢失缩进的,几行还好,手动改改,多了呢,不敢想象,没有缩进的代码.别说排错,就是阅读都是困难的,还好,有两个常用工具可以轻松的解决问题. (一)Vim(简单方便,可将代码 ...

  4. 谈一谈struts2和springmvc的拦截器

    最近涉及到了两个项目,都需要考虑全局的拦截器,其功能就是判断session的登陆状态,如果session信息完好,可以从中取得相应的信息,则放行,否则拦截,进入重定向的uri. 既然是全局的拦截器,其 ...

  5. TodoList开发笔记 – Part Ⅳ

    跌跌撞撞总算是把客户端开发好了,后台的代码因为不复杂,就写了些单元测试跑一跑就算通过了,大部分时间都是在调整脚本. 这一节开始部署TodoList项目. 一.了解IIS(Internet Infoma ...

  6. 一步一步实现基于Task的Promise库(五)waitFor和waitForAny的实现

    在实现waitFor方法之前,我们先要搞明白下面这些问题: 1. waitFor方法的形参有限制吗? 没有!如果形参是Task类型,不应该启动Task,如果是function类型,会执行方法.所以wa ...

  7. 自定义MVP .net框架

      一个自定义MVP .net框架 AngleFrame   摘要:本篇是本人在完成.net平台下一个项目时,对于MVP框架引发的一些思考,以及开发了一个小型的配置型框架,名字叫作AngleFrame ...

  8. 看AngularJS

    最近一段时间一直在看AngularJS,趁着一点时间总结一下. 官网地址:http://angularjs.org/ 先推荐几个教程 1. AngularJS入门教程 比较基础,是官方Tutorial ...

  9. .net实现依赖注入

    .net实现依赖注入 1. 问题的提出 开发中,尤其是大型项目的开发中,为了降低模块间.类间的耦合关系,比较提倡基于接口开发,但在实现中也必须面临最终是“谁”提供实体类的问题.Martin Fowle ...

  10. baidu 200兆SVN代码服务器

    转 今天心情好,给各位免费呈上200兆SVN代码服务器一枚,不谢!   开篇先给大家讲个我自己的故事,几个月前在网上接了个小软件开发的私活,平日上班时间也比较忙,就中午一会儿休息时间能抽出来倒腾着去做 ...