进制转换hdu49512014 Multi-University Training Contest 8

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

Multiplication table

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 362    Accepted Submission(s): 179


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

比赛快结束的时候才去做这个题,结果最后统计的方式还不太对,结果也没有A掉

思路:通过观察p-1是不会出现在十位上的,然后(p-1)*(p-x)=p*(p-x-1)+x,所以(p-1)*(p-x)转化成p进制后十位是(p-x-1)所以这样尽可以地推出前面的。所以首先找出p-1,然后再进行递推。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=600;
int a[maxn][maxn],vis[maxn],ans[maxn];
int p;
void solve()
{
    int pre=0;
    for(int i=0;i<p;i++)
        if(!vis[i]){pre=i;break;}
    ans[p-1]=pre;
    for(int i=p-2;i>=0;i--)
    {
        ans[i]=a[ans[p-1]][pre];
        pre=ans[i];
    }
    for(int i=0;i<p;i++)printf(" %d",ans[i]);
    printf("\n");
}
int main()
{
    int cas=1;
    while(scanf("%d",&p)!=EOF&&p)
    {
        memset(vis,0,sizeof(vis));
        for(int i=0;i<p;i++)
            for(int j=0;j<p;j++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                a[i][j]=x;
                vis[x]=1;
            }
        printf("Case #%d:",cas++);
        solve();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值