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为根节点的子 ...
随机推荐
- 快速构建Windows 8风格应用37-常见发布注意事项
原文:快速构建Windows 8风格应用37-常见发布注意事项 引言 通常我们发布Windows Store应用失败后,会返回一些错误需要我们去修改.我之前在给学生做培训的时候发现大部分同学应用被打回 ...
- Asterisk 未来之路3.0_0005
原文:Asterisk 未来之路3.0_0005 第二章: Asterisk的架构 Asterisk 和其他众多传统的PBX是有区别的,拨号方案针对各种通道处理本质上采用同一种方式. 在传统的PB ...
- 安装uBuntu操作系统 - 初学者系列 - 学习者系列文章
uBuntu是一款不错的Linux操作系统,在上面的应用软件不少,就是说它的支持率挺高.下面就对这款操作系统的安装做下介绍. 1. 下载uBuntu安装文件 打开中文页面.http://www.ub ...
- Js Date泣血整理
原文:Js Date泣血整理 JS Date 对象用于处理日期和时间. 创建 Date 对象的语法: var myDate=new Date() Date 对象会自动把当前日期和时间保存为其初始值. ...
- android中怎么调整字体的间距和行间距
在网页中都是很轻松的就可以调整间距的.在android中,我个人并没有去设置过. 下面就来说说android中的间距问题. 原文:http://blog.csdn.net/fancylovejava/ ...
- ajax的post请求
get和post是http请求方法最主要的两种方式. post: 来个例子test.html <!doctype html> <html lang="en"> ...
- viewstate cookie和session原理回顾
--个人理解会存在些错误仅供参考!!! ----浏览器保持会话状态原理 用户发送一次请求,服务器端会检索报文中是否存在sessionid不存在,就分配一个写到cookie当中,存在浏览器的缓存中,当再 ...
- VS多平台开发
Xamarin技术文档------VS多平台开发 此技术业余时间研究,仅供大家学习参考,不涉及深入研究,有一定开发基础的人员,应该都能较快上手. 一.简介 Xamarin始创于2011年,旨在使移 ...
- JavaScript精彩范例(1)——Jquery EasyUI应用的一个框架实例
从网上看到的,非常漂亮,放在这里和大家分享一下,作者是疯狂秀才 这是截图 >>这是下载地址<<
- Visual Studio 2013 上使用 Github
教你如何在 Visual Studio 2013 上使用 Github 介绍 我承认越是能将事情变简单的工具我越会更多地使用它.尽管我已经知道了足够的命令来使用Github,但我宁愿它被集成到IDE中 ...