| ||||||||||
Multiplication tableTime 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 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: 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
Sample Output
|
比赛快结束的时候才去做这个题,结果最后统计的方式还不太对,结果也没有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;
}