350 - Pseudo-Random Numbers
| 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 Z, I, M, 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 Z, I, M, 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的更多相关文章
- Pseudo Random Nubmer Sampling
Pseudo Random Nubmer Sampling https://en.wikipedia.org/wiki/Inverse\_transform\_sampling given a dis ...
- C++ Standard-Library Random Numbers
Extracted from Section 17.4 Random Numbers, C++ Primer 5th. Ed. The random-number library generates ...
- Random Numbers Gym - 101466K dfs序+线段树
Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...
- Generating Gaussian Random Numbers(转)
Generating Gaussian Random Numbers http://www.taygeta.com/random/gaussian.html This note is about th ...
- 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 ...
- [Swift] 随机数 | Random numbers
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Random numbers
Most computer programs do the same thing every time they execute, given the same inputs, so they are ...
- [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 ...
- [Python] Generating random numbers using numpy lib
import numpy as np def test_run(): data=np.random.random((3,4)) """ [[ 0.80150549 0.9 ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
随机推荐
- 于CentOS 6 安装 Wordpress
1.两种方式给Wordpress 首先,你可以去wordpress最新的官方网站看看wordpress多少下载.例wordpress 3.9.1下载地址: http://cn.wordpress.or ...
- C++ AMP 介绍(两)
C++ AMP 介绍(两) 最后更新:2014-05-02 读前提:<C++ AMP介绍(一个)> 周边环境:Windows 8.1 64bit英文版,Visual Studio 2013 ...
- linux大杂烩
linux: 进入hbase后不能移动光标和删除 Options-Session Options -- Terminal --右边的Terminal中选择linux然后点击OK就好了
- C#用Open与Add方法打开word文档的区别
C#打开word文档常用有两种方法:Add与Open. Microsoft.Office.Interop.Word._Document doc = (Document)appWord.Document ...
- .net图片自动裁剪白边函数案例
1.项目要求上传白底的图片要进行裁剪白边,于是同事谢了个函数感觉很好用. 2. #region 剪切白边 /// <summary> /// 剪切白边 /// </summary&g ...
- IOC 容器在 ASP.NET MVC 中的应用
IOC 容器在 ASP.NET MVC 中的应用 IOC:Inversion Of Control 翻译为控制反转,我们在面向对象软件开发过程中,一个应用程序它的底层结构可能由N种不同的构件来相互协作 ...
- 求解轨道力学二体意义下的Lambert方程(兰伯特方程)的Fortran程序
轨道力学中二体问题下求解兰伯特方程. 老外写的Matlab程序,我把它转成了Fortran程序. !************************************************** ...
- T4模版
T4模版 这讲适合所有的MVC程序 很荣幸,我们的系统有了体验的地址了.演示地址 之前我们发布了一个简单的代码生成器,其原理就是读取数据库的表结构,生成文本的一个方式来生成代码! 为了替代重复的劳动, ...
- CEGUI添加自定义控件
用CEGUI做界面将近3个月了,比较忙,而且自己懒了许多,没能像以前那样抽出大量时间研究CEGUI,查阅更多的资料书籍,只是在工作间隙,将官网上的一些资料和同事推荐的<CEGUI深入解析> ...
- [Usaco2008 Dec]Patting Heads 轻拍牛头[筛法]
Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶 ...