Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这一题到底是想干嘛的,简单的算法没过,反而暴力过了没过的: #include <map> #include <iostream> using namespace std; int main() { int a,c,m,r,max=0,d; map<int,bool> str; str.clear(); scanf("%d %d %d %d",&a,&c,&m,&r); while(!str[r]) { str[r]=true; r=(a*r+c)%m; } map<int,bool>::iterator i=str.begin(),j=str.end(),k; j--; while(i!=j) { k=i++; d=i->first - k->first; max=max>d?max:d; } printf("%d\n",max); return 0; } 过了: #include <iostream> using namespace std; int main() { bool *flag; long R0, R, Rn, i, j, a, c, m, max, max_num; scanf("%ld%ld%ld%ld", &a, &c, &m, &R0); flag = new bool[m+1]; memset(flag, false, sizeof(bool) * (m+1)); flag[R0] = true; max = 0; max_num = 0; R = R0; for(i = 1; ; i++) { if(R > max_num) max_num = R; Rn = (a*R + c) % m; if(flag[Rn]) break; else flag[Rn] = true; R = Rn; } for(i = 0; i < max_num; i++) { if(flag[i]) { for(j = i+1; j <= max_num; j++) { if(flag[j]) break; } if(max < j-i) max = j-i; i = j-1; } } printf("%ld\n", max); return 1; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator