HDU 1014 Uniform Generator(模拟和公式)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1014
Uniform Generator
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33120 Accepted Submission(s): 13137
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.
每一个数都出现一次。这样就均等分布了。就打印Good否则就Bad
#include<bits/stdc++.h> using namespace std; int main() { int n,m,i; int s[100005]; while(cin>>n>>m) { s[0]=0; for(i=1;i<m;i++) s[i]=(s[i-1]+n)%m; sort(s,s+m); for(i=0;i<m;i++) if(s[i]!=i) break; printf("%10d%10d",n,m); if(i==m) cout<<" Good Choice"<<endl<<endl; else cout<<" Bad Choice"<<endl<<endl; } return 0; }
2.公式法
这是网上比较神奇的做法(判断一下两个数是不是互质)
就是比较step和mod的最大公约数是不是1
大佬题解:
本题就是求step和mod如果GCD(最大公约数位1)那么就是Good Choice,否则为Bad Choice
为什么这个结论成立呢?
因为当GCD(step, mod) == 1的时候,那么第一次得到序列:x0, x0 + step, x0 + step…… 那么mod之后,必然下一次重复出现比x0大的数必然是x0+1,为什么呢?
因为(x0 + n*step) % mod; 且不需要考虑x0 % mod的值为多少,因为我们想知道第一次比x0大的数是多少,那么就看n*step%mod会是多少了,因为GCD(step, mod) == 1,那么n*step%mod必然是等于1,故此第一次重复出现比x0大的数必然是x0+1,那么第二次出现比x0大的数必然是x0+2,以此类推,就可得到必然会出现所有0到mod-1的数,然后才会重复出现x0.
当GCD(step, mod) != 1的时候,可以推出肯定跨过某些数了,这里不推了。
然后可以扩展这个结论,比如如果使用函数 x(n) = (x(n-1) * a + b)%mod;增加了乘法因子a,和步长b了;
那么如果是Good Choice,就必然需要GCD(a, mod) == 1,而且GCD(b, mod) == 1;
code:
#include<bits/stdc++.h> using namespace std; typedef long long LL; int gcd(int a,int b)//最大公约数 { if (b==0) return a; return gcd(b, a%b); } int main() { int n,m; while(~scanf("%d %d",&n,&m)) { if(gcd(n,m)==1) printf("%10d%10d Good Choice\n\n",n,m); else printf("%10d%10d Bad Choice\n\n",n,m); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南