HDU 1014 Uniform Generator(最大公约数,周期循环)

#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(void)
{
    int s,m,i;
    /*int seed=0;
    int a[10005]={0};
    //memset(a,0,sizeof(a));*/
    while(scanf("%d%d",&s,&m)!=EOF);
    {
        int seed=0;
        int a[10005]={0};
        while(!a[seed])//
        {
            a[seed]=1;//若本位0,变换标志位为1,直到出现循环到头,标志过了的就跳出
            seed=(seed+s)%m;//seed根据随机数公式变化
        }
        for(i=0;i<m;i++)//范围0-m
        {
            if(a[i]==0)//只要有没出现在0~m-1的数就不好
            {
                printf("%10d%10d    Bad Choice\n\n",s,m);
                break;
            }
        }
        if(i==m){ //跳出循环后,刚好以m为周期
            printf("%10d%10d    Good Choice\n\n",s,m);
        }
    }
}

  

#include<iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int gcd(int a,int b)
{
    if(a<b)
        swap(a,b);
    return b?gcd(b,a%b):a;
}
int main()
{
    int i,j,s,m;
    while(~scanf("%d%d",&s,&m))
    {
        if(gcd(s,m)==1)
        {
            printf("%10d%10d    Good Choice\n\n",s,m);
        }
        else
        {
            printf("%10d%10d    Bad Choice\n\n",s,m);
        }
    }
    return 0;
}

  

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int step,mod,seed,count;
  while(scanf("%d%d",&step,&mod)!=EOF)
  {
    seed=0,count=1;
    do
    {
      seed=(seed+step)%mod;
      count++;                 
    }while(seed!=0);  
    count--;
    printf("%10d%10d    ",step,mod);
    if(count==mod)
      printf("%s\n","Good Choice"); 
    else
      printf("%s\n","Bad Choice"); 
    printf("\n");                                  
  }
  system("pause");
  return 0;    
}

  

#include<iostream>
using namespace std;

int main()
{
    int s,m,x,c;
    while(scanf("%d%d",&s,&m)!=EOF)
	{
        x=c=0;
        do
		{
            x=(x+s)%m;
            ++c;
        } while(x!=0);

        if(c==m)
            printf("%10d%10d    Good Choice\n\n",s,m);
        else
            printf("%10d%10d    Bad Choice\n\n",s,m);
    }
    return 0;
}

  

posted @ 2017-07-18 09:55  Roni_i  阅读(168)  评论(0编辑  收藏  举报