Uniform Generator

1014 Uniform Generator

Time Limit: 2000/1000 MS (Java/Others)    MemoryLimit: 65536/32768 K (Java/Others)
Total Submission(s): 17950    Accepted Submission(s): 7046


Problem Description

Computer simulations often require randomnumbers. One way to generate pseudo-random numbers is via a function of theform

seed(x+1) = [seed(x) + STEP] % MOD

where '%' is the modulus operator. 

Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1.One problem with functions of this form is that they will always generate thesame pattern over and over. In order to minimize this effect, selecting theSTEP and MOD values carefully can result in a uniform distribution of allvalues between (and including) 0 and MOD-1. 

For example, if STEP = 3 and MOD = 5, the function will generate the series ofpseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, allof the numbers between and including 0 and MOD-1 will be generated every MODiterations of the function. Note that by the nature of the function to generatethe same seed(x+1) every time seed(x) occurs means that if a function willgenerate all the numbers between 0 and MOD-1, it will generate pseudo-randomnumbers uniformly with every MOD iterations. 

If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (orany other repeating series if the initial seed is other than 0). This is a poorselection of STEP and MOD because no initial seed will generate all of thenumbers from 0 and MOD-1. 

Your program will determine if choices of STEP and MOD will generate a uniformdistribution of pseudo-random numbers. 

 

 

Input

Each line of input will contain a pair ofintegers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).

 

 

Output

For each line of input, your program shouldprint the STEP value right- justified in columns 1 through 10, the MOD valueright-justified in columns 11 through 20 and either "Good Choice" or"Bad Choice" left-justified starting in column 25. The "GoodChoice" message should be printed when the selection of STEP and MOD willgenerate all the numbers between and including 0 and MOD-1 when MOD numbers aregenerated. Otherwise, your program should print the message "BadChoice". After each output test set, your program should print exactly oneblank line.

 

 

Sample Input

3 5

15 20

63923 99999

 

 

Sample Output

        3         5    Good Choice

 

       15        20    Bad Choice

 

    63923     99999    Good Choice

#include<iostream>

#include<string.h>

#include<stdio.h>

using namespace std;

int f(int t, int m)

{

   for(int i=2;i<=t&&i<=m;i++)

   if(t%i==0&&m%i==0)

   return 0;

   return 1;

}

int main()

{

        intt,m;

   while(cin>>t>>m)

    {

       

        printf("%10d%10d   ",t,m); 

      if(f(t,m))

                       printf("GoodChoice\n\n"); 

       else

                       printf("BadChoice\n\n"); 

}

        return0;

}

这道题目一开始想的是用随机数写的,但是写着写着不对,就只好找规律,带了几个数,发现这个题目其实就是求最大公约数,俩个数的最大公约数大于1就是Bad 否则就是Good,

这就简单了转化成求最大公约数了,这里为了控制格式,加入了c的头文件,来是用printf,也行,以后也可以在有格式控制时这样写了。我在这里找了很多关于最大公约数的模板,以及多个数的:

 

①、函数嵌套调用

其算法过程为: 前提:设两数为a,b设其中a 做被除数,b做除数,temp为余数

1、大数放a中、小数放b中;

2、求a/b的余数;

3、若temp=0则b为最大公约数;

4、如果temp!=0则把b的值给a、temp的值给a;

5、返回第第二步;

int divisor (inta,int b)    /*自定义函数求两数的最大公约数*/

{  int temp;          /*定义整型变量*/

  if(a<b)             /*通过比较求出两个数中的最大值和最小值*/

    { temp=a;a=b;b=temp;} /*设置中间变量进行两数交换*/

   while(b!=0)           /*通过循环求两数的余数,直到余数为0*/

    {

      temp=a%b;

      a=b;             /*变量数值交换*/

      b=temp;

    }

  return (a);            /*返回最大公约数到调用函数处*/

}

 

L②、函数递归调用

int gcd (int a,int b)

{  if(a%b==0)

       return b;   

else  

      return gcd(b,a%b);

  }

2、穷举法(利用数学定义)

穷举法(也叫枚举法)穷举法求两个正整数的最大公约数的解题步骤:从两个数中较小数开始由大到小列举,直到找到公约数立即中断列举,得到的公约数便是最大公约数 。

①、定义1:对两个正整数a,b如果能在区间[a,0]或[b,0]内能找到一个整数temp能同时被a和b所整除,则temp即为最大公约数。

int divisor (inta,int b) /*自定义函数求两数的最大公约数*/

{

    int temp;          /*定义义整型变量*/

    temp=(a>b)?b:a;    /*采种条件运算表达式求出两个数中的最小值*/

    while(temp>0)    

    {

       if (a%temp==0&&b%temp==0) /*只要找到一个数能同时被a,b所整除,则中止循环*/

          break;   

       temp--;      /*如不满足if条件则变量自减,直到能被a,b所整除*/

    }

  return (temp); /*返回满足条件的数到主调函数处*/

}

int gcd(int m, int n)

{

    int temp;

 

    while(n)

    {

        temp = m % n;

        m = n;

        n = temp;

    }

 

    return (m);

}

 

int simple_gcd(int m, int n)

{

    int i = 0;

    int temp = 0;

    int gcd = 1;

   

    if( m > n)

    {

        temp = n;

    }

    else

    {

        temp = m;

    }

 

    for(i = 2; i <= temp; i ++)

    {

        if(m % i == 0 && n % i == 0)

        {

            gcd = i;

        }

    }

 

    return gcd;

}

 

int recur_gcd(int m, int n)

{

    if( n == 0)

    {

 

        return m;

    }

 

    return recur_gcd(n, m % n);

}

多个数的最大公约数:

1.  INT gcd(INT a, INT b);  

2.    

3.  /***************************************************************************** 

4.  函数:GcdN                                                                * 

5.  参数:pnNum:数字数组.                                                         * 

6.  *      nLen:数字个数.                                                        * 

7.  返回值:返回多个数字的最大公约数.                                          * 

8.  功能:计算并返回多个数字的最大公约数.                                        * 

9.  说明:调用函数:INT gcd(INT a, INT b);                                         * 

10. *****************************************************************************/  

11. INT GcdN(INT *pnNum, INT nLen)  

12. {  

13.     INT i;  

14.     INT nRes;  

15.       

16.     if (nLen < 0)  

17.         return -1;  

18.     if (1 == nLen)  

19.         return pnNum[0];  

20.   

21.     nRes = gcd(pnNum[0], pnNum[1]);  

22.     for (i=2; i<nLen; i++)  

23.         nRes = gcd(nRes, pnNum[i]);  

24.   

25.     return nRes;  

26. }  

27.   

28. /***************************************************************************** 

29. 函数:gcd                                                                     * 

30. 参数:a:数字1.                                                              * 

31. *      b:数字2.                                                                * 

32. 返回值:返回ab的最大公约数.                                               * 

33. 功能:计算并返回ab的最大公约数(递归版本).                               * 

34. *****************************************************************************/  

35. /* 

36. INT gcd(INT a, INT b) 

37. { 

38.     if (0 == b) 

39.         return a; 

40.     return gcd(b, a % b); 

41. } 

42. */  

43.   

44. /***************************************************************************** 

45. 函数:gcd                                                                     * 

46. 参数:a:数字1.                                                              * 

47. *      b:数字2.                                                                * 

48. 返回值:返回ab的最大公约数.                                               * 

49. 功能:计算并返回ab的最大公约数(循环版本).                               * 

50. *****************************************************************************/  

51. INT gcd(INT a, INT b)  

52. {  

53.     INT nTem;  

54.   

55.     while (0 != b)  

56.     {  

57.         nTem = b;  

58.         b = a % b;  

59.         a = nTem;  

60.     }  

61.   

62.     return a;  

63. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值