hdu 2721(字符串处理,位运算 暴力)
来源:互联网 发布:mac网页图片显示问号 编辑:程序博客网 时间:2024/04/30 01:54
Persistent Bits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 201 Accepted Submission(s): 116
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, ....
(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 31 1 64000 02 0 16 2256 85 32768 218451 4097 32776 2480
Sample Output
00000000000????1????????????????000000000000???001010101010101010???000011111???
Source
Mid-Central USA 2007
难点:
如何有效率的将十进制转化为16位的二进制,如何判断是否发生了数字循环出现的现象。
易错点:
数组要开的足够大,否则不能正常输出结果。
题意:
给定一个式子:(A * X + B) % C 转化成二进制要是x是可以变化的,通过x的不断变化,式子也
在不断变化。要是二进制的某一位上,保持不变则还是原始数据,否则是 ?将结果输出。
代码如下:
<span style="font-size:14px;">#include<stdio.h>#include<string.h>int f[101000];//数组要开足够大int main(){int a,b,c,s,i,bit;char ch[20];while(~scanf("%d",&a),a){memset(f,0,sizeof(f));scanf("%d%d%d",&b,&c,&s);for(i=0;i<16;i++)ch[i]=((s>>(15-i))&1)+'0';//将十进制高效的转化为16位二进制while(!f[s]){f[s]=1;s=((a%c)*(s%c)+(b%c))%c;for(i=0;i<16;i++){bit=(s>>(15-i))&1;if(bit+'0'!=ch[i])ch[i]='?';}}for(i=0;i<16;i++){printf("%c",ch[i]);}printf("\n");}return 0;}</span>
0 0
- hdu 2721(字符串处理,位运算 暴力)
- 位运算处理字符串大小写转换
- 位运算处理字符串大小写转换
- 【字符串暴力】hdu 2847
- Gym - 100203A Ariel 暴力+位运算
- UVALive 7279 Sheldon Numbers 【位运算】【暴力】
- 【位运算】hdu 3711
- hdu 3006 位运算
- hdu 2014 位运算
- hdu 5491(位运算)
- hdu 1238 Substrings 字符串暴力
- hdu 4886 字符串 hash+暴力
- zoj 3432 字符串 位运算
- 字符串包含 位运算法
- hdu 5014(dfs + 位运算)
- hdu 5014(贪+位运算)
- HDU 5747 Aaronson [位运算]
- HDU 3257 Hello World!(字符串找规律——位运算来模拟过程)
- svn提交、更新常见错误
- 揭秘腾讯大数据之平台综述篇
- iOS 删除文件夹下所有文件的方式
- JS的方括号测试
- 详解Hadoop核心架构
- hdu 2721(字符串处理,位运算 暴力)
- HDU 4946 凸包 判重点 多校练习赛8 1002
- Android之ListView原理学习与优化总结
- 递归及递推问题系列之 报数问题
- OC中字符串的简单操作
- 杭电4081 Qin Shi Huang's National Road System(次小生成树)
- poj 3277 City Horizon 线段树加上离散化
- 关于android:launchMode 的有趣现象
- 使用AsyncHttpClient 实现get和post请求