首页 > 其他分享 >UVa 350 Pseudo-Random Numbers (伪随机数的循环长度)

UVa 350 Pseudo-Random Numbers (伪随机数的循环长度)

时间:2023-04-12 13:04:10浏览次数:54  
标签:count Random pseudo random Pseudo d% vis Numbers numbers


350 - Pseudo-Random Numbers

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=286

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



注意:循环不一定回到开头,比如样例3中有(3111*1234+5309)%6000=(3111*5234+5309)%6000,这里循环回到了第二个数。


完整代码:


/*0.015s*/

#include<cstdio>
#include<cstring>

int father[10000];
bool vis[10000];

int main()
{
	int cas = 0, k, b, m, x, temp, count;
	while (scanf("%d%d%d%d", &k, &b, &m, &x), k || b || m || x)
	{
		memset(father, 0, sizeof(father));
		memset(vis, 0, sizeof(vis));
		vis[x] = true;
		while (true)
		{
			temp = x;
			x = (k * x + b) % m;
			father[x] = temp;
			if (vis[x]) break;
			vis[x] = true;
		}
		temp = x;
		count = 0;
		do
		{
			x = father[x];
			++count;
		}
		while (x != temp);
		printf("Case %d: %d\n", ++cas, count);
	}
	return 0;
}


但事实是循环要么回到起点,要么回到第二个数:

/*0.012s*/

#include<cstdio>

int main()
{
	int cas = 0, k, b, m, x, beg, beg2, count;
	while (scanf("%d%d%d%d", &k, &b, &m, &x), k || b || m || x)
	{
		k %= m, b %= m;
		beg = x;
		beg2 = x = (k * x + b) % m;
		count = 1;
		while (x != beg)
		{
			x = (k * x + b) % m;
			if (x == beg2) break;
			++count;
		}
		printf("Case %d: %d\n", ++cas, count);
	}
	return 0;
}





标签:count,Random,pseudo,random,Pseudo,d%,vis,Numbers,numbers
From: https://blog.51cto.com/u_5535544/6185486

相关文章

  • UVa 443 / POJ 2247 Humble Numbers (4因子-丑数&STL灵活运用)
    443-HumbleNumbersTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=384http://poj.org/problem?id=2247Anumberwhoseonlyprimefactorsare2,3,5or7isc......
  • Semi-prime H-numbers UVA - 11105
     所有形如4n+1(n为非负整数)的数叫H数。定义1是唯一的单位H数,H素数是指本身不是1,且不能写成两个不是1的H数的乘积。H-半素数是指能写成两个H素数的乘积的H数(这两个数可以相同也可以不同)。 例如,25是H-半素数,但125不是。输入一个H数h(h≤1000001),输出1~h之间有多少个H-半素数。......
  • numpy.random.choice(a, size=None, replace=True, p=None)
    importnumpyasnpimportrandomlist_a=["a","b","c","d","e","f","g"]get_=np.random.choice(list_a,4,replace=False)print(get_)输出:['a''f''c�......
  • Sum of Consecutive Prime Numbers UVA - 121
     #include<iostream>#include<cstring>#include<cmath>#include<algorithm>usingnamespacestd;constintM=1e4+33;intb[M],c[M],tot;ints[M];voidinit(inttop){memset(b,1,sizeofb);b[1]=0;inti,j;......
  • cpp: random
     //RandomlySampled.h:此文件包含"RandomlySampled"类。十个常用排序算法C++11//2023年4月9日涂聚文GeovinDuedit.#pragmaonce#ifndefRANDOMLYSAMPLED_H#defineRANDOMLYSAMPLED_H#include<iostream>#include<string>#include<vector&g......
  • 迁移学习(SPI)《Semi-Supervised Domain Adaptation by Similarity based Pseudo-label
    论文信息论文标题:Semi-SupervisedDomainAdaptationbySimilaritybasedPseudo-labelInjection论文作者:AbhayRawat, IshaDua, SauravGupta, RahulTallamraju 论文来源:PublishedinECCVWorkshops5September2022论文地址:download 论文代码:download视屏讲解:click......
  • 迁移学习《Efficient and Robust Pseudo-Labeling for Unsupervised Domain Adaptatio
    论文信息论文标题:EfficientandRobustPseudo-LabelingforUnsupervisedDomainAdaptation论文作者:HochangRhee、NamIkCho论文来源:2019——ICML论文地址:download 论文代码:download视屏讲解:click1摘要问题:无监督域适应传统方法将超过一定置信度阈值的数据视为目标域......
  • (C#)Random实现随机点名
    namespaceWindowsFormsApp3{publicpartialclassForm1:Form{//实例化字符串,设置字符串长度与内容string[]student=newstring[7]{"张三","李四","王五","赵六","hello","world","helloworl......
  • cpp generate random array then sort by quick sort
    #include<chrono>#include<ctime>#include<iomainp>#include<iostream>#include<random>#include<sstream>std::stringget_time_now(){std::chrono::time_point<std::chrono::high_resolution_clock>now=st......
  • 随机生成工具 RandomValueUtil
    packageentity;/*****@Author:www.itheima.com*@Description:itheima*****/publicclassRandomValueUtil{publicstaticStringbase="abcdefghijklmnopqrstuvwxyz0123456789";privatestaticStringfirstName="赵钱孙李周吴郑王冯陈褚卫蒋沈......