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<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<list>
using namespace std;
int a[]; int main()
{
int Z,I,M,L,t=;
while ( scanf("%d%d%d%d",&Z,&I,&M,&L))
{
t=t+;
if (Z*I*M*L==)
break;
memset(a,,sizeof(a));
int k=;
L=(Z*L+I)%M;
while(!a[L])
{
k=k+;
a[L] = ;
L = (Z*L+I)%M;
}
printf("Case %d: %d\n",t,k);
}
return ;
}

最新文章

  1. 001_SPL工作中总结
  2. (Hibernate进阶)Hibernate映射——多对多关联映射(八)
  3. php——用for循环打印半金字塔、金字塔、正方形、倒金字塔、菱形、空心图形等
  4. HTML5全局属性和事件
  5. SQL Server xtype
  6. 错误处理--pure specifier can only be specified for functions
  7. 百篇大计敬本年之系统篇《六》—— Ubuntu 16.04开启 root 超级用户
  8. 在UWSGI和NGINX配合的情况下,配置个别目录上传及超时
  9. UI篇---RadioButton(单选按钮)
  10. 北京Uber优步司机奖励政策(12月3日)
  11. thinkphp带查询条件的分页
  12. 查看Page结构
  13. js随机生成验证码及其颜色
  14. PHP源代码加密
  15. 利用python实现简单随机验证码
  16. LeetCode &amp; Q66-Plus One-Easy
  17. oracle中的listener.ora和tnsnames.ora
  18. webservice调用dll
  19. TWebSocketClient
  20. JS-鼠标彩色拖尾小效果

热门文章

  1. aarch64_m2
  2. aarch64_l4
  3. MySQL5.7之多源复制&amp;Nginx中间件(下)【转】
  4. Team Foundation Server 2010服务器安装
  5. React-Native 之 FlexBox介绍和使用
  6. 十五、springboot集成定时任务(Scheduling Tasks)(二)之(线程配置)
  7. 读书笔记--C陷阱与缺陷(六)
  8. git —— 基本命令以及操作(No.1)
  9. PHP array_key_exists() 函数(判断某个数组中是否存在指定的 key)
  10. 容器计划任务大坑:在alpine容器里,想用非root帐号执行crontab任务