Persistent Bits

Persistent Bits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 142    Accepted Submission(s): 85


Problem Description
WhatNext Software creates sequence generators that they hope will produce fairly random sequences of 16-bit unsigned integers in the range 0–65535. In general a sequence is specified by integers A, B, C, and S, where 1 ≤ A < 32768, 0 ≤ B < 65536, 2 ≤ C < 65536, and 0 ≤ S < C. S is the first element (the seed) of the sequence, and each later element is generated from the previous element. If X is an element of the sequence, then the next element is

(A * X + B) % C

where '%' is the remainder or modulus operation. Although every element of the sequence will be a 16-bit unsigned integer less than 65536, the intermediate result A * X + B may be larger, so calculations should be done with a 32-bit int rather than a 16-bit short to ensure accurate results.

Some values of the parameters produce better sequences than others. The most embarrassing sequences to WhatNext Software are ones that never change one or more bits. A bit that never changes throughout the sequence is persistent. Ideally, a sequence will have no persistent bits. Your job is to test a sequence and determine which bits are persistent.

For example, a particularly bad choice is A = 2, B = 5, C = 18, and S = 3. It produces the sequence 3, (2*3+5)%18 = 11, (2*11+5)%18 = 9, (2*9+5)%18 = 5, (2*5+5)%18 = 15, (2*15+5)%18 = 17, then (2*17+5)%18 = 3 again, and we're back at the beginning. So the sequence repeats the the same six values over and over:
Decimal  16-Bit Binary
3  0000000000000011
11  0000000000001011
9  0000000000001001
5  0000000000000101
15  0000000000001111
17  0000000000010001
overall  00000000000????1

The last line of the table indicates which bit positions are always 0, always 1, or take on both values in the sequence. Note that 12 of the 16 bits are persistent. (Good random sequences will have no persistent bits, but the converse is not necessarily true. For example, the sequence defined by A = 1, B = 1, C = 64000, and S = 0 has no persistent bits, but it's also not random: it just counts from 0 to 63999 before repeating.) Note that a sequence does not need to return to the seed: with A = 2, B = 0, C = 16, and S = 2, the sequence goes 2, 4, 8, 0, 0, 0, ....
 

Input
There are from one to sixteen datasets followed by a line containing only 0. Each dataset is a line containing decimal integer values for A, B, C, and S, separated by single blanks.
 

Output
There is one line of output for each data set, each containing 16 characters, either '1', '0', or '?' for each of the 16 bits in order, with the most significant bit first, with '1' indicating the corresponding bit is always 1, '0' meaning the corresponding bit is always 0, and '?' indicating the bit takes on values of both 0 and 1 in the sequence.
 

Sample Input
  
  
2 5 18 3 1 1 64000 0 2 0 16 2 256 85 32768 21845 1 4097 32776 248 0
 

Sample Output
  
  
00000000000????1 ???????????????? 000000000000???0 0101010101010101 0???000011111???
/*
思路:很笨的想法,计算出所有可能的值,保存(这里我使用map来判断是否开始循环),然后将计算的值转化成
2进制,拿开始循环的那个数的二进制和其他的数的二进制进行比较,找到不同的就将那位变成“?”,
这里我有一个n>65535优化,如果n>65535即超出16位数,那么就说明不能用16位保存,输出16个“?”。

  我本来是用字符数组来保存二进制数的,指针没学好,嘿嘿,搞的一摊糊涂,最后用了p[]和q[]两个数组,
  唉!!!
*/
#include<stdio.h>
#include<string>
#include<stdlib.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
char *change(__int64 n){	//将数字转变成16位2进制
	char str[100];
	int k=0;
	//sprintf(str,"%I64d",n);
	while(n){
		str[k++]='0'+(n%2);
		n/=2;
	}
	while(k<16)	//未满16位,填0
		str[k++]='0';
	str[k]='\0';
	char str1[100];
	int l=0;
	for(int i=strlen(str)-1;i>=0;i--)	//将字符串逆转,本来是用reverse()库函数的,~~~~(>_<)~~~~ ,可是不会用。
		str1[l++]=str[i];
	str1[l]='\0';
	return str1;
}
int main(void)
{
	__int64 a,b,c,s;

	while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&s)&&a!=0){
		map<int,bool>m;
		__int64 d[65540],k=0;
		d[k++]=s;
		m[d[k-1]]=true;
		__int64 n=s;
		while(true){
			n=(a*n+b)%c;
			if(n>65536)
				break;
			if(m[n]==true){
				char q[1002];
				memcpy(q,change(n),17);
				for(int i=0;i<k;i++){
					char pp[1002];
					memcpy(pp,change(d[i]),17);
					int j=0;
					while(*(pp+j)!='\0'){
						if(*(q+j)!=*(pp+j)){
							*(q+j)='?';
						}
						j++;
					}
				}
				cout<<q<<endl;
				break;
			}
			else{
				m[n]=true;
				d[k++]=n;
			}
		}
		if(n>65536)
		{
			for(int i=0;i<16;i++)
				cout<<"?";
			cout<<endl;
		}
	}
	return 0;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值