CodeForces B Multiplication Table

B. Multiplication Table
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Sasha grew up and went to first grade. To celebrate this event her mother bought her a multiplication table M with n rows and n columns such that Mij=ai⋅aj where a1,…,an is some sequence of positive integers.

Of course, the girl decided to take it to school with her. But while she was having lunch, hooligan Grisha erased numbers on the main diagonal and threw away the array a1,…,an. Help Sasha restore the array!

Input
The first line contains a single integer n (3⩽n⩽103), the size of the table.

The next n lines contain n integers each. The j-th number of the i-th line contains the number Mij (1≤Mij≤109). The table has zeroes on the main diagonal, that is, Mii=0.

Output
In a single line print n integers, the original array a1,…,an (1≤ai≤109). It is guaranteed that an answer exists. If there are multiple answers, print any.

Examples
inputCopy
5
0 4 6 2 4
4 0 6 2 4
6 6 0 3 6
2 2 3 0 2
4 4 6 2 0
outputCopy
2 2 3 1 2
inputCopy
3
0 99990000 99970002
99990000 0 99980000
99970002 99980000 0
outputCopy
9999 10000 9998
给你一个n*n的矩阵,m[i][j]=a[i]*a[j],让你求出a这个数列。

把矩阵用字符表达写出来,map[i][j]=a[i]*a[j];

就很好发现a[1]=sqrt(a[1][2]*a[1][3]/a[2][3]);

那么根据a[1]a[2],a[2][3],…,a[i][i+1],a[n-1][n]就可以求出a[2],a[3],…,a[I+1],a[n].

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#define INF 0x3f3f3f3f
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a)) 
using namespace std;
LL map[1010][1010],a[1010];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			cin>>map[i][j];
	a[1]=sqrt(map[1][2]*map[1][3]/map[2][3]);
	cout<<a[1]<<' ';
	for(int i=1;i<n;i++)
	{
		a[i+1]=map[i][i+1]/a[i];
		cout<<a[i+1]<<' ';
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值