码迷,mamicode.com
首页 > 其他好文 > 详细

HDU4951:Multiplication table

时间:2014-08-15 19:44:19      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:hdu

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.
 

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.
 

Sample Input
4 2 3 1 1 3 2 1 0 1 1 1 1 1 1 1 1 3 2 1 1 3 1 1 2 1 0 1 1 1 2 1 3 0
 

Sample Output
Case #1: 1 3 2 0
可以发现,0对应的情况下,这行的所有数字都相同
其他数字则看第一位,假设有n种不同的情况,对应的数字就是n
1的情况要对应0做特殊处理
用G++超时,用C++才过,因为一个细节没有注意,WA了几次,纠错花了很久。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define up(i,x,y,z) for(i=x;i<=y;i+=z)
#define mem(a,b) memset(a,b,sizeof(a))
#define w(x) while(x)
int n,a[505][1005],i,j,vis[505],ans[505],cas=1;
int main()
{
    w((scanf("%d",&n),n))
    {
        up(i,0,n-1,1)
        up(j,0,2*n-1,1)
        scanf("%d",&a[i][j]);
        up(i,0,n-1,1)
        {
            up(j,1,2*n-1,1)
            {
                if(a[i][j]!=a[i][j-1])
                    break;
            }
            if(j==2*n)
            {
                ans[0]=i;
                break;
            }
        }
        up(i,0,n-1,1)
        {
            int cnt=0;
            mem(vis,0);
            up(j,0,2*n-1,2)
            {
                if(!vis[a[i][j]])
                {
                    vis[a[i][j]]=1;
                    cnt++;
                }
            }
            if(cnt==1&&ans[0]!=i) ans[1]=i;
            else if(cnt!=1)ans[cnt]=i;
        }
        printf("Case #%d:",cas++);
        up(i,0,n-1,1)
        printf(" %d",ans[i]);
        printf("\n");
    }

    return 0;
}


HDU4951:Multiplication table,布布扣,bubuko.com

HDU4951:Multiplication table

标签:hdu

原文地址:http://blog.csdn.net/libin56842/article/details/38588677

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!