Hdu4951 Multiplication table

来源:互联网 发布:短信群发网站源码 编辑:程序博客网 时间:2024/06/12 03:20
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;}

0 0