Hdu4951 Multiplication table

Problem Description
Teacher Mai has a multiplication table in base p.

For example, the following is a multiplication table in base 4:

* 0 1 2 3
0 00 00 00 00
1 00 01 02 03
2 00 02 10 12
3 00 03 12 21


But a naughty kid maps numbers 0..p-1 into another permutation and shuffle the multiplication table.

For example Teacher Mai only can see:

1*1=11 1*3=11 1*2=11 1*0=11
3*1=11 3*3=13 3*2=12 3*0=10
2*1=11 2*3=12 2*2=31 2*0=32
0*1=11 0*3=10 0*2=32 0*0=23


Teacher Mai wants you to recover the multiplication table. Output the permutation number 0..p-1 mapped into.

It's guaranteed the solution is unique.

题目比较难理解,首先有一个p进制的乘法表,将其中每个数换成对应的另一个对应的数,这样得到新的乘法表,现在给定新的乘法表,求出每个数对应的数;

首先0对应的一行一列肯定都是0,所以如果有一行和一列的数都是相同的 那么那个数就是0所对应的数,再看新乘法表的第一位,如果只有一种数字,那么就是1对应的数,如果有2种数字,就是2对应的数,依次类推,就可以求出对应的数了。
#include<cstdio> 
#include<cstring>
int map[800][1200];
int ans[600];
bool f[600];
int main()
{

	int p,t=1;
	while(1)
	{
		
		scanf("%d",&p);
		if(p==0) break ;
		for(int i=0;i<p;i++)
		{
			for(int j=0;j<p;j++)
			{
				scanf("%d%d",&map[i][2*j],&map[i][2*j+1]);
				
			}
		}
		int cnt=0;
		for(int i=0;i<p;i++)
		{
			for(int j=1;j<2*p;j++)
			{
				if(map[i][j]!=map[i][j-1]) break;
				if(j==2*p-1) cnt=i;
			}
		}
		ans[0]=map[cnt][0];
		
		for(int i=0;i<p;i++)
		{
			int cont=0;
			memset(f,0,sizeof(f));
			if(i==cnt) continue;
			else
			{
				for(int j=0;j<2*p;j=j+2)
				{
					if(f[map[i][j]]==0)
					{
						f[map[i][j]]=1;
						cont++;
					}
				}
				ans[cont]=i;
			}
		}
		printf("Case #%d:",t++);
		for(int i=0;i<p;i++)
		{
			printf(" %d",ans[i]);
		}
		printf("\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值