题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=3652
思路:循环的判断:可能出现不和S循环的情况,所以要保存一下前面出现的所有数,不要增加循环,否则可能TLE。。。
结果的判断:不保存中间变量,只改变结果的数据,否则可能MLE。。。
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- int a;
- int b;
- int c;
- int s;
- int x;
- char[] result;
- int flg;
- boolean[] dup;
- public Main() {
- Scanner scan = new Scanner(System.in);
- while ((a = scan.nextInt()) != 0) {
- b = scan.nextInt();
- c = scan.nextInt();
- s = scan.nextInt();
- result = new char[16];
- Arrays.fill(result, '0');
- initFlg(Integer.toBinaryString(s));
- dup = new boolean[c];
- dup[s] = true;
- x = (a * s + b) % c;
- while (!dup[x]) {
- dup[x] = true;
- if (setFlg(Integer.toBinaryString(x))) {
- break;
- }
- x = (a * x + b) % c;
- }
- System.out.println(result);
- }
- }
- public void initFlg(String str) {
- for (int i = 15, k = str.length() - 1; k >= 0; i--, k--) {
- if (str.charAt(k) == '1') {
- result[i] = '1';
- }
- }
- }
- public boolean setFlg(String str) {
- flg = 0;
- for (int i = 15, k = str.length() - 1; k >= 0; i--, k--) {
- if (result[i] == '?') {
- flg++;
- if (flg == 16) {
- return true;
- }
- continue;
- } else {
- if (str.charAt(k) != result[i]) {
- result[i] = '?';
- }
- }
- }
- return false;
- }
- public static void main(String[] args) {
- new Main();
- }
- }