Beta Round #9 (酱油杯noi考后欢乐赛)随机数生成器

题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFnoi%E8%80%83%E5%90%8E%E6%AC%A2%E4%B9%90%E8%B5%9B%29/%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%94%9F%E6%88%90%E5%99%A8

题解:这题做的我也是醉了。。。

一阶递推然后还要翻转,那矩阵是不能做了。。。然后发现好像是不可做题。。。简要题解说是循环节?

如果出现以前出现过的显然可以输出ans了,可是模数这么大,如果模数只有100W的话很容易出现,20亿怎么保证会在不超时的情况下重复?

唉?我想到了什么?生日攻击!如果有23个人,那么他们中有同生日的概率超过50%。

然后在hash killer II中,有这样的结论:

生日攻击:如果你在n个数中随机选数,那么最多选√n次就能选到相同的数(不考虑Rp broken)

同样的,这题的Hash值在0到1000000007.

那就要选差不多10^5次

唯一注意的是l要取大,使得方案数超过Mod

否则就不可能有2个数有相同的Hash值

所以我们可以暴力模拟前面的直到出现重复。

代码:

 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 
23 #define inf 1000000000
24 
25 #define maxn 1000000+5
26 
27 #define maxm 500+100
28 
29 #define eps 1e-10
30 
31 #define ll long long
32 
33 #define pa pair<int,int>
34 
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36 
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38 
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40 
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42 
43 #define mod 1000000007
44 
45 using namespace std;
46 
47 inline ll read()
48 
49 {
50 
51     ll x=0,f=1;char ch=getchar();
52 
53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
54 
55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
56 
57     return x*f;
58 
59 }
60 int c[maxn];
61 map<ll,int>mp;
62 
63 int main()
64 
65 {
66 
67     freopen("input.txt","r",stdin);
68 
69     freopen("output.txt","w",stdout);
70 
71     ll a=read(),b=read(),m=read(),x=read(),n=read();
72     for(ll i=1;i<=n;i++)
73     {
74         ll y=(a*x+b)%m;x=0;
75         for(;y;y/=10)x=10*x+y%10;
76         x%=m;
77         y=mp[x];
78         if(y){printf("%d
",c[y+(n-i+1-1)%(i-1-y+1)+1-1]);return 0;}else c[mp[x]=i]=x;
79     }
80     cout<<x<<endl;
81 
82     return 0;
83 
84 } 
View Code
原文地址:https://www.cnblogs.com/zyfzyf/p/4120403.html