HDU 4951 Multiplication table

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.

Input
There are multiple test cases, terminated by a line "0".

For each test case, the first line contains one integer p(2<=p<=500).

In following p lines, each line contains 2*p integers. The (2*j+1)-th number x and (2*j+2)-th number y in the i-th line indicates equation i*j=xy in the shuffled multiplication table.

Warning: Large IO!

Output
For each case, output one line.

First output "Case #k:", where k is the case number counting from 1. The following are p integers, indicating the permutation number 0..p-1 mapped into.

题解:很绕的一道题目,题目大意是给一个P进制的乘法表,然后给出一个0到p-1的置换,把乘法表里面的数字都换掉,然后按着新数字的顺序调整乘法表的顺序,然后给你新的表,让你输出置换……好吧,不太好表达,按着样例弄一遍,应该就能理解
比赛的时候完全没有看这道题目,看懂了题目发现真是伤,不就是找规律么,唉…
规律:1. 0所在的行和列都是一个数字
2.十位出现一次的数字是n-2
3.十位上不会出现的数字是n-1
这样就可以找到n-1,n-2,然后一路递推下去
PS.输入太大,输入外挂还是要加滴
渣代码奉上
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;

struct node 
{
	int x;
	int y;
};
node matrix[550][550];
int n,change[550],hash[550];

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//????
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//??????
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}

int main()
{
	int a,b;
	int kasc=0;
	while(~scanf("%d",&n))
	{
		if (n==0) break;
		kasc++;
		memset(hash,0,sizeof(hash));
		for (a=0;a<n;a++)
		{
			bool flag=true;
			int yuan;
			for (b=0;b<n;b++)
			{
				//scanf("%d%d",&matrix[a][b].x,&matrix[a][b].y);
				matrix[a][b].x=Scan();
				matrix[a][b].y=Scan();
				if (b==0) yuan=matrix[a][b].x;
				if (matrix[a][b].x!=yuan) flag=false;
				if (matrix[a][b].y!=yuan) flag=false;
				hash[matrix[a][b].x]++;
				//if (!flag) break;
			}
			if (flag) 
			{
				change[0]=a;
			}
		}
		
		for (a=0;a<n;a++)
		{
			if (hash[a]==0) change[n-1]=a;
			if (hash[a]==1) change[n-2]=a;
		}
		
		for (a=2;a<n-1;a++)
		{
			change[n-a-1]=matrix[change[n-1]][change[n-a]].x;
			//cout<<n-a-1<<endl;
		}
		
		printf("Case #%d:",kasc);
		for (a=0;a<n;a++)
			printf(" %d",change[a]);
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值