Problem:
Problem
ある日、moritaoyさんは爆裂魔法を覚えました。
新しい魔法を覚えたmoritaoyさんは（傍迷惑なことに）試し撃ちをすることにしました。
試射場は $N$ 次元実ベクトル全体からなる空間であり、$N$ 次元超直方体 $E$ が浮かんでいます。
超直方体 $E$ は $N$ 個の互いに直交するベクトル $\vec{V_1},\vec{V_2},\ldots ,\vec{V_N}$ によって以下のように表される領域です。
$$E=  \left \{ \sum_{i=1}^{N} x_i \vec{V_i} \mid x_i \in \mathbb{R},0 \leq x_i \leq 1 \right \} $$
moritaoyさんは $\vec{P}$ を中心として爆裂魔法を打つつもりです。
$\vec{a}$ と $\vec{b}$ の距離 ${\rm d}(\vec{a},\vec{b})$ を以下のように定義します。
$${\rm d} (\vec{a},\vec{b})= \sqrt{\sum_{i=1}^{N} {(a_i-b_i)}^2}$$
moritaoyさんは、爆裂魔法が超直方体Eに届くかどうか気になりました。
$\vec{P}$ から超直方体 $E$ までの距離 $\displaystyle \min_{\vec{e} \in E} {\rm d} (\vec{P},\vec{e})$ を求めてください。
Input
入力は以下の形式で与えられる。
$N$
$(V_1)_1$ $(V_1)_2$ $\cdots$ $(V_1)_N$
$(V_2)_1$ $(V_2)_2$ $\cdots$ $(V_2)_N$
$\vdots$
$(V_N)_1$ $(V_N)_2$ $\cdots$ $(V_N)_N$
$P_1$ $P_2$ $\cdots$ $P_N$
Constraints
入力は以下の条件を満たす。
$1 \le N \le 100$
$| (V_i)_j | \le 10^6$
$| P_i | \le 10^6$
$\vec{V_i}$ は零ベクトルでない
$\vec{V_i} \cdot \vec{V_j} =0 \ (i \neq j)$
入力は全て整数である
Output
答えを一行に出力する。
想定解との絶対誤差、または相対誤差が $10^{-4}$ 以下のとき正解と判定される。
Sample Input 1
2
1 0
0 1
2 2
Sample Output 1
1.41421
Sample Input 2
2
2 2
-2 2
1 0
Sample Output 2
0.707106781186
Sample Input 3
4
354025 223975 106481 405167
-169225 -383225 314143 277151
383225 -169225 277151 -314143
223975 -354025 -405167 106481
802128 -252014 197656 762352
Sample Output 3
348481.74254367457169381382
Sample Input 4
2
2 0
0 2
1 1
Sample Output 4
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}
const ld LIMIT=0.99*CLOCKS_PER_SEC;

int main(){
  clock_t start=clock();
  cin>>n;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  int idx=0;
  while(true){
    clock_t nowt=clock();
    const double time=static_cast<double>(nowt-start);
    if(time>LIMIT)break;
    idx+=(n-1)*(time/LIMIT)+1;if(idx>=n)idx-=n;
    ld L=0,R=1;
    change(idx,-x[idx]);
    for(int i=0;i<40;i++){
      ld diff=(R-L)/3;
      change(idx,diff);
      ld tmp1=D();
      change(idx,diff);
      ld tmp2=D();
      if(tmp1<tmp2)R-=diff;
      else L+=diff;
      change(idx,L-x[idx]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(D())<<endl;
}
accuracy: 0.6590909090909091
time_ms: 990
memory_kb: 3312
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}
const ld LIMIT=0.98*CLOCKS_PER_SEC;

signed main(){
  clock_t start = clock();
  cin>>n;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  int idx=0;
  while(true){
    clock_t nowt = clock();
    const double time = static_cast<double>(nowt - start);
    if(time>LIMIT)break;
    idx+=(n-1)*(time/LIMIT)+1;if(idx>=n)idx-=n;
    ld L=0,R=1;
    change(idx,-x[idx]);
    for(int i=0;i<40;i++){
      ld diff=(R-L)/3;
      change(idx,diff);
      ld tmp1=D();
      change(idx,diff);
      ld tmp2=D();
      if(tmp1<tmp2)R-=diff;
      else L+=diff;
      change(idx,L-x[idx]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(D())<<endl;
}
accuracy: 0.6590909090909091
time_ms: 980
memory_kb: 3296
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[30];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}
const ld LIMIT=0.98*CLOCKS_PER_SEC;

signed main(){
  clock_t start = clock();
  cin>>n;
  for(int i=1;i<30;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  int idx=0;
  while(true){
    clock_t nowt = clock();
    const double time = static_cast<double>(nowt - start);
    if(time>LIMIT)break;
    idx+=n*(time/LIMIT);if(idx>=n)idx-=n;
    ld rem=1-x[idx];
    for(int j=1;j<30;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    if(rem!=1-x[idx])continue;
    rem=x[idx];
    for(int j=1;j<30;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6818181818181818
time_ms: 980
memory_kb: 3436
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[30];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}
const ld LIMIT=0.98*CLOCKS_PER_SEC;

signed main(){
  clock_t start = clock();
  cin>>n;
  for(int i=0;i<30;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  int idx=0;
  while(true){
    clock_t nowt = clock();
    const double time = static_cast<double>(nowt - start);
    if(time>LIMIT)break;
    idx+=n*(time/LIMIT);if(idx>=n)idx-=n;
    ld rem=1-x[idx];
    for(int j=1;j<30;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    rem=x[idx];
    for(int j=2;j<30;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6818181818181818
time_ms: 980
memory_kb: 3468
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[30];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}
const ld LIMIT=0.98*CLOCKS_PER_SEC;

signed main(){
  clock_t start = clock();
  cin>>n;
  for(int i=0;i<30;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  int idx=0;
  while(true){
    clock_t nowt = clock();
    const double time = static_cast<double>(nowt - start);
    if(time>LIMIT)break;
    idx+=n*(time/LIMIT);if(idx>=n)idx-=n;
    ld rem=1-x[idx];
    for(int j=2;j<30;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    rem=x[idx];
    for(int j=2;j<30;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6818181818181818
time_ms: 980
memory_kb: 3492
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[10];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}
const ld LIMIT=0.98*CLOCKS_PER_SEC;

signed main(){
  clock_t start = clock();
  cin>>n;
  for(int i=0;i<10;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  int idx=0;
  while(true){
    clock_t nowt = clock();
    const double time = static_cast<double>(nowt - start);
    if(time>LIMIT)break;
    idx+=n*(time/LIMIT);if(idx>=n)idx-=n;
    ld rem=1-x[idx];
    for(int j=2;j<10;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    rem=x[idx];
    for(int j=2;j<10;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6818181818181818
time_ms: 980
memory_kb: 3436
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[10];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}
const ld LIMIT=0.99*CLOCKS_PER_SEC;

signed main(){
  clock_t start = clock();
  cin>>n;
  for(int i=0;i<10;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  while(true){
    clock_t nowt = clock();
    const double time = static_cast<double>(nowt - start);
    if(time>LIMIT)break;
    int idx=rand()%n;
    ld rem=1-x[idx];
    for(int j=2;j<10;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    rem=x[idx];
    for(int j=2;j<10;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6818181818181818
time_ms: 990
memory_kb: 3488
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[10];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}

signed main(){
  clock_t start = clock();
  cin>>n;
  for(int i=0;i<10;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  while(true){
    clock_t nowt = clock();
    const double time = static_cast<double>(nowt - start) / CLOCKS_PER_SEC;
    if(time>0.97)break;
    int idx=rand()%n;
    ld rem=1-x[idx];
    for(int j=2;j<10;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    rem=x[idx];
    for(int j=2;j<10;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6818181818181818
time_ms: 970
memory_kb: 3492
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[10];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}

signed main(){
  cin>>n;
  for(int i=0;i<10;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  for(int _=0;_<1000000;_++){
    int idx=rand()%n;
    ld rem=1-x[idx];
    for(int j=2;j<5;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    rem=x[idx];
    for(int j=2;j<5;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.045454545454545456
time_ms: 130
memory_kb: 3460
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[10];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}

signed main(){
  cin>>n;
  for(int i=0;i<10;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  for(int _=0,idx=0;_<1000000;_++,idx++){
    if(idx==n)idx=0;
    ld rem=1-x[idx];
    for(int j=2;j<5;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    rem=x[idx];
    for(int j=2;j<10;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.11363636363636363
time_ms: 460
memory_kb: 3380
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100],tw[10];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}

signed main(){
  cin>>n;
  for(int i=0;i<10;i++)tw[i]=pow(0.5,i);
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  for(int _=0,idx=0;_<100000;_++,idx++){
    if(idx==n)idx=0;
    ld rem=1-x[idx];
    for(int j=2;j<5;j++){
      change(idx,rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,-rem*tw[j]);
    }
    rem=x[idx];
    for(int j=2;j<10;j++){
      change(idx,-rem*tw[j]);
      ld tmp=D();
      if(ans>tmp)ans=tmp;
      else change(idx,rem*tw[j]);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.11363636363636363
time_ms: 40
memory_kb: 3460
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100],now[100];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++)res+=(now[i]-p[i])*(now[i]-p[i]);
  return res;
}
void change(int idx,ld diff){
  x[idx]+=diff;
  for(int i=0;i<n;i++)now[i]+=v[idx][i]*diff;
}

signed main(){
  cin>>n;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)now[i]+=(ld)v[j][i]*x[j];
  ld ans=D();
  for(int _=0;_<100000;_++){
    int idx=rand()%n;
    ld rem=1-x[idx];
    for(int j=2;j<10;j++){
      change(idx,rem*pow(0.5,j));
      if(ans>D())ans=D();
      else change(idx,-rem*pow(0.5,j));
    }
    rem=x[idx];
    for(int j=2;j<10;j++){
      change(idx,-rem*pow(0.5,j));
      if(ans>D())ans=D();
      else change(idx,rem*pow(0.5,j));
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6818181818181818
time_ms: 980
memory_kb: 3460
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++){
    ld tmp=0;
    for(int j=0;j<n;j++)tmp+=(ld)v[j][i]*x[j];
    res+=((ld)p[i]-tmp)*((ld)p[i]-tmp);
  }
  return res;
}

signed main(){
  cin>>n;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  ld ans=D();
  for(int _=0;_<1000;_++){
    int idx=rand()%n;
    ld rem=1-x[idx];
    for(int j=1;j<10;j++){
      x[idx]+=rem*pow(0.5,j);
      if(ans>D())ans=D();
      else x[idx]-=rem*pow(0.5,j);
    }
    rem=x[idx];
    for(int j=1;j<10;j++){
      x[idx]-=rem*pow(0.5,j);
      if(ans>D())ans=D();
      else x[idx]+=rem*pow(0.5,j);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6818181818181818
time_ms: 140
memory_kb: 3476
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ld=long double;
int n,v[100][100],p[100];
ld x[100];
ld D(){
  ld res=0;
  for(int i=0;i<n;i++){
    ld tmp=0;
    for(int j=0;j<n;j++)tmp+=(ld)v[j][i]*x[j];
    res+=((ld)p[i]-tmp)*((ld)p[i]-tmp);
  }
  return res;
}

signed main(){
  cin>>n;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
  for(int i=0;i<n;i++)cin>>p[i];
  for(int i=0;i<n;i++)x[i]=0.5;
  ld ans=D();
  for(int i=0;i<n;i++){
    for(int j=2;j<30;j++){
      x[i]+=pow(0.5,j);
      if(ans>D())ans=D();
      else x[i]-=pow(0.5,j);
      x[i]-=pow(0.5,j);
      if(ans>D())ans=D();
      else x[i]+=pow(0.5,j);
    }
  }
  cout<<fixed<<setprecision(12)<<sqrt(ans)<<endl;
}
accuracy: 0.6590909090909091
time_ms: 40
memory_kb: 3468
final_rank: 15

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 21;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

//-t rotate
void rotate(ld& x, ld& y, ld t) {
	t *= -1;
	ld nx = x * cosl(t) - y * sinl(t);
	ld ny = x * sinl(t) + y * cosl(t);
	swap(x, nx); swap(y, ny);
}

void solve() {
	int n; cin >> n;
	vector<vector<ld>> v(n, vector<ld>(n));
	rep(i, n)rep(j, n) {
		cin >> v[i][j];
	}
	vector<ld> p(n);
	rep(i, n)cin >> p[i];
	rep(j, n) {
		for (int i = j; i < n;i++) {
			if (abs(v[i][j]) < eps)continue;
			swap(v[i], v[j]);
			break;
		}
		for (int k = j + 1; k < n; k++) {
			ld t = atan2l(v[j][k], v[j][j]);
			for (int i = j; i < n; i++) {
				rotate(v[i][j], v[i][k], t);
			}
			rotate(p[j], p[k], t);
		}
	}
	/*rep(j, n-1) {
		for (int i = j; i < n; i++) {
			if (abs(v[i][j]) < eps && abs(v[i][j]) < eps)continue;
			swap(v[i], v[j]); break;
		}
		ld t = atan2l(v[j][j + 1], v[j][j]);
		for (int i = j; i < n; i++) {
			rotate(v[i][j], v[i][j + 1], t);
		}
		rotate(p[j], p[j + 1], t);
	}*/
	/*rep(i, n){
		rep(j, n) {
			cout << v[i][j] << " ";
		}cout << "\n";
	}*/
	if (v[n - 1][n - 1] < 0) {
		v[n - 1][n - 1] *= -1;
		p[n - 1] *= -1;
	}
	/*rep(i, n) {
		cout << v[i][i] << " " << p[i] << "\n";
	}*/
	ld ans = 0;
	rep(i, n) {
		if (p[i] < 0) {
			ans += p[i] * p[i];
		}
		else if (p[i] > v[i][i]) {
			ld dif = p[i] - v[i][i];
			ans += dif * dif;
		}
	}
	ans = sqrtl(ans);
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(8);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 36860
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

long double eps = 1e-6;

long double dist(vector<long double> a,vector<long double> b){
    long double sum=0.0;
    for(int i = 0; i < a.size(); ++i){
        sum+=powl((a[i]-b[i]),2);
    }
    return sum;
}

int main(){
    int n;
    cin>>n;
    vector<vector<long double>> ev(n,vector<long double>(n));
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin>>ev[i][j];
        }
    }
    vector<long double> p(n);
    for(int i = 0; i < n; ++i){
        cin>>p[i];
    }
    vector<long double> ans(n,0.0);
    vector<long double> t(n,0.0);
    vector<bool> used(n,false);
    for(int N = 0; N < n; ++N){
        long double tmindist=1e18;
        int tmind=-1;
        long double rat;
        for(int i = 0; i < n; ++i){
            if(used[i])continue;
            long double l=0.0,r=1.0;
            long double ml=0.33,mr=0.67;
            vector<long double>tt(n);
            for(int j = 0; j < 100; ++j){
                for(int k = 0; k < n; ++k){
                    tt[k]=ev[i][k]*ml+t[k];
                }
                long double td=dist(p,tt);

                for(int k = 0; k < n; ++k){
                    tt[k]=ev[i][k]*mr+t[k];
                }
                if(td<dist(p,tt)){
                    r=mr;
                    ml=(2*l+r)/3;
                    mr=(l+2*r)/3;
                }
                else{
                    l=ml;
                    ml=(2*l+r)/3;
                    mr=(l+2*r)/3;
                }
            }
            
            if(dist(p,tt)<tmindist){
                tmindist=dist(p,tt);
                tmind=i;
                rat=l;
            }

            //cerr<<i<<" "<<rat[i]<<endl;
        }
        for(int j = 0; j < n; ++j){
            t[j]+=ev[tmind][j]*rat;
        }
        used[tmind]=true;
    }
    
    
    cout<<fixed<<setprecision(15);
    cout<<sqrtl(dist(p,t))<<endl;
    return 0;
}
accuracy: 0.7045454545454546
time_ms: 340
memory_kb: 3644
final_rank: 4

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

long double eps = 1e-6;

long double dist(vector<long double> a,vector<long double> b){
    long double sum=0.0;
    for(int i = 0; i < a.size(); ++i){
        sum+=powl((a[i]-b[i]),2);
    }
    return sum;
}

int main(){
    int n;
    cin>>n;
    vector<vector<long double>> ev(n,vector<long double>(n));
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin>>ev[i][j];
        }
    }
    vector<long double> p(n);
    for(int i = 0; i < n; ++i){
        cin>>p[i];
    }

    vector<long double> t(n,0.0);
    for(int i = 0; i < n; ++i){
        long double l=0.0,r=1.0;
        long double ml=0.33,mr=0.67;
        vector<long double>tt(n);
        for(int j = 0; j < 500; ++j){
            for(int k = 0; k < n; ++k){
                tt[k]=ev[i][k]*ml+t[k];
            }
            long double td=dist(p,tt);

            for(int k = 0; k < n; ++k){
                tt[k]=ev[i][k]*mr+t[k];
            }
            if(td<dist(p,tt)){
                r=mr;
                ml=(2*l+r)/3;
                mr=(l+2*r)/3;
            }
            else{
                l=ml;
                ml=(2*l+r)/3;
                mr=(l+2*r)/3;
            }
        }
        for(int j = 0; j < n; ++j){
            t[j]+=ev[i][j]*l;
        }
        
        //cerr<<i<<" "<<rat[i]<<endl;
    }
    
    cout<<fixed<<setprecision(15);
    cout<<sqrtl(dist(p,t))<<endl;
    return 0;
}
accuracy: 0.7045454545454546
time_ms: 30
memory_kb: 3648
final_rank: 2

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
#define io_init cin.tie(0);ios::sync_with_stdio(0);cout<<fixed << setprecision(20)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

inline double score(vector<vector<double>>& V, vector<double>& x, vector<double>& p) {
	int N = V.size();
	double sc = 0;
	vector<double> tmp(V.size(), 0);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) tmp[j] += x[i] * V[i][j];
	}
	for (int i = 0; i < N; i++) {
		sc += (tmp[i] - p[i]) * (tmp[i] - p[i]);
	}
	return sc;
}

inline double score_(vector<vector<double>>& v, vector<double>& x, vector<double>& P, vector<double> &ep, int n, double dif) {
	int N = v.size(); double sc = 0;
	for (int i = 0; i < N; i++) {
		ep[i] += dif * v[n][i];
	}
	for (int i = 0; i < N; i++) {
		sc += (ep[i] - P[i]) * (ep[i] - P[i]);
	}
	return sc;
}

inline long long getTime() {
	return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

mt19937 mt;
int main() {
	io_init;
	ll inittime = getTime();
	int N;
	cin >> N;
	vector<vector<double>> V(N, vector<double>(N));
	for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)cin >> V[i][j];
	vector<double> P(N);
	for (int i = 0; i < N; i++)cin >> P[i];

	if (N == 1) {
		if (0 <= P[0] && P[0] <= V[0][0])cout << 0 << endl;
		else {
			cout << min(sqrt(abs(P[0])), sqrt(abs(P[0] - V[0][0]))) << endl;
		}
		return 0;
	}
	double stmp = 1e30;
	vector<double> x; //状態量
	for (int i = 0; i < 100; i++) {
		vector<double> candidate(N);
		for (int i = 0; i < N; i++)candidate[i] = (double)(mt() % 2);
		double tmp = score(V, candidate, P);
		if (tmp < stmp) {
			stmp = tmp;
			x = candidate;
		}
	}

	vector<double> ep(N, 0); //最近点の座標
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) ep[j] += x[i] * V[i][j];
	}
	uniform_real_distribution u(-0.1, 0.1);
	double ans = score(V, x, P);
	ll time = getTime() - inittime;
	for(int n = 0; ; n++) {
		if (n % 1000 == 0)time = getTime() - inittime;
		ll remainTime = 1000 - time;
		if (time > 980)break;
		int i = mt() % N;
		double p = u(mt) * ((double)remainTime / (double)1000);
		
		if (x[i] + p > 1)p = 1 - x[i];
		if (x[i] + p < 0)p = -x[i];

		if (mt() % 1000 == 0)p = (mt() % 2) - x[i];

		x[i] += p;
		double tmp = score_(V, x, P, ep, i, p);
		if (tmp < ans) {
			ans = tmp;
			continue;
		}
		else {
			score_(V, x, P, ep, i, -p);
			x[i] -= p;
		}
	}
	cout << sqrt(ans) << endl;
}
accuracy: 0.6818181818181818
time_ms: 980
memory_kb: 3652
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

long double eps = 1e-6;

long double dist(vector<long double> a,vector<long double> b){
    long double sum=0.0;
    for(int i = 0; i < a.size(); ++i){
        sum+=powl((a[i]-b[i]),2);
    }
    return sum;
}

int main(){
    int n;
    cin>>n;
    vector<vector<long double>> ev(n,vector<long double>(n));
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin>>ev[i][j];
        }
    }
    vector<long double> p(n);
    for(int i = 0; i < n; ++i){
        cin>>p[i];
    }

    vector<long double> rat(n);
    for(int i = 0; i < n; ++i){
        long double l=0.0,r=1.0;
        long double ml=0.33,mr=0.67;
        vector<long double>t(n);
        for(int j = 0; j < 5000; ++j){
            for(int k = 0; k < n; ++k){
                t[k]=ev[i][k]*ml;
            }
            long double td=dist(p,t);

            for(int k = 0; k < n; ++k){
                t[k]=ev[i][k]*mr;
            }
            if(td<dist(p,t)){
                r=mr;
                ml=l+(r-l)/3;
                mr=r-(r-l)/3;
            }
            else{
                l=ml;
                ml=l+(r-l)/3;
                mr=r-(r-l)/3;
            }
        }
        rat[i]=ml;
        //cerr<<i<<" "<<rat[i]<<endl;
    }
    vector<long double> t(n,0.0);
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            t[j]+=ev[i][j]*rat[i];
        }
//cerr<<t[i]<<" ";
    }
//cerr<<endl;
    cout<<fixed<<setprecision(15);
    cout<<sqrtl(dist(p,t))<<endl;
    return 0;
}
accuracy: 0.7045454545454546
time_ms: 310
memory_kb: 3636
final_rank: 3

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
#define io_init cin.tie(0);ios::sync_with_stdio(0);cout<<fixed << setprecision(20)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

inline double score(vector<vector<double>>& V, vector<double>& x, vector<double>& p) {
	int N = V.size();
	double sc = 0;
	vector<double> tmp(V.size(), 0);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) tmp[j] += x[i] * V[i][j];
	}
	for (int i = 0; i < N; i++) {
		sc += (tmp[i] - p[i]) * (tmp[i] - p[i]);
	}
	return sqrt(sc);
}

inline double score_(vector<vector<double>>& v, vector<double>& x, vector<double>& P, vector<double> &ep, int n, double dif) {
	int N = v.size(); double sc = 0;
	for (int i = 0; i < N; i++) {
		ep[i] += dif * v[n][i];
	}
	for (int i = 0; i < N; i++) {
		sc += (ep[i] - P[i]) * (ep[i] - P[i]);
	}
	return sqrt(sc);
}

inline long long getTime() {
	return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

mt19937 mt;
int main() {
	io_init;
	ll inittime = getTime();
	int N;
	cin >> N;
	vector<vector<double>> V(N, vector<double>(N));
	for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)cin >> V[i][j];
	vector<double> P(N);
	for (int i = 0; i < N; i++)cin >> P[i];

	if (N == 1) {
		if (0 <= P[0] && P[0] <= V[0][0])cout << 0 << endl;
		else {
			cout << min(sqrt(abs(P[0])), sqrt(abs(P[0] - V[0][0]))) << endl;
		}
		return 0;
	}
	double stmp = 1e30;
	vector<double> x; //状態量
	uniform_real_distribution gen1(0.0, 1.0);
	for (int i = 0; i < 100; i++) {
		vector<double> candidate(N);
		for (int i = 0; i < N; i++)candidate[i] = gen1(mt);
		double tmp = score(V, candidate, P);
		if (tmp < stmp) {
			stmp = tmp;
			x = candidate;
		}
	}

	vector<double> ep(N, 0); //最近点の座標
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) ep[j] += x[i] * V[i][j];
	}
	uniform_real_distribution u(-0.1, 0.1);
	double ans = score(V, x, P);
	ll time = getTime() - inittime;
	for(int n = 0; ; n++) {
		if (n % 1000 == 0)time = getTime() - inittime;
		ll remainTime = 1000 - time;
		if (time > 980)break;
		int i = mt() % N;
		double p = u(mt) * ((double)remainTime / (double)1000);

		if (x[i] + p > 1)p = 1 - x[i];
		if (x[i] + p < 0)p = -x[i];

		x[i] += p;
		double tmp = score_(V, x, P, ep, i, p);
		if (tmp < ans) {
			ans = tmp;
			continue;
		}
		else {
			score_(V, x, P, ep, i, -p);
			x[i] -= p;
		}
	}
	cout << ans << endl;
}
accuracy: 0.6818181818181818
time_ms: 980
memory_kb: 3660
final_rank: 13