POJ3652

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=3652

思路:循环的判断:可能出现不和S循环的情况,所以要保存一下前面出现的所有数,不要增加循环,否则可能TLE。。。

            结果的判断:不保存中间变量,只改变结果的数据,否则可能MLE。。。

 

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class Main {
  4.     int a;
  5.     int b;
  6.     int c;
  7.     int s;
  8.     int x;
  9.     char[] result;
  10.     int flg;
  11.     boolean[] dup;
  12.     public Main() {
  13.         Scanner scan = new Scanner(System.in);
  14.         while ((a = scan.nextInt()) != 0) {
  15.             b = scan.nextInt();
  16.             c = scan.nextInt();
  17.             s = scan.nextInt();
  18.             result = new char[16];
  19.             Arrays.fill(result, '0');
  20.             initFlg(Integer.toBinaryString(s));
  21.             dup = new boolean[c];
  22.             dup[s] = true;
  23.             x = (a * s + b) % c;
  24.             while (!dup[x]) {
  25.                 dup[x] = true;
  26.                 if (setFlg(Integer.toBinaryString(x))) {
  27.                     break;
  28.                 }
  29.                 x = (a * x + b) % c;
  30.             }
  31.             System.out.println(result);
  32.         }
  33.     }
  34.     public void initFlg(String str) {
  35.         for (int i = 15, k = str.length() - 1; k >= 0; i--, k--) {
  36.             if (str.charAt(k) == '1') {
  37.                 result[i] = '1';
  38.             }
  39.         }
  40.     }
  41.     public boolean setFlg(String str) {
  42.         flg = 0;
  43.         for (int i = 15, k = str.length() - 1; k >= 0; i--, k--) {
  44.             if (result[i] == '?') {
  45.                 flg++;
  46.                 if (flg == 16) {
  47.                     return true;
  48.                 }
  49.                 continue;
  50.             } else {
  51.                 if (str.charAt(k) != result[i]) {
  52.                     result[i] = '?';
  53.                 }
  54.             }
  55.         }
  56.         return false;
  57.     }
  58.     public static void main(String[] args) {
  59.         new Main();
  60.     }
  61. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值