codeforces 172B Pseudorandom Sequence Period

http://www.elijahqi.win/archives/2890
题目描述

Polycarpus has recently got interested in sequences of pseudorandom numbers. He learned that many programming languages generate such sequences in a similar way: (for

i>=1
i>=1 ). Here

a
a ,

b
b ,

m
m are constants, fixed for the given realization of the pseudorandom numbers generator,

r_{0}
r0​ is the so-called

randseed
randseed (this value can be set from the program using functions like RandSeed(r) or srand(n)), and denotes the operation of taking the remainder of division.

For example, if

a=2,b=6,m=12,r_{0}=11
a=2,b=6,m=12,r0​=11 , the generated sequence will be:

4,2,10,2,10,2,10,2,10,2,10,…
4,2,10,2,10,2,10,2,10,2,10,… .

Polycarpus realized that any such sequence will sooner or later form a cycle, but the cycle may occur not in the beginning, so there exist a preperiod and a period. The example above shows a preperiod equal to 1 and a period equal to 2.

Your task is to find the period of a sequence defined by the given values of

a,b,m
a,b,m and

r_{0}
r0​ . Formally, you have to find such minimum positive integer

t
t , for which exists such positive integer

k
k , that for any

i>=k
i>=k :

r_{i}=r_{i+t}
ri​=ri+t​ .

输入输出格式

输入格式:
The single line of the input contains four integers

a
a ,

b
b ,

m
m and

r_{0}
r0​ ( 1<=m<=10^{5},0<=a,b<=1000,0<=r_{0}&lt;m 1<=m<=10^{5},0<=a,b<=1000,0<=r_{0}&lt;m ), separated by single spaces.

输出格式:
Print a single integer — the period of the sequence.

输入输出样例

输入样例#1: 复制

2 6 12 11
输出样例#1: 复制

2
输入样例#2: 复制

2 3 5 1
输出样例#2: 复制

4
输入样例#3: 复制

3 6 81 9
输出样例#3: 复制

1
说明

The first sample is described above.

In the second sample the sequence is (starting from the first element):

0,3,4,1,0,3,4,1,0,…
0,3,4,1,0,3,4,1,0,…

In the third sample the sequence is (starting from the first element):

33,24,78,78,78,78,…
33,24,78,78,78,78,…

单纯形学不会..

实在困了就只能写写这样的水题..

求伪随机数循环节的长度 注意可能不是从1开始

#include<cstdio>
#include<cctype>
#define ll long long
#include<algorithm>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
int flag[100010];int a,b,m,r,i;
int main(){
//  freopen("cf172b.in","r",stdin);
    a=read();b=read();m=read();r=read();
    for (i=1;i<=1e8;++i){
        r=(a*r+b)%m;if (flag[r]) break;
        else flag[r]=i; //printf("%d ",r);
    }//printf("%d %d\n",r,flag[r]);
    printf("%d\n",i-flag[r]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值