update
This commit is contained in:
parent
9e8abf771f
commit
60a6fcaa0a
123
src/2/P2455.cpp
Normal file
123
src/2/P2455.cpp
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <ios>
|
||||||
|
#include <iostream>
|
||||||
|
#include <random>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
using ll = int64_t;
|
||||||
|
|
||||||
|
constexpr ll maxn=50;
|
||||||
|
ll n;
|
||||||
|
double m[maxn+5][maxn+5];
|
||||||
|
|
||||||
|
void sp(double *a,double* b){
|
||||||
|
for(ll i=1;i<=n+1;i++){
|
||||||
|
std::swap(a[i],b[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pm(){
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
for(ll j=1;j<=n+1;j++){
|
||||||
|
std::cout<<m[i][j]<<' ';
|
||||||
|
}
|
||||||
|
std::cout<<'\n';
|
||||||
|
}
|
||||||
|
std::cout<<'\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isz(double a){
|
||||||
|
if(a > 1e-8 || a<-1e-8){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// if(a!=0){
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
ll rank = 0;
|
||||||
|
std::cin>>n;
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
for(ll j=1;j<=n+1;j++){
|
||||||
|
std::cin>>m[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// std::random_device rd{};
|
||||||
|
// std::uniform_int_distribution<ll> uid(1,n);
|
||||||
|
// ll asp[maxn];
|
||||||
|
// for(ll i=1;i<=n;i++){
|
||||||
|
// asp[i]=uid(rd);
|
||||||
|
// }
|
||||||
|
// for(ll i=1;i<=n/2;i++){
|
||||||
|
// sp(m[i],m[asp[i]]);
|
||||||
|
// }
|
||||||
|
// pm();
|
||||||
|
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
static ll nowused{1};
|
||||||
|
ll maxnj=nowused;
|
||||||
|
for(ll j=nowused+1;j<=n;j++){
|
||||||
|
if(std::abs(m[j][i])>std::abs(m[maxnj][i])){
|
||||||
|
maxnj=j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(isz(m[maxnj][i]))continue;
|
||||||
|
else{
|
||||||
|
nowused++;
|
||||||
|
}
|
||||||
|
if(maxnj!=i){
|
||||||
|
sp(m[i],m[maxnj]);
|
||||||
|
}
|
||||||
|
double fact = m[i][i];
|
||||||
|
for(ll j=1;j<=n+1;j++){
|
||||||
|
m[i][j]/=fact;
|
||||||
|
}
|
||||||
|
for(ll k=i+1;k<=n;k++){
|
||||||
|
if(k==i)continue;
|
||||||
|
double fact = m[k][i];
|
||||||
|
for(ll j=1;j<=n+1;j++){
|
||||||
|
m[k][j]-=m[i][j]*fact;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// pm();
|
||||||
|
bool isfy=false,isl=false;
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
bool isAZ=true;
|
||||||
|
for(ll j=1;j<=n;j++){
|
||||||
|
if(!isz(m[i][j])){
|
||||||
|
isAZ=false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
if(std::isnan(m[i][n+1])){
|
||||||
|
isl=true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(isAZ){
|
||||||
|
if(isz(m[i][n+1])){
|
||||||
|
isl=true;
|
||||||
|
}else{
|
||||||
|
isfy=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(isfy){
|
||||||
|
std::cout<<"-1\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(isl){
|
||||||
|
std::cout<<"0\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
std::cout<<std::fixed<<std::setprecision(0)<<"x"<<i<<"="<<std::fixed<<std::setprecision(2)<<m[i][n+1]<<'\n';
|
||||||
|
}
|
||||||
|
}
|
83
src/2/P3389.cpp
Normal file
83
src/2/P3389.cpp
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <ios>
|
||||||
|
#include <iostream>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
using ll = int64_t;
|
||||||
|
template<class T>
|
||||||
|
T read(){
|
||||||
|
T t;
|
||||||
|
std::cin>>t;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
constexpr ll maxn = 100;
|
||||||
|
ll n;
|
||||||
|
double im[maxn+5][maxn+5],tmp[maxn+5];
|
||||||
|
void cp(double*f,double*t){
|
||||||
|
for(ll i=1;i<=n+1;i++){
|
||||||
|
t[i]=f[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void sp(double*f,double*t){
|
||||||
|
for(ll i=1;i<=n+1;i++){
|
||||||
|
std::swap(f[i],t[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int main(){
|
||||||
|
std::cin>>n;
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
for(ll j=1;j<=n+1;j++){
|
||||||
|
std::cin>>im[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
for(ll k=i;k<=n;k++){
|
||||||
|
if(im[k][i]!=0){
|
||||||
|
if(k!=i){
|
||||||
|
sp(im[k],im[i]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double fact = im[i][i];
|
||||||
|
for(ll j=1;j<=n+1;j++){
|
||||||
|
im[i][j]/=fact;
|
||||||
|
}
|
||||||
|
for(ll k=1;k<=n;k++){
|
||||||
|
if(k==i)continue;
|
||||||
|
double fact = im[k][i];
|
||||||
|
for(ll j=1;j<=n+1;j++){
|
||||||
|
im[k][j]-=fact*im[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if([]()->bool{
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
bool isAllZero=true;
|
||||||
|
for(ll j=1;j<=n;j++){
|
||||||
|
if(im[i][j]!=0){
|
||||||
|
isAllZero=false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(isAllZero){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
if(std::isnan(im[i][n+1])){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}()){
|
||||||
|
std::cout<<std::fixed<<std::setprecision(2);
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
std::cout<<im[i][n+1]<<'\n';
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
std::cout<<"No Solution\n";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user