POJ 3652 Persistent Bits 简单字符串模拟

本文针对POJ3652 PersistentBits问题提供了解决方案,详细介绍了如何通过模运算和位操作来判断随机数序列中哪些位是持久不变的,并给出了AC代码。

Persistent Bits
Time Limit:1000MS Memory Limit:65536K



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 integersA,B,C, andS, where 1 ≤A< 32768, 0 ≤B< 65536, 2 ≤C< 65536, and 0 ≤S<C.Sis the first element (theseed) of the sequence, and each later element is generated from the previous element. IfXis 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 resultA*X+ B may be larger, so calculations should be done with a 32-bitintrather than a 16-bitshortto 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 ispersistent. 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 isA= 2,B= 5,C= 18, andS= 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:

Decimal16-Bit Binary
30000000000000011
110000000000001011
90000000000001001
50000000000000101
150000000000001111
170000000000010001
overall00000000000????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 byA= 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: withA= 2,B= 0,C= 16, andS= 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???
我的WA代码
/* Author : yan
 * Question : POJ 3652 Persistent Bits
 * Data : Tuesday, December 21 2010
*/
#include<stdio.h>
#define bool _Bool
#define true 1
#define false 0

int bit[16];
int bit_cache[16];
int bit_cnt[16];

int visited[65536];
int seed;
int s;

void Deci_to_Bin(int a)
{
	memset(bit_cache,0,sizeof(bit_cache));
	int _i;
	int cnt=15;
	while(a)
	{
		bit_cache[cnt--]=a%2;
		a/=2;
	}
	//for(_i=0;_i<16;_i++) printf("%d",bit_cache[_i]);
	//printf("/n");
}
bool have_visited(int a)
{
	int _i;
	if(a==s) return true;
	for(_i=0;_i<seed;_i++)
		if(a==visited[_i])
			return true;
	return false;
}
int main()
{
	freopen("input","r",stdin);
	int a,b,c;
	int tmp,i;
	int pos;
	while(scanf("%d",&a)&&a!=0)
	{
		seed=0;
		scanf("%d %d %d",&b,&c,&s);
		visited[seed++]=s;

		if(s==0)
		{
			Deci_to_Bin(c-1);
			for(i=0;i<16;i++)
				if(bit_cache[i]==1)
				{
					pos=i;
					break;
				}
			for(i=0;i<pos;i++) printf("%d",bit_cache[i]);
			for(i=pos;i<16;i++) printf("?");
			printf("/n");
			continue;
		}
		Deci_to_Bin(s);
		for(i=0;i<16;i++) bit[i]=bit_cache[i];

		tmp=s;

		memset(bit_cnt,0,sizeof(bit_cnt));

		while(1)
		{
			tmp=(a*tmp+b)%c;

			if(have_visited(tmp)) break;
			Deci_to_Bin(tmp);
			for(i=0;i<16;i++)
				if(bit[i]!=bit_cache[i]) bit_cnt[i]++;
			visited[seed++]=tmp;
		}
		for(i=0;i<16;i++)
		{
			if(bit_cnt[i]!=0) printf("?");
			else printf("%d",bit[i]);
		}
		printf("/n");
	}
	return 0;
}
别人的代码,为了方便阅读,暂时贴过来看看
#include<iostream>
using namespace std;

bool flag[100001];
int wk[2][16];

int main()
{
int A=0, S=0 ,S1=0, B=0, C=0,k=16,j=0,yu=0,time=0;
while( scanf("%d",&A) && A != 0 )
{       
scanf("%d%d%d",&B,&C,&S);
S1=0,k=16,j=0,yu=0,time=0;
memset(flag,false,sizeof(flag));
memset(wk,0,sizeof(wk));
if(S==0&&B==0)
{
printf("0000000000000000/n");
continue;
}   
while(flag[S]!=true)
{
k=15;
time++;
yu=S;
while(yu!=1&&yu!=0&&time==1)
{
wk[0][k--]=yu%2;
yu/=2;
wk[0][k]=1;       
}
S1=(A*S+B)%C;
k=15;
yu=S1;
if(S1==0)
{
if( B == 0 )
{
break;
}
for(j=0;j<16;j++)
wk[1][j]=0;

}
if(yu==1)
{
for(j=0;j<16;j++)
wk[1][j]=0;
wk[1][15]=1;
}

while( yu!=1 && S1 != 0 )
{       
wk[1][k--]=yu%2;
yu/=2;
wk[1][k]=1;
}
flag[S]=true;    
for(j=15;j>=0;j--)
{                 
if(wk[0][j]!=wk[1][j])
{                             
wk[1][j]=100;
}       
}

for(j=0;j<16;j++)
wk[0][j]=wk[1][j];
S=S1;
if(time==63999)
break;
} 
for(j=0;j<16;j++) 
{
if(wk[1][j]==100)
{
cout<<"?";
continue;
}
cout<<wk[1][j];
}
cout<<endl; 
}
return 0;
}
http://hi.baidu.com/wklove123/blog/item/4123bfcc1883205a0fb34594.html
唉,原来没看清题意,又仔细读了读题目  最后AC!!!

/* Author : yan * Question : POJ 3652 Persistent Bits * Data : Tuesday, December 21 2010 */ #include<stdio.h> #define bool _Bool #define true 1 #define false 0 int bit[16]; int bit_cache[16]; int bit_cnt[16]; bool visited[65536]; int s; void Deci_to_Bin(int a) { memset(bit_cache,0,sizeof(bit_cache)); int _i; int cnt=15; while(a) { bit_cache[cnt--]=a%2; a/=2; } } int main() { //freopen("input","r",stdin); //freopen("output","w",stdout); int a,b,c; int tmp,i; int pos; int cnt; while(scanf("%d",&a)&&a!=0) { scanf("%d %d %d",&b,&c,&s); memset(bit_cnt,0,sizeof(bit_cnt)); memset(visited,false,sizeof(visited)); visited[s]=true; if(a==0 && b==0) { printf("0000000000000000/n"); continue; } Deci_to_Bin(s); for(i=0;i<16;i++) bit[i]=bit_cache[i]; tmp=s; cnt=65535; while(cnt--) { tmp=(a*tmp+b)%c; if(visited[tmp]) break; Deci_to_Bin(tmp); for(i=0;i<16;i++) if(bit[i]!=bit_cache[i]) bit_cnt[i]++; visited[tmp]=true; } for(i=0;i<16;i++) { //printf("/nbit_cnt[%d] = %d/n",i,bit_cnt[i]); if(bit_cnt[i]!=0) printf("?"); else printf("%d",bit[i]); } printf("/n"); } //Deci_to_Bin(10); return 0; }

内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值