Codeforces Round #586 (Div. 1 + Div. 2) B. Multiplication Table

链接:

https://codeforces.com/contest/1220/problem/B

题意:

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!

思路:

a1 = sqrt(a1a2a1a3/(a2a3))
然后挨个计算.

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int MAXN = 1e3+10;
LL MT[MAXN][MAXN];
LL a[MAXN];
int n;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= n;j++)
            cin >> MT[i][j];
    }
    a[1] = sqrt((MT[1][2]*MT[1][3])/MT[2][3]);
    for (int i = 2;i <= n;i++)
        a[i] = MT[i-1][i]/a[i-1];
    for (int i = 1;i <= n;i++)
        cout << a[i] << ' ' ;
    cout << endl;

    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/11626598.html