HDOJ 1014 Uniform Generator(公约数问题)

Problem Description
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form

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 the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.

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

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

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

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

Output
For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either “Good Choice” or “Bad Choice” left-justified starting in column 25. The “Good Choice” message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message “Bad Choice”. After each output test set, your program should print exactly one blank line.

Sample Input
3 5
15 20
63923 99999

Sample Output

     3         5    Good Choice

    15        20    Bad Choice

 63923     99999    Good Choice

题目这么长,其实意思就是判断输入的2个数的最大公约数是不是1.
如果是输出:Good Choice
否则输出:Bad Choice
还有注意的是:输出后面跟一个空行。其实我也不知道是输出之间还是输出之后(英语水平有限)。我是输出之后跟一个空行,AC了。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            if(gcd(a,b)==1){
                System.out.printf("%10d%10d",a,b);
                System.out.println("    Good Choice");
            }else{
                System.out.printf("%10d%10d",a,b);
                System.out.println("    Bad Choice");
            }
            System.out.println();
        }
    }

    //递归求最大公约数
    private static int gcd(int a, int b) {
        return b==0?a:gcd(b,a%b);
    }

}
时间: 2024-09-20 16:36:43

HDOJ 1014 Uniform Generator(公约数问题)的相关文章

UVa 408 Uniform Generator:最大公约数&amp;amp;证明

408 - Uniform Generator Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=349 Computer simulations often require random numbers. One way to generate pseudo

HDOJ(HDU) 2503 a/b + c/d(最大公约数问题)

Problem Description 给你2个分数,求他们的和,并要求和为最简形式. Input 输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正整数a,b,c,d(0 import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t =sc.nextInt()

C++:随机数生成器(random-number generator) 详解

随机数, C语言的函数是rand(), C++则是随机数生成器(random-number generator) = 分布对象(distribution object) + 引擎(engine); 使函数每次生成不同的随机数, 需要使用静态(static)局部变量, 这样分布对象和引擎就能保持(hold)状态(state), 每次都生成一个新的; 生成随机的整数, 使用分布对象uniform_int_distribution<>, 默认模板参数是int; 生成随机的浮点数, 使用分布对象uni

编程之美之2.7 最大公约数问题

问题: 求两个数的最大公约数 解法一: 欧几里得辗转相除法: f(x,y) = GCD(x,y), 取k = x / y, b = x % y,则:x = k*y + b; 如果一个数能整除x,y,则它也能整除b,y: 而且能整除b,y的数必能整除x,y,即x,y和b,y的公约数是相同的,其最大公约数也是相同的,即f(x,y) = f(y ,x % y) (x>=y>0) 例如,计算a = 1071和b = 462的最大公约数的过程如下:从1071中不断减去462直到小于462(可以减2次,即

Hibernate主键生成方式 Key Generator

Hibernate主键生成方式 Key Generator 主键产生器 可选项说明: 1) assigned 主键由外部程序负责生成,无需Hibernate参与. 2) hilo 通过hi/lo 算法实现的主键生成机制,需要额外的数据库表保存主键生成历史状态. 3) seqhilo 与hilo 类似,通过hi/lo 算法实现的主键生成机制,只是主键历史状态保存在Sequence中,适用于支持Sequence的数据库,如Oracle. 4) increment 主键按数值顺序递增.此方式的实现机制

CssGaga教程:AutoSprite(CSS Sprite Generator)

文章简介:CssGaga – AutoSprite(CSS Sprite Generator). 市面上有一些sprite生成器,要么是要人工调整图片位置,要么要拷贝粘贴代码,用起来总是觉得不够爽,CssGaga使用了不同的思路,希望能解放你的双手:) 使用时选中AutoSprite即可开启此功能,下面通过一个例子来说明: 比如HTML: <s class="i1"></s><s class="i2"></s><

在线调试css3.0的工具:css3 please和css3 generator

网页制作Webjx文章简介:两个在线调试css3.0的工具,css3 please和css3 generator. 两个在线调试css3.0的工具,css3 please和css3 generator.css3.0增加的新属性,如投影.渐变.旋转.圆角等等!这些新标准属性在ie6.ie7.ie8浏览器版本里得不到很好的支持,相信ie以后的新版本也会支持这些新属性的.目前ie6.ie7.ie8浏览器不支持这些属性并不能说明ie就实现不了css3.0新属性的效果!css3please.com网站为我

输入两个正整数m和n并求其最大公约数和最小公倍数

查看全套"c语言习题集" 题目: 输入两个正整数m和n,求其最大公约数和最小公倍数. 1.程序分析:利用辗除法. 2.程序源代码: #include "stdio.h"#include "conio.h"main(){ int a,b,num1,num2,temp; printf("please input two numbers:\n"); scanf("%d,%d",&num1,&num

C++求二个数的最大公约数与最小公倍数实例

1.求二个数的最大公约数: #include <iostream.h> int maxye(int a,int b) { int temp; while(a%b) { temp=b; b=a%b; a=temp; } return b; } void main() { int aa,bb; cout<<"请输入第一个数:"; cin>>aa; cout<<"\n请输入第二个数:"; cin>>bb; cou