POJ 3652 & ZOJ 2934 & HDU 2721 Persistent Bits (mathematical elements)

Source: Internet
Author: User

Topic Links:

pku:http://poj.org/problem?id=3652

Zju:http://acm.zju.edu.cn/onlinejudge/showproblem.do?

problemid=1933

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=2721


Description

Whatnext software creates sequence generators that they hope would produce fairly random sequences of 16-bit unsigned integ ERs in the range 0–65535. In general a sequence are specified by integers a, B, C, and S, where 1≤ a &l T 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 was generated from the Previo US element. If X is a 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 is a 16-bit unsigned integer less than 65536, the intermediate result a * X + B May is larger, so calculations should is done with a 32-bit int rather than a 16-bit Shor T to ensure accurate results.

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

For example, a particularly-choice is a = 2, B = 5, C =, and S = 3. It produces the sequence 3, (2*3+5)%18 = one, (2*11+5)%18 = 9, (2*9+5)%18 = 5, (2*5+5)%18 = all, (2*15+5)%18 = +, then (2*1 7+5)%18 = 3 again, and we ' re back at the beginning. 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 is always 0, always 1, or take on both values in the sequence. Note that the "the" bits is persistent. (good random sequences would have no persistent bits, and the converse is not necessarily true. For example, the sequence defined is 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 does need to return to the Seed:with a = 2, B = 0, C = +, and s< /c6> = 2, the sequence goes 2, 4, 8, 0, 0, 0, ....

Input

There is from one to sixteen datasets followed by a line containing only 0. Each dataset was 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 characters, either ' 1 ', ' 0 ', or '? Its in order, with the most significant bit first, with ' 1 ' indicating the corresponding bit are 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 31 1 64000 02 0 16 2256 85 32768 218451 4097 32776 2480

Sample Output

00000000000???

?

1?

??

?????

??

???

?

?? 000000000000??? 001010101010101010??

? 000011111??

?

Source

field=source&key=mid-central+usa+2007 "style=" Text-decoration:none ">mid-central USA 2007

Test instructions

Give A, B, C, S.

S is the initial value, and each change has S = (a*s+b)%c.

So until repeated.

These numbers are represented as binary, assuming that a bit is 0 in all numbers and outputs 0. 1 is the output 1. It is assumed that it is possible to output a question mark.


The code is as follows:

#include <cstdio> #include <cstring>int vis[1000000];int main () {int str[47];    int A, B, C, S;        while (scanf ("%d", &a)) {if (a = = 0) break;        scanf ("%d%d%d", &b,&c,&s);        memset (vis,0,sizeof (VIS));        int tt = s;        int l = 0;            while (TT) {str[l++] = tt%2;        tt/=2;        } for (int i = l; l <; l++) {str[i++] = 0;        }/*for (int i = 0; i <; i++) {printf ("%d", str[i]);        }*/TT = s;            while (!vis[tt]) {L = 0;            VIS[TT] = 1;            int TM = TT;                While (tm) {if (str[l]! = tm%2) {Str[l] = 3;                } tm/=2;            l++;        } TT = (((A*TT)%c) + (b%c))%c; } for (int i = +; I >= 0; i--) {if (str[i]!=3) printf ("%D ", Str[i]);        else printf ("?");    } printf ("\ n"); } return 0;}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

POJ 3652 &amp; ZOJ 2934 &amp; HDU 2721 Persistent Bits (mathematical elements)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.