output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 600005;
int base = 1000000 + 25;
long long ans = 0;
int pw[N + 5];
vector<int> divs;
vector<int> g[505];
vector<int> H[505];
int F[N];
int val[N];
int h[N + 5];
int a[N];
int n;
bool prime(int n) {
for (int i = 2; i * i <= n; ++i)
if (n % i == 0) return false;
return true;
}
inline int getHash(int l, int r) {
int ret = h[r];
if (l) ret -= h[l - 1];
ret *= pw[N - l];
return ret;
}
inline int getHashFromGcd(int c, int pos) {
int l = pos, r = pos + divs[c] - 1;
int ret = H[c][r];
if (l) ret -= H[c][l - 1];
ret *= pw[N - l];
return ret;
}
int gcd(int a, int b) {
while (a > 0 && b > 0)
if (a > b)
a %= b;
else
b %= a;
return (a + b);
}
int main() {
while (!prime(base)) base++;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
a[i + n] = a[i];
}
for (int i = 1; i < n; ++i) {
if (n % i == 0) {
divs.push_back(i);
}
}
pw[0] = 1;
for (int i = 1; i <= N; ++i) {
pw[i] = pw[i - 1] * base;
}
for (int i = 0; i < divs.size(); ++i) {
g[i].resize(2 * divs[i]);
for (int j = 0; j < n; ++j)
g[i][j % divs[i]] = max(g[i][j % divs[i]], a[j]);
for (int j = 0; j < divs[i]; ++j) g[i][j + divs[i]] = (g[i][j]);
int cur = 0;
for (int j = 0; j < divs[i] * 2; ++j) {
cur = cur + g[i][j] * pw[j];
H[i].push_back(cur);
}
}
int cur = 0;
for (int i = 0; i < 2 * n; ++i) {
cur = cur + a[i] * pw[i];
h[i] = cur;
}
int res = 0;
for (int j = 0; j < divs.size(); ++j) {
int last = 0;
for (int i = 1; i <= n / divs[j]; ++i) {
last += gcd(i, n / divs[j]) == 1;
F[i - 1] = last;
}
for (int i = 0; i < n + n; ++i) {
if (i + divs[j] - 1 >= n + n) {
val[i] = 0;
continue;
}
int H = getHashFromGcd(j, i % divs[j]);
if (H == getHash(i, i + divs[j] - 1))
val[i] = 1;
else
val[i] = 0;
}
for (int i = n + n - 1; i >= 0; --i) {
if (val[i]) {
val[i] = val[i + divs[j]] + 1;
if (val[i] >= n / divs[j]) val[i] = n / divs[j] - 1;
if (i < n) ans += F[val[i] - 1];
}
}
}
cout << ans << endl;
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 600005;
int base = 1000000 + 25;
long long ans = 0;
int pw[N + 5];
vector<int> divs;
vector<int> g[505];
vector<int> H[505];
int F[N];
int val[N];
int h[N + 5];
int a[N];
int n;
bool prime(int n) {
for (int i = 2; i * i <= n; ++i)
if (n % i == 0) return false;
return true;
}
inline int getHash(int l, int r) {
int ret = h[r];
if (l) ret -= h[l - 1];
ret *= pw[N - l];
return ret;
}
inline int getHashFromGcd(int c, int pos) {
int l = pos, r = pos + divs[c] - 1;
int ret = H[c][r];
if (l) ret -= H[c][l - 1];
ret *= pw[N - l];
return ret;
}
int gcd(int a, int b) {
while (a > 0 && b > 0)
if (a > b)
a %= b;
else
b %= a;
return (a + b);
}
int main() {
while (!prime(base)) base++;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
a[i + n] = a[i];
}
for (int i = 1; i < n; ++i) {
if (n % i == 0) {
divs.push_back(i);
}
}
pw[0] = 1;
for (int i = 1; i <= N; ++i) {
pw[i] = pw[i - 1] * base;
}
for (int i = 0; i < divs.size(); ++i) {
g[i].resize(2 * divs[i]);
for (int j = 0; j < n; ++j)
g[i][j % divs[i]] = max(g[i][j % divs[i]], a[j]);
for (int j = 0; j < divs[i]; ++j) g[i][j + divs[i]] = (g[i][j]);
int cur = 0;
for (int j = 0; j < divs[i] * 2; ++j) {
cur = cur + g[i][j] * pw[j];
H[i].push_back(cur);
}
}
int cur = 0;
for (int i = 0; i < 2 * n; ++i) {
cur = cur + a[i] * pw[i];
h[i] = cur;
}
int res = 0;
for (int j = 0; j < divs.size(); ++j) {
int last = 0;
for (int i = 1; i <= n / divs[j]; ++i) {
last += gcd(i, n / divs[j]) == 1;
F[i - 1] = last;
}
for (int i = 0; i < n + n; ++i) {
if (i + divs[j] - 1 >= n + n) {
val[i] = 0;
continue;
}
int H = getHashFromGcd(j, i % divs[j]);
if (H == getHash(i, i + divs[j] - 1))
val[i] = 1;
else
val[i] = 0;
}
for (int i = n + n - 1; i >= 0; --i) {
if (val[i]) {
val[i] = val[i + divs[j]] + 1;
if (val[i] >= n / divs[j]) val[i] = n / divs[j] - 1;
if (i < n) ans += F[val[i] - 1];
}
}
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(2 * n);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
a[n + i] = a[i];
}
vector<int> maxa;
vector<int> ds;
long long ans = 0;
vector<bool> covered(n, false);
for (int d = n - 1; d >= 1; --d) {
if (n % d != 0) {
continue;
}
ds.resize(0);
ds.push_back(0);
for (int x = d; x < n; x += d) {
if (!covered[x]) {
ds.push_back(x);
covered[x] = true;
}
}
maxa.resize(d);
for (int start = 0; start < d; ++start) {
maxa[start] = 0;
for (int x = start; x < n; x += d) {
maxa[start] = max(maxa[start], a[x]);
}
}
int end = 2 * n - 1;
int p = 1;
for (int l = 2 * n - 1; l >= 0; --l) {
if (a[l] < maxa[l % d]) {
end = l - 1;
p = 1;
continue;
}
if (l < n) {
int len = min(end - l + 1, n - 1);
while (p < ds.size() && ds[p] <= len) {
p++;
}
ans += p - 1;
}
}
}
cout << ans << endl;
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(2 * n);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
a[n + i] = a[i];
}
vector<int> maxa;
vector<int> ds;
long long ans = 0;
vector<bool> covered(n, false);
for (int d = n - 1; d >= 1; --d) {
if (n % d != 0) {
continue;
}
ds.resize(0);
ds.push_back(0);
for (int x = d; x < n; x += d) {
if (!covered[x]) {
ds.push_back(x);
covered[x] = true;
}
}
maxa.resize(d);
for (int start = 0; start < d; ++start) {
maxa[start] = 0;
for (int x = start; x < n; x += d) {
maxa[start] = max(maxa[start], a[x]);
}
}
int end = 2 * n - 1;
int p = 1;
for (int l = 2 * n - 1; l >= 0; --l) {
if (a[l] < maxa[l % d]) {
end = l - 1;
p = 1;
continue;
}
if (l < n) {
int len = min(end - l + 1, n - 1);
while (p < ds.size() && ds[p] <= len) {
p++;
}
ans += p - 1;
}
}
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
const int maxn = 3e5 + 10;
int n, s, g;
long long ans;
int a[maxn], b[maxn], c[maxn], d[maxn];
int GCD[maxn];
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
void calc(int g) {
for (int i = 0; i <= g - 1; i++) b[i] = 0;
for (int i = 0; i <= n - 1; i++) {
int v = i % g;
b[v] = max(b[v], a[i]);
d[i] = c[i] = 0;
}
d[n] = 0;
for (int i = 0; i <= n - 1; i++)
if (a[i] == b[i % g]) c[i] = 1;
bool flag = false;
for (int l = 0; l < n;) {
int r = (l + 1) % n;
if (c[l]) {
l++;
continue;
}
flag = true;
int len = 0;
while (c[r]) {
len++, r = (r + 1) % n;
}
for (int i = 1; i <= len; ++i) d[i] += len - i + 1;
if (r <= l) break;
l = r;
}
if (!flag)
for (int i = 1; i <= n; i++) d[i] += n;
for (int i = 1; i <= n; i++)
if (GCD[i] == g) ans += 1ll * d[i];
}
int main() {
scanf("%d", &n);
for (int i = 0; i <= n - 1; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) GCD[i] = gcd(i, n);
for (int g = 1; g < n; g++)
if (n % g == 0) {
calc(g);
}
cout << ans << endl;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
const int maxn = 3e5 + 10;
int n, s, g;
long long ans;
int a[maxn], b[maxn], c[maxn], d[maxn];
int GCD[maxn];
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
void calc(int g) {
for (int i = 0; i <= g - 1; i++) b[i] = 0;
for (int i = 0; i <= n - 1; i++) {
int v = i % g;
b[v] = max(b[v], a[i]);
d[i] = c[i] = 0;
}
d[n] = 0;
for (int i = 0; i <= n - 1; i++)
if (a[i] == b[i % g]) c[i] = 1;
bool flag = false;
for (int l = 0; l < n;) {
int r = (l + 1) % n;
if (c[l]) {
l++;
continue;
}
flag = true;
int len = 0;
while (c[r]) {
len++, r = (r + 1) % n;
}
for (int i = 1; i <= len; ++i) d[i] += len - i + 1;
if (r <= l) break;
l = r;
}
if (!flag)
for (int i = 1; i <= n; i++) d[i] += n;
for (int i = 1; i <= n; i++)
if (GCD[i] == g) ans += 1ll * d[i];
}
int main() {
scanf("%d", &n);
for (int i = 0; i <= n - 1; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) GCD[i] = gcd(i, n);
for (int g = 1; g < n; g++)
if (n % g == 0) {
calc(g);
}
cout << ans << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
int n, divN;
vector<int> a;
set<int> DIV;
map<int, int> p;
map<int, int> lens[100000];
void addDiv(map<int, int>::iterator it, int curr) {
if (it == p.end()) return;
int t = 1;
for (int i = 0; i < (int)it->second + 1; i++) {
DIV.insert(curr * t);
map<int, int>::iterator it1 = it;
it1++;
addDiv(it1, curr * t);
t *= it->first;
}
}
void setDiv(int n) {
while (n % 2 == 0) p[2]++, n /= 2;
int eIdx = int(sqrt(double(n))) + 10, idx = 3;
while (idx < eIdx && n > 1) {
while (n % idx == 0) p[idx]++, n /= idx;
idx += 2;
}
if (n > 1) p[n]++;
addDiv(p.begin(), 1);
}
int getGCD(int a, int b) {
if (b == 0) return a;
if (a < b) return getGCD(b, a);
if (a % b == 0) return b;
return getGCD(b, a % b);
}
int main() {
cin >> n;
a.resize(n);
setDiv(n);
divN = DIV.size();
for (int i = 0; i < (int)n; i++) cin >> a[i];
int cnt = 0;
for (set<int>::iterator it = DIV.begin(); it != DIV.end(); it++, cnt++) {
int t = getGCD(n, *it);
if (t == n) break;
vector<int> maxVal(t, 0);
vector<bool> isMax(n, 0);
for (int i = 0; i < (int)n; i++) {
int idx = i % t;
if (maxVal[idx] < a[i]) maxVal[idx] = a[i];
}
for (int i = 0; i < (int)n; i++) {
int idx = i % t;
if (maxVal[idx] == a[i]) isMax[i] = 1;
}
int fIdx = -1;
for (int i = 0; i < (int)n; i++) {
if (!isMax[i]) {
fIdx = i;
break;
}
}
if (fIdx == -1) {
lens[cnt][n]++;
;
} else {
int cnt1 = 0;
for (int i = fIdx; i < (int)fIdx + n; i++) {
int idx = i % n;
if (isMax[idx])
cnt1++;
else {
if (cnt1) lens[cnt][cnt1]++;
cnt1 = 0;
}
}
if (cnt1) {
lens[cnt][cnt1]++;
}
}
}
long long ans = 0;
for (int k = 1; k < n; k++) {
int t = getGCD(n, k);
set<int>::iterator it = DIV.lower_bound(t);
int idx = distance(DIV.begin(), it);
for (map<int, int>::iterator it = lens[idx].begin(); it != lens[idx].end();
it++) {
if (it->first == n) {
ans += n;
break;
}
if (it->first >= k) {
ans += (it->first - k + 1) * it->second;
}
}
}
cout << ans << endl;
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
int n, divN;
vector<int> a;
set<int> DIV;
map<int, int> p;
map<int, int> lens[100000];
void addDiv(map<int, int>::iterator it, int curr) {
if (it == p.end()) return;
int t = 1;
for (int i = 0; i < (int)it->second + 1; i++) {
DIV.insert(curr * t);
map<int, int>::iterator it1 = it;
it1++;
addDiv(it1, curr * t);
t *= it->first;
}
}
void setDiv(int n) {
while (n % 2 == 0) p[2]++, n /= 2;
int eIdx = int(sqrt(double(n))) + 10, idx = 3;
while (idx < eIdx && n > 1) {
while (n % idx == 0) p[idx]++, n /= idx;
idx += 2;
}
if (n > 1) p[n]++;
addDiv(p.begin(), 1);
}
int getGCD(int a, int b) {
if (b == 0) return a;
if (a < b) return getGCD(b, a);
if (a % b == 0) return b;
return getGCD(b, a % b);
}
int main() {
cin >> n;
a.resize(n);
setDiv(n);
divN = DIV.size();
for (int i = 0; i < (int)n; i++) cin >> a[i];
int cnt = 0;
for (set<int>::iterator it = DIV.begin(); it != DIV.end(); it++, cnt++) {
int t = getGCD(n, *it);
if (t == n) break;
vector<int> maxVal(t, 0);
vector<bool> isMax(n, 0);
for (int i = 0; i < (int)n; i++) {
int idx = i % t;
if (maxVal[idx] < a[i]) maxVal[idx] = a[i];
}
for (int i = 0; i < (int)n; i++) {
int idx = i % t;
if (maxVal[idx] == a[i]) isMax[i] = 1;
}
int fIdx = -1;
for (int i = 0; i < (int)n; i++) {
if (!isMax[i]) {
fIdx = i;
break;
}
}
if (fIdx == -1) {
lens[cnt][n]++;
;
} else {
int cnt1 = 0;
for (int i = fIdx; i < (int)fIdx + n; i++) {
int idx = i % n;
if (isMax[idx])
cnt1++;
else {
if (cnt1) lens[cnt][cnt1]++;
cnt1 = 0;
}
}
if (cnt1) {
lens[cnt][cnt1]++;
}
}
}
long long ans = 0;
for (int k = 1; k < n; k++) {
int t = getGCD(n, k);
set<int>::iterator it = DIV.lower_bound(t);
int idx = distance(DIV.begin(), it);
for (map<int, int>::iterator it = lens[idx].begin(); it != lens[idx].end();
it++) {
if (it->first == n) {
ans += n;
break;
}
if (it->first >= k) {
ans += (it->first - k + 1) * it->second;
}
}
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int M = 200000;
int n;
int a[2 * M];
int gds[M];
int divs[500] = {0};
int nd = 0;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
memcpy(a + n, a, sizeof(a));
for (int d = 1; d < n; d++)
if (n % d == 0) divs[nd++] = d;
for (int i = 1; i < n; i++) gds[i] = gcd(i, n);
long long res = 0;
for (int i = 0; i < nd; i++) {
int d = divs[i];
int mx[M] = {0};
for (int j = 0; j < n; j++) {
int idx = j % d;
mx[idx] = max(mx[idx], a[j]);
}
bool all = true;
for (int j = 0; j < n; j++) {
int idx = j % d;
all &= mx[idx] == a[j];
}
if (all) {
for (int j = 0; j < n; j++)
if (gds[j] == d) res += n;
} else {
int ofst;
for (ofst = 0; mx[ofst % d] == a[ofst]; ofst++)
;
int j = ofst;
while (j < n + ofst) {
for (; mx[j % d] != a[j] && j < n + ofst; j++)
;
int len = 0;
for (; mx[j % d] == a[j] && j < n + ofst; j++, len++)
;
for (int k = 1; k <= len; k++)
if (gds[k] == d) res += len - k + 1;
}
}
}
printf("%lld", res);
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int M = 200000;
int n;
int a[2 * M];
int gds[M];
int divs[500] = {0};
int nd = 0;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
memcpy(a + n, a, sizeof(a));
for (int d = 1; d < n; d++)
if (n % d == 0) divs[nd++] = d;
for (int i = 1; i < n; i++) gds[i] = gcd(i, n);
long long res = 0;
for (int i = 0; i < nd; i++) {
int d = divs[i];
int mx[M] = {0};
for (int j = 0; j < n; j++) {
int idx = j % d;
mx[idx] = max(mx[idx], a[j]);
}
bool all = true;
for (int j = 0; j < n; j++) {
int idx = j % d;
all &= mx[idx] == a[j];
}
if (all) {
for (int j = 0; j < n; j++)
if (gds[j] == d) res += n;
} else {
int ofst;
for (ofst = 0; mx[ofst % d] == a[ofst]; ofst++)
;
int j = ofst;
while (j < n + ofst) {
for (; mx[j % d] != a[j] && j < n + ofst; j++)
;
int len = 0;
for (; mx[j % d] == a[j] && j < n + ofst; j++, len++)
;
for (int k = 1; k <= len; k++)
if (gds[k] == d) res += len - k + 1;
}
}
}
printf("%lld", res);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 500001;
int n;
int a[N];
vector<int> g[N];
int c[N];
long long d[N];
bool used[N];
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = n + 1; i <= 2 * n; ++i) a[i] = a[i % n];
for (int i = 1; i < n; ++i) {
int k = gcd(i, n);
g[k].push_back(i);
}
long long ans = 0;
for (int i = 1; i < n; ++i) {
if (g[i].size() == 0) continue;
for (int j = 1; j <= i; ++j) {
int mx = a[j];
for (int k = j; k <= 2 * n; k += i) mx = max(mx, a[k]);
for (int k = j; k <= 2 * n; k += i) c[k] = mx;
}
int l = 1;
int r = 1;
while (l <= n) {
if (r == 2 * n) {
for (int j = l; j < min(r, n + 1); ++j) {
int len = r - j;
d[len]++;
}
break;
}
if (a[r] == c[r])
r++;
else {
for (int j = l; j < min(r, n + 1); ++j) {
int len = r - j;
d[len]++;
}
l = r + 1;
r = l;
}
}
for (int j = 0; j < g[i].size(); ++j) used[g[i][j]] = true;
for (int j = 2 * n; j >= 1; --j) {
if (used[j] == true) ans += d[j];
d[j - 1] += d[j];
d[j] = 0;
}
d[0] = 0;
for (int j = 0; j < g[i].size(); ++j) used[g[i][j]] = false;
}
cout << ans << endl;
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 500001;
int n;
int a[N];
vector<int> g[N];
int c[N];
long long d[N];
bool used[N];
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = n + 1; i <= 2 * n; ++i) a[i] = a[i % n];
for (int i = 1; i < n; ++i) {
int k = gcd(i, n);
g[k].push_back(i);
}
long long ans = 0;
for (int i = 1; i < n; ++i) {
if (g[i].size() == 0) continue;
for (int j = 1; j <= i; ++j) {
int mx = a[j];
for (int k = j; k <= 2 * n; k += i) mx = max(mx, a[k]);
for (int k = j; k <= 2 * n; k += i) c[k] = mx;
}
int l = 1;
int r = 1;
while (l <= n) {
if (r == 2 * n) {
for (int j = l; j < min(r, n + 1); ++j) {
int len = r - j;
d[len]++;
}
break;
}
if (a[r] == c[r])
r++;
else {
for (int j = l; j < min(r, n + 1); ++j) {
int len = r - j;
d[len]++;
}
l = r + 1;
r = l;
}
}
for (int j = 0; j < g[i].size(); ++j) used[g[i][j]] = true;
for (int j = 2 * n; j >= 1; --j) {
if (used[j] == true) ans += d[j];
d[j - 1] += d[j];
d[j] = 0;
}
d[0] = 0;
for (int j = 0; j < g[i].size(); ++j) used[g[i][j]] = false;
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int N, S[200000], CNT[200000], GCD[200000];
bool VIS[200000];
int gcd(const int &a, const int &b) { return b ? gcd(b, a % b) : a; }
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d", &S[i]);
for (int i = 1; i < N; i++) GCD[i] = gcd(i, N);
long long ans = 0LL;
for (int g = 1; g < N; g++) {
if (N % g != 0) continue;
bool all = true;
for (int i = 0; i < g; i++) {
int mx = 0;
for (int j = i; j < N; j += g) mx = max(mx, S[j]);
for (int j = i; j < N; j += g) all &= (VIS[j] = (S[j] >= mx));
}
if (all) {
for (int i = 1; i < N; i++)
if (GCD[i] == g) ans += N;
} else {
for (int i = 1; i < N; i++) CNT[i] = 0;
for (int i = 0, j = 0; i < N;) {
if (!VIS[i]) {
i++, j = i;
continue;
}
while (VIS[(++j) % N])
;
for (int k = i; k < N && k < j; k++) CNT[j - k]++;
i = j;
}
for (int i = N - 2; i >= 1; i--) CNT[i] += CNT[i + 1];
for (int i = 1; i < N; i++)
if (GCD[i] == g) ans += CNT[i];
}
}
printf("%I64d\n", ans);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N, S[200000], CNT[200000], GCD[200000];
bool VIS[200000];
int gcd(const int &a, const int &b) { return b ? gcd(b, a % b) : a; }
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d", &S[i]);
for (int i = 1; i < N; i++) GCD[i] = gcd(i, N);
long long ans = 0LL;
for (int g = 1; g < N; g++) {
if (N % g != 0) continue;
bool all = true;
for (int i = 0; i < g; i++) {
int mx = 0;
for (int j = i; j < N; j += g) mx = max(mx, S[j]);
for (int j = i; j < N; j += g) all &= (VIS[j] = (S[j] >= mx));
}
if (all) {
for (int i = 1; i < N; i++)
if (GCD[i] == g) ans += N;
} else {
for (int i = 1; i < N; i++) CNT[i] = 0;
for (int i = 0, j = 0; i < N;) {
if (!VIS[i]) {
i++, j = i;
continue;
}
while (VIS[(++j) % N])
;
for (int k = i; k < N && k < j; k++) CNT[j - k]++;
i = j;
}
for (int i = N - 2; i >= 1; i--) CNT[i] += CNT[i + 1];
for (int i = 1; i < N; i++)
if (GCD[i] == g) ans += CNT[i];
}
}
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
vector<int> v[N / 2];
int a[N], maxi[N];
int gdc(int x, int y) {
if (x == 0) return y;
return gdc(y % x, x);
}
int main() {
int n, i, x, j, index, cc;
cin >> n;
for (i = 0; i < n; ++i) cin >> a[i], a[i + n] = a[i];
for (i = 1; i < n; ++i) {
v[gdc(i, n)].push_back(i);
}
long long ret = 0;
for (i = 1; i < n; ++i)
if (v[i].size() > 0) {
index = 0, cc = 0;
memset(maxi, 0, sizeof(maxi));
for (j = 0; j < n * 2; ++j) maxi[j % i] = max(maxi[j % i], a[j]);
for (j = 0; j < n * 2; ++j) {
if (a[j] >= maxi[j % i]) {
++cc;
if (index < v[i].size() && v[i][index] <= cc) ++index;
if (j >= n) ret += index;
} else
index = 0, cc = 0;
}
}
cout << ret << endl;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
vector<int> v[N / 2];
int a[N], maxi[N];
int gdc(int x, int y) {
if (x == 0) return y;
return gdc(y % x, x);
}
int main() {
int n, i, x, j, index, cc;
cin >> n;
for (i = 0; i < n; ++i) cin >> a[i], a[i + n] = a[i];
for (i = 1; i < n; ++i) {
v[gdc(i, n)].push_back(i);
}
long long ret = 0;
for (i = 1; i < n; ++i)
if (v[i].size() > 0) {
index = 0, cc = 0;
memset(maxi, 0, sizeof(maxi));
for (j = 0; j < n * 2; ++j) maxi[j % i] = max(maxi[j % i], a[j]);
for (j = 0; j < n * 2; ++j) {
if (a[j] >= maxi[j % i]) {
++cc;
if (index < v[i].size() && v[i][index] <= cc) ++index;
if (j >= n) ret += index;
} else
index = 0, cc = 0;
}
}
cout << ret << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int a[N];
int gc[N];
int is_max[N];
int cnt[N];
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int _n(n), i(0); i < _n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
long long ans = 0;
gc[0] = 0;
for (int _n(n + 1), g(1); g < _n; g++) {
if (n % g) continue;
for (int i = g; i <= n; i += g) {
gc[i] = g;
}
}
for (int _n(n + 1), g(1); g < _n; g++) {
if (n % g) continue;
for (int _n(g), i(0); i < _n; i++) {
int m = 0;
for (int j = i; j < n; j += g) {
m = max(m, a[j]);
}
for (int j = i; j < 2 * n; j += g) {
is_max[j] = a[j] == m ? 1 : 0;
}
}
is_max[0] = 0;
for (int _n(2 * n), i(1); i < _n; i++) {
if (is_max[i]) {
is_max[i] += is_max[i - 1];
}
}
cnt[0] = 0;
for (int _n(n), i(1); i < _n; i++) cnt[i] = cnt[i - 1] + (gc[i] == g);
for (int _n(2 * n), i(n); i < _n; i++) {
ans += cnt[min(n - 1, is_max[i])];
}
}
cout << ans << endl;
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int a[N];
int gc[N];
int is_max[N];
int cnt[N];
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int _n(n), i(0); i < _n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
long long ans = 0;
gc[0] = 0;
for (int _n(n + 1), g(1); g < _n; g++) {
if (n % g) continue;
for (int i = g; i <= n; i += g) {
gc[i] = g;
}
}
for (int _n(n + 1), g(1); g < _n; g++) {
if (n % g) continue;
for (int _n(g), i(0); i < _n; i++) {
int m = 0;
for (int j = i; j < n; j += g) {
m = max(m, a[j]);
}
for (int j = i; j < 2 * n; j += g) {
is_max[j] = a[j] == m ? 1 : 0;
}
}
is_max[0] = 0;
for (int _n(2 * n), i(1); i < _n; i++) {
if (is_max[i]) {
is_max[i] += is_max[i - 1];
}
}
cnt[0] = 0;
for (int _n(n), i(1); i < _n; i++) cnt[i] = cnt[i - 1] + (gc[i] == g);
for (int _n(2 * n), i(n); i < _n; i++) {
ans += cnt[min(n - 1, is_max[i])];
}
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int A[200005 << 1], n, ln[200005 << 1], C[200005 + 200005];
long long ans;
bool mark[200005 << 1];
int Gcd(int a, int b) { return b ? Gcd(b, a % b) : a; }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
A[i + n] = A[i];
}
for (int i = 1; i <= n; i++) {
if (n % i) continue;
memset(mark, 0, sizeof(mark));
for (int j = 0; j < i; j++) {
int mx = 0;
for (int t = j; t < n + n; t += i) mx = max(mx, A[t]);
for (int t = j; t < n + n; t += i)
if (A[t] == mx) mark[t] = true;
}
ln[0] = mark[0];
for (int j = 1; j < n + n; j++) {
if (mark[j] == true)
ln[j] = ln[j - 1] + 1;
else
ln[j] = 0;
if (ln[j] == n) ln[j]--;
}
C[0] = 0;
for (int j = 1; j < (n / i); j++) C[j] = C[j - 1] + (Gcd(j, n / i) == 1);
for (int j = n; j < n + n; j++) ans += C[ln[j] / i];
}
cout << ans << endl;
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int A[200005 << 1], n, ln[200005 << 1], C[200005 + 200005];
long long ans;
bool mark[200005 << 1];
int Gcd(int a, int b) { return b ? Gcd(b, a % b) : a; }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
A[i + n] = A[i];
}
for (int i = 1; i <= n; i++) {
if (n % i) continue;
memset(mark, 0, sizeof(mark));
for (int j = 0; j < i; j++) {
int mx = 0;
for (int t = j; t < n + n; t += i) mx = max(mx, A[t]);
for (int t = j; t < n + n; t += i)
if (A[t] == mx) mark[t] = true;
}
ln[0] = mark[0];
for (int j = 1; j < n + n; j++) {
if (mark[j] == true)
ln[j] = ln[j - 1] + 1;
else
ln[j] = 0;
if (ln[j] == n) ln[j]--;
}
C[0] = 0;
for (int j = 1; j < (n / i); j++) C[j] = C[j - 1] + (Gcd(j, n / i) == 1);
for (int j = n; j < n + n; j++) ans += C[ln[j] / i];
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
const int maxn = 400010;
using namespace std;
int n, f[maxn], a[maxn], g[maxn], cnt[maxn];
long long ans;
bool bo[maxn];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]), a[i + n] = a[i];
for (int d = 1; d < n; d++) {
if (n % d == 0) {
memset(bo, 0, sizeof(bo));
for (int k = 0; k < d; k++) {
g[k] = 0;
for (int i = k; i < (n << 1); i += d) g[k] = max(g[k], a[i]);
for (int i = k; i < (n << 1); i += d)
if (a[i] == g[k]) bo[i] = 1;
}
f[0] = bo[0];
for (int i = 1; i < (n << 1); i++) {
if (bo[i])
f[i] = f[i - 1] + 1;
else
f[i] = 0;
f[i] = min(f[i], n - 1);
}
cnt[0] = 0;
for (int i = 1; i < (n / d); i++)
cnt[i] = cnt[i - 1] + (gcd(i, n / d) == 1);
for (int i = n; i < (n << 1); i++) ans += cnt[f[i] / d];
}
}
printf("%I64d\n", ans);
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
const int maxn = 400010;
using namespace std;
int n, f[maxn], a[maxn], g[maxn], cnt[maxn];
long long ans;
bool bo[maxn];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]), a[i + n] = a[i];
for (int d = 1; d < n; d++) {
if (n % d == 0) {
memset(bo, 0, sizeof(bo));
for (int k = 0; k < d; k++) {
g[k] = 0;
for (int i = k; i < (n << 1); i += d) g[k] = max(g[k], a[i]);
for (int i = k; i < (n << 1); i += d)
if (a[i] == g[k]) bo[i] = 1;
}
f[0] = bo[0];
for (int i = 1; i < (n << 1); i++) {
if (bo[i])
f[i] = f[i - 1] + 1;
else
f[i] = 0;
f[i] = min(f[i], n - 1);
}
cnt[0] = 0;
for (int i = 1; i < (n / d); i++)
cnt[i] = cnt[i - 1] + (gcd(i, n / d) == 1);
for (int i = n; i < (n << 1); i++) ans += cnt[f[i] / d];
}
}
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int n, v[200200], is[200200 * 2], su[200200], g[200200];
long long as;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]), g[i] = gcd(i, n);
for (int i = 1; i <= n; i++)
if (!(n % i)) {
for (int j = 1; j <= n; j++) is[j] = is[j + n] = su[j] = 0;
for (int j = 1; j < n; j++) su[j] = (g[j] == g[i]) + su[j - 1];
for (int j = 1; j <= g[i]; j++) {
int mx = 0;
for (int k = j; k <= n; k += g[i])
if (mx < v[k]) mx = v[k];
for (int k = j; k <= n; k += g[i])
if (mx == v[k]) is[k] = is[n + k] = 1;
}
for (int j = n * 2 - 1; j >= 1; j--) is[j] = is[j] ? is[j + 1] + 1 : 0;
for (int j = 1; j <= n; j++) as += su[is[j] >= n ? n - 1 : is[j]];
}
printf("%lld\n", as);
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int n, v[200200], is[200200 * 2], su[200200], g[200200];
long long as;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]), g[i] = gcd(i, n);
for (int i = 1; i <= n; i++)
if (!(n % i)) {
for (int j = 1; j <= n; j++) is[j] = is[j + n] = su[j] = 0;
for (int j = 1; j < n; j++) su[j] = (g[j] == g[i]) + su[j - 1];
for (int j = 1; j <= g[i]; j++) {
int mx = 0;
for (int k = j; k <= n; k += g[i])
if (mx < v[k]) mx = v[k];
for (int k = j; k <= n; k += g[i])
if (mx == v[k]) is[k] = is[n + k] = 1;
}
for (int j = n * 2 - 1; j >= 1; j--) is[j] = is[j] ? is[j + 1] + 1 : 0;
for (int j = 1; j <= n; j++) as += su[is[j] >= n ? n - 1 : is[j]];
}
printf("%lld\n", as);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, maxk = 200;
int n, a[maxn + 1];
void read() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
}
vector<int> b[maxk + 1];
int d[maxk + 1], tt = 0;
int s1[maxn + 1], s[maxn + 1], sum, ok[maxn + 1];
long long ans = 0;
void work() {
for (int i = 1; i <= n - 1; i++) {
if (n % i == 0) {
tt++;
assert(tt <= maxk);
d[tt] = i;
b[tt].push_back(i);
} else {
for (int j = tt; j > 0; j--) {
if (i % d[j] == 0) {
b[j].push_back(i);
break;
}
}
}
}
s[0] = 0;
for (int i = 1; i <= tt; i++) {
sum = 0;
memset(s1, 0, sizeof(s1));
for (int j : b[i]) s1[j]++;
for (int j = 1; j <= n; j++) {
sum += s1[j];
s[j] = s[j - 1] + sum;
}
memset(ok, 0, sizeof(ok));
int maxi = 0;
bool allok = 1;
for (int k = 1; k <= d[i]; k++) {
maxi = 0;
for (int j = k; j <= n; j += d[i]) {
maxi = max(maxi, a[j]);
}
for (int j = k; j <= n; j += d[i]) {
if (a[j] == maxi)
ok[j] = 1;
else
allok = 0;
}
}
int l = 1, r, m = n + 1;
if (allok) {
ans += 1ll * n * b[i].size();
continue;
}
if (ok[1] && ok[n]) {
r = l;
while (r < n && ok[r + 1]) r++;
int len = r;
while (m - 1 > r && ok[m - 1]) m--;
len += n - m + 1;
ans += s[len];
l = r + 1;
}
m--;
while (1) {
while (l < m && ok[l] == 0) l++;
r = l;
while (r < m && ok[r + 1] == 1) r++;
if (ok[l] == 0) break;
ans += s[r - l + 1];
if (r == m) break;
l = r + 1;
}
}
printf("%lld\n", ans);
return;
}
int main() {
read();
work();
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, maxk = 200;
int n, a[maxn + 1];
void read() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
}
vector<int> b[maxk + 1];
int d[maxk + 1], tt = 0;
int s1[maxn + 1], s[maxn + 1], sum, ok[maxn + 1];
long long ans = 0;
void work() {
for (int i = 1; i <= n - 1; i++) {
if (n % i == 0) {
tt++;
assert(tt <= maxk);
d[tt] = i;
b[tt].push_back(i);
} else {
for (int j = tt; j > 0; j--) {
if (i % d[j] == 0) {
b[j].push_back(i);
break;
}
}
}
}
s[0] = 0;
for (int i = 1; i <= tt; i++) {
sum = 0;
memset(s1, 0, sizeof(s1));
for (int j : b[i]) s1[j]++;
for (int j = 1; j <= n; j++) {
sum += s1[j];
s[j] = s[j - 1] + sum;
}
memset(ok, 0, sizeof(ok));
int maxi = 0;
bool allok = 1;
for (int k = 1; k <= d[i]; k++) {
maxi = 0;
for (int j = k; j <= n; j += d[i]) {
maxi = max(maxi, a[j]);
}
for (int j = k; j <= n; j += d[i]) {
if (a[j] == maxi)
ok[j] = 1;
else
allok = 0;
}
}
int l = 1, r, m = n + 1;
if (allok) {
ans += 1ll * n * b[i].size();
continue;
}
if (ok[1] && ok[n]) {
r = l;
while (r < n && ok[r + 1]) r++;
int len = r;
while (m - 1 > r && ok[m - 1]) m--;
len += n - m + 1;
ans += s[len];
l = r + 1;
}
m--;
while (1) {
while (l < m && ok[l] == 0) l++;
r = l;
while (r < m && ok[r + 1] == 1) r++;
if (ok[l] == 0) break;
ans += s[r - l + 1];
if (r == m) break;
l = r + 1;
}
}
printf("%lld\n", ans);
return;
}
int main() {
read();
work();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int main() {
int n;
cin >> n;
int niza[n];
for (int ctr1 = 0; ctr1 < n; ctr1++) cin >> niza[ctr1];
vector<int> vek[n + 1];
for (int ctr1 = 1; ctr1 < n; ctr1++) vek[gcd(ctr1, n)].push_back(ctr1);
long long rez = 0;
int big[n];
bool ok[n * 2];
for (int ctr1 = 1; ctr1 <= n; ctr1++) {
if (vek[ctr1].size() == 0) continue;
memset(big, -1, sizeof(big));
for (int ctr2 = 0; ctr2 < n; ctr2++)
if (niza[ctr2] > big[ctr2 % ctr1]) big[ctr2 % ctr1] = niza[ctr2];
for (int ctr2 = 0; ctr2 < 2 * n; ctr2++)
ok[ctr2] = (niza[ctr2 % n] >= big[ctr2 % ctr1]);
int cons = 0;
for (int ctr2 = 0; ctr2 < 2 * n; ctr2++) {
if (ok[ctr2])
cons++;
else
cons = 0;
if (ctr2 >= n)
rez += upper_bound(vek[ctr1].begin(), vek[ctr1].end(), cons) -
vek[ctr1].begin();
}
}
cout << rez << endl;
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int main() {
int n;
cin >> n;
int niza[n];
for (int ctr1 = 0; ctr1 < n; ctr1++) cin >> niza[ctr1];
vector<int> vek[n + 1];
for (int ctr1 = 1; ctr1 < n; ctr1++) vek[gcd(ctr1, n)].push_back(ctr1);
long long rez = 0;
int big[n];
bool ok[n * 2];
for (int ctr1 = 1; ctr1 <= n; ctr1++) {
if (vek[ctr1].size() == 0) continue;
memset(big, -1, sizeof(big));
for (int ctr2 = 0; ctr2 < n; ctr2++)
if (niza[ctr2] > big[ctr2 % ctr1]) big[ctr2 % ctr1] = niza[ctr2];
for (int ctr2 = 0; ctr2 < 2 * n; ctr2++)
ok[ctr2] = (niza[ctr2 % n] >= big[ctr2 % ctr1]);
int cons = 0;
for (int ctr2 = 0; ctr2 < 2 * n; ctr2++) {
if (ok[ctr2])
cons++;
else
cons = 0;
if (ctr2 >= n)
rez += upper_bound(vek[ctr1].begin(), vek[ctr1].end(), cons) -
vek[ctr1].begin();
}
}
cout << rez << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int n, a[N], b[N], c[N], mx[N], gc[N];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) gc[i] = gcd(i, n);
long long ans = 0;
for (int g = 1; g < n; g++)
if (n % g == 0) {
for (int i = 1; i <= n; i++) c[i] = c[i - 1] + (gc[i] == g);
for (int i = 0; i < g; i++) mx[i] = a[i];
for (int i = g; i < n; i++) mx[i % g] = max(mx[i % g], a[i]);
for (int i = 0; i < n; i++) b[i] = b[i + n] = (mx[i % g] == a[i]);
for (int i = n + n - 2; i >= 0; i--)
if (b[i]) b[i] += b[i + 1];
for (int i = 0; i < n; i++) ans += c[min(n - 1, b[i])];
}
printf("%lld", ans);
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int n, a[N], b[N], c[N], mx[N], gc[N];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) gc[i] = gcd(i, n);
long long ans = 0;
for (int g = 1; g < n; g++)
if (n % g == 0) {
for (int i = 1; i <= n; i++) c[i] = c[i - 1] + (gc[i] == g);
for (int i = 0; i < g; i++) mx[i] = a[i];
for (int i = g; i < n; i++) mx[i % g] = max(mx[i % g], a[i]);
for (int i = 0; i < n; i++) b[i] = b[i + n] = (mx[i % g] == a[i]);
for (int i = n + n - 2; i >= 0; i--)
if (b[i]) b[i] += b[i + 1];
for (int i = 0; i < n; i++) ans += c[min(n - 1, b[i])];
}
printf("%lld", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int n, v[200050], is[200050 * 2], su[200050], g[200050];
long long as;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]), g[i] = gcd(i, n);
for (int i = 1; i <= n; i++)
if (!(n % i)) {
for (int j = 1; j <= n; j++) is[j] = is[j + n] = su[j] = 0;
for (int j = 1; j < n; j++) su[j] = (g[j] == g[i]) + su[j - 1];
for (int j = 1; j <= g[i]; j++) {
int mx = 0;
for (int k = j; k <= n; k += g[i])
if (mx < v[k]) mx = v[k];
for (int k = j; k <= n; k += g[i])
if (mx == v[k]) is[k] = is[n + k] = 1;
}
for (int j = n * 2 - 1; j >= 1; j--) is[j] = is[j] ? is[j + 1] + 1 : 0;
for (int j = 1; j <= n; j++) as += su[is[j] >= n ? n - 1 : is[j]];
}
printf("%lld\n", as);
}
|
### Prompt
Please create a solution in Cpp to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int n, v[200050], is[200050 * 2], su[200050], g[200050];
long long as;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]), g[i] = gcd(i, n);
for (int i = 1; i <= n; i++)
if (!(n % i)) {
for (int j = 1; j <= n; j++) is[j] = is[j + n] = su[j] = 0;
for (int j = 1; j < n; j++) su[j] = (g[j] == g[i]) + su[j - 1];
for (int j = 1; j <= g[i]; j++) {
int mx = 0;
for (int k = j; k <= n; k += g[i])
if (mx < v[k]) mx = v[k];
for (int k = j; k <= n; k += g[i])
if (mx == v[k]) is[k] = is[n + k] = 1;
}
for (int j = n * 2 - 1; j >= 1; j--) is[j] = is[j] ? is[j + 1] + 1 : 0;
for (int j = 1; j <= n; j++) as += su[is[j] >= n ? n - 1 : is[j]];
}
printf("%lld\n", as);
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n[524288], mod[524288], m[524288], len[524288], cant[524288];
bool v[524288];
int gcd(int a, int b) {
if (!b)
return a;
else
return gcd(b, a % b);
}
int main() {
int N, i, j, d, LEN;
scanf("%d", &N);
for (i = 0; i < N; i++) scanf("%d", &n[i]);
for (i = 0; i < N; i++) n[N + i] = n[i];
long long RES = 0LL;
for (d = 1; d < N; d++)
if (N % d == 0) {
for (i = 0; i < N; i++) mod[i] = i % d;
for (i = 0; i < d; i++) m[i] = 0;
for (i = 0; i < N; i++) m[mod[i]] = max(m[mod[i]], n[i]);
for (i = 0; i < N; i++) {
if (n[i] >= m[mod[i]])
v[i] = true;
else
v[i] = false;
}
for (i = 0; i < N; i++) v[N + i] = v[i];
memset(cant, 0, sizeof(cant));
j = 0;
for (i = 0; i < N; i++)
if (v[i]) {
if (j <= i) j = i + 1;
while (j - i < N && v[j]) j++;
cant[j - i]++;
}
LEN = 0;
for (i = 1; i <= N; i++)
while (cant[i]--) len[LEN++] = i;
j = 0;
for (i = 1; i <= N / d; i++)
if (gcd(i, N / d) == 1) {
while (j < LEN && len[j] < d * i) j++;
RES += LEN - j;
}
}
printf("%I64d\n", RES);
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n[524288], mod[524288], m[524288], len[524288], cant[524288];
bool v[524288];
int gcd(int a, int b) {
if (!b)
return a;
else
return gcd(b, a % b);
}
int main() {
int N, i, j, d, LEN;
scanf("%d", &N);
for (i = 0; i < N; i++) scanf("%d", &n[i]);
for (i = 0; i < N; i++) n[N + i] = n[i];
long long RES = 0LL;
for (d = 1; d < N; d++)
if (N % d == 0) {
for (i = 0; i < N; i++) mod[i] = i % d;
for (i = 0; i < d; i++) m[i] = 0;
for (i = 0; i < N; i++) m[mod[i]] = max(m[mod[i]], n[i]);
for (i = 0; i < N; i++) {
if (n[i] >= m[mod[i]])
v[i] = true;
else
v[i] = false;
}
for (i = 0; i < N; i++) v[N + i] = v[i];
memset(cant, 0, sizeof(cant));
j = 0;
for (i = 0; i < N; i++)
if (v[i]) {
if (j <= i) j = i + 1;
while (j - i < N && v[j]) j++;
cant[j - i]++;
}
LEN = 0;
for (i = 1; i <= N; i++)
while (cant[i]--) len[LEN++] = i;
j = 0;
for (i = 1; i <= N / d; i++)
if (gcd(i, N / d) == 1) {
while (j < LEN && len[j] < d * i) j++;
RES += LEN - j;
}
}
printf("%I64d\n", RES);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> divisors(int n) {
vector<int> divisors;
divisors.reserve(n);
for (int i = 1; i < n; ++i) {
if (n % i == 0) {
divisors.push_back(i);
}
}
return divisors;
}
template <class T>
T mod(T arg, T mod) {
arg %= mod;
if (arg < 0) {
arg += mod;
}
return arg;
}
template <class T>
T gcd(T a, T b) {
return (a == 0) ? b : gcd(b % a, a);
}
vector<int> good_count(const vector<int>& input, int g) {
int n = input.size();
vector<int> good(g);
for (int r = 0; r < g; ++r) {
for (int j = r; j < n; j += g) {
good[r] = max(input[j], good[r]);
}
}
return good;
}
int64_t count(const vector<int>& input, int g, const vector<int>& gcda) {
int n = input.size();
vector<int> good = good_count(input, g);
int it1 = 0, it2 = 0;
vector<int> suff(n);
bool touched = false;
while (it1 < n) {
if (input[it1] == good[mod(it1, g)] &&
(input[mod(it1 - 1, n)] != good[mod(mod(it1 - 1, n), g)])) {
it2 = it1;
while (it2 - it1 + 1 <= n) {
if (input[mod(it2, n)] == good[mod(mod(it2, n), g)] &&
(input[mod(it2 + 1, n)] != good[mod(mod(it2 + 1, n), g)])) {
int k = it2 - it1 + 1;
for (int i = 1; i <= k; ++i) {
suff[i] += k - i + 1;
}
touched = true;
it1 = it2;
break;
}
++it2;
}
}
++it1;
}
if (!touched) {
for (int i = 1; i < n; ++i) {
suff[i] += n;
}
}
int64_t result = 0;
for (int i = 1; i < n; ++i) {
if (gcda[i] == g) {
result += suff[i];
}
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
int n = 0;
cin >> n;
vector<int> input(n);
for (int i = 0; i < n; ++i) {
cin >> input[i];
}
vector<int> divs = divisors(n);
int64_t counter = 0;
vector<int> gcda(n);
for (int i = 1; i < n; ++i) {
gcda[i] = gcd(i, n);
}
for (const auto& divisor : divs) {
counter += count(input, divisor, gcda);
}
cout << counter << '\n';
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> divisors(int n) {
vector<int> divisors;
divisors.reserve(n);
for (int i = 1; i < n; ++i) {
if (n % i == 0) {
divisors.push_back(i);
}
}
return divisors;
}
template <class T>
T mod(T arg, T mod) {
arg %= mod;
if (arg < 0) {
arg += mod;
}
return arg;
}
template <class T>
T gcd(T a, T b) {
return (a == 0) ? b : gcd(b % a, a);
}
vector<int> good_count(const vector<int>& input, int g) {
int n = input.size();
vector<int> good(g);
for (int r = 0; r < g; ++r) {
for (int j = r; j < n; j += g) {
good[r] = max(input[j], good[r]);
}
}
return good;
}
int64_t count(const vector<int>& input, int g, const vector<int>& gcda) {
int n = input.size();
vector<int> good = good_count(input, g);
int it1 = 0, it2 = 0;
vector<int> suff(n);
bool touched = false;
while (it1 < n) {
if (input[it1] == good[mod(it1, g)] &&
(input[mod(it1 - 1, n)] != good[mod(mod(it1 - 1, n), g)])) {
it2 = it1;
while (it2 - it1 + 1 <= n) {
if (input[mod(it2, n)] == good[mod(mod(it2, n), g)] &&
(input[mod(it2 + 1, n)] != good[mod(mod(it2 + 1, n), g)])) {
int k = it2 - it1 + 1;
for (int i = 1; i <= k; ++i) {
suff[i] += k - i + 1;
}
touched = true;
it1 = it2;
break;
}
++it2;
}
}
++it1;
}
if (!touched) {
for (int i = 1; i < n; ++i) {
suff[i] += n;
}
}
int64_t result = 0;
for (int i = 1; i < n; ++i) {
if (gcda[i] == g) {
result += suff[i];
}
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
int n = 0;
cin >> n;
vector<int> input(n);
for (int i = 0; i < n; ++i) {
cin >> input[i];
}
vector<int> divs = divisors(n);
int64_t counter = 0;
vector<int> gcda(n);
for (int i = 1; i < n; ++i) {
gcda[i] = gcd(i, n);
}
for (const auto& divisor : divs) {
counter += count(input, divisor, gcda);
}
cout << counter << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1 << 28;
const long long LINF = 1ll << 61;
inline long long getnum() {
register long long r = 0;
register bool ng = 0;
register char c;
c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') ng = 1, c = getchar();
while (c != ' ' && c != '\n') r = r * 10 + c - '0', c = getchar();
if (ng) r = -r;
return r;
}
template <class T>
inline void putnum(T x) {
if (x < 0) putchar('-'), x = -x;
register short a[20] = {}, sz = 0;
while (x > 0) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline void putsp() { putchar(' '); }
inline void putendl() { putchar('\n'); }
inline char mygetchar() {
register char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
int n, a[200111], g[200111], cnt[200111], mx[200111], r[400111];
bool f[400111];
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
int main() {
n = getnum();
for (int i = 1; i <= n; i++) a[i] = getnum();
for (int i = 1; i <= n; i++) g[i] = gcd(i, n);
long long ans = 0;
for (int l = 1; l < n; l++) {
if (n % l == 0) {
for (int i = 1; i <= n; i++) cnt[i] = (g[i] == l) + cnt[i - 1];
memset(mx, 0, sizeof(mx));
for (int i = 1; i <= n; i++) mx[i % l] = max(mx[i % l], a[i]);
for (int i = 1; i <= n; i++) f[i] = (a[i] >= mx[i % l]);
for (int i = n + 1; i <= n * 2; i++) f[i] = f[i - n];
for (int i = n * 2; i >= 1; i--) {
if (i < n * 2 && f[i + 1])
r[i] = r[i + 1];
else
r[i] = i;
}
for (int i = 1; i <= n; i++)
if (f[i]) ans += cnt[min(n, r[i] - i + 1)];
}
}
putnum(ans), putendl();
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1 << 28;
const long long LINF = 1ll << 61;
inline long long getnum() {
register long long r = 0;
register bool ng = 0;
register char c;
c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') ng = 1, c = getchar();
while (c != ' ' && c != '\n') r = r * 10 + c - '0', c = getchar();
if (ng) r = -r;
return r;
}
template <class T>
inline void putnum(T x) {
if (x < 0) putchar('-'), x = -x;
register short a[20] = {}, sz = 0;
while (x > 0) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline void putsp() { putchar(' '); }
inline void putendl() { putchar('\n'); }
inline char mygetchar() {
register char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
int n, a[200111], g[200111], cnt[200111], mx[200111], r[400111];
bool f[400111];
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
int main() {
n = getnum();
for (int i = 1; i <= n; i++) a[i] = getnum();
for (int i = 1; i <= n; i++) g[i] = gcd(i, n);
long long ans = 0;
for (int l = 1; l < n; l++) {
if (n % l == 0) {
for (int i = 1; i <= n; i++) cnt[i] = (g[i] == l) + cnt[i - 1];
memset(mx, 0, sizeof(mx));
for (int i = 1; i <= n; i++) mx[i % l] = max(mx[i % l], a[i]);
for (int i = 1; i <= n; i++) f[i] = (a[i] >= mx[i % l]);
for (int i = n + 1; i <= n * 2; i++) f[i] = f[i - n];
for (int i = n * 2; i >= 1; i--) {
if (i < n * 2 && f[i + 1])
r[i] = r[i + 1];
else
r[i] = i;
}
for (int i = 1; i <= n; i++)
if (f[i]) ans += cnt[min(n, r[i] - i + 1)];
}
}
putnum(ans), putendl();
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;
int gcd(int a, int b) {
if (!a) return b;
return gcd(b % a, a);
}
int mas[400400];
bool sum[400400];
int ost[400400];
int l[400400];
int canlen[400400];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &mas[i]);
mas[i + n] = mas[i];
}
int n2 = n * 2;
long long res = 0;
vector<int> len;
for (int gg = 1; gg < n; gg++) {
if (n % gg == 0) {
for (int i = 0; i <= n2; i++) sum[i] = false;
for (int i = 0; i <= n2; i++) ost[i] = 0;
for (int i = 0; i <= n2; i++) canlen[i] = 0;
for (int i = 0; i <= n2; i++) l[i] = 0;
int o = 0;
for (int i = 0; i < n; i++) {
ost[o] = max(ost[o], mas[i]);
o++;
if (o == gg) o = 0;
}
o = 0;
for (int i = 0; i < n; i++) {
if (mas[i] >= ost[o]) sum[i] = true, sum[i + n] = true;
o++;
if (o == gg) o = 0;
}
int curlen = 0;
int sta = 0;
len.clear();
for (int i = 0; i < n2; i++) {
if (sum[i]) {
if (curlen == 0 && i >= n) break;
if (curlen == 0) sta = i;
curlen++;
if (i + 1 == 2 * n) {
l[sta] = curlen;
break;
}
} else {
if (curlen != 0) l[sta] = curlen;
curlen = 0;
if (i >= n) break;
}
}
for (int i = 1; i < n2; i++) {
l[i] = max(l[i], l[i - 1] - 1);
}
for (int i = gg; i <= n; i += gg) {
int g = gcd(i, n);
if (g == gg) {
canlen[i] = 1;
}
}
for (int i = 1; i <= n2; i++) canlen[i] += canlen[i - 1];
for (int i = 0; i < n; i++) {
if (l[i] != 0) {
res += canlen[l[i]];
}
}
}
}
printf("%lld\n", res);
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;
int gcd(int a, int b) {
if (!a) return b;
return gcd(b % a, a);
}
int mas[400400];
bool sum[400400];
int ost[400400];
int l[400400];
int canlen[400400];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &mas[i]);
mas[i + n] = mas[i];
}
int n2 = n * 2;
long long res = 0;
vector<int> len;
for (int gg = 1; gg < n; gg++) {
if (n % gg == 0) {
for (int i = 0; i <= n2; i++) sum[i] = false;
for (int i = 0; i <= n2; i++) ost[i] = 0;
for (int i = 0; i <= n2; i++) canlen[i] = 0;
for (int i = 0; i <= n2; i++) l[i] = 0;
int o = 0;
for (int i = 0; i < n; i++) {
ost[o] = max(ost[o], mas[i]);
o++;
if (o == gg) o = 0;
}
o = 0;
for (int i = 0; i < n; i++) {
if (mas[i] >= ost[o]) sum[i] = true, sum[i + n] = true;
o++;
if (o == gg) o = 0;
}
int curlen = 0;
int sta = 0;
len.clear();
for (int i = 0; i < n2; i++) {
if (sum[i]) {
if (curlen == 0 && i >= n) break;
if (curlen == 0) sta = i;
curlen++;
if (i + 1 == 2 * n) {
l[sta] = curlen;
break;
}
} else {
if (curlen != 0) l[sta] = curlen;
curlen = 0;
if (i >= n) break;
}
}
for (int i = 1; i < n2; i++) {
l[i] = max(l[i], l[i - 1] - 1);
}
for (int i = gg; i <= n; i += gg) {
int g = gcd(i, n);
if (g == gg) {
canlen[i] = 1;
}
}
for (int i = 1; i <= n2; i++) canlen[i] += canlen[i - 1];
for (int i = 0; i < n; i++) {
if (l[i] != 0) {
res += canlen[l[i]];
}
}
}
}
printf("%lld\n", res);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
array<int, 200000> a, m, sum, g;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) g[i] = gcd(i, n);
long long ans = 0;
for (int i = 1; i < n; i++)
if (n % i == 0) {
m.fill(0);
for (int j = 0; j < n; j++) m[j % i] = max(m[j % i], a[j]);
int l = 0;
while (l < n && a[l] == m[l % i]) l++;
sum.fill(0);
if (l == n)
for (int j = 1; j < n; j++) sum[j] += n;
while (l < n) {
while (l < n && a[l] != m[l % i]) l++;
int r = l;
while (a[r % n] == m[r % i]) r++;
for (int j = l; j < r; j++) sum[j - l + 1] += r - j;
l = r;
}
for (int j = 1; j < n; j++)
if (g[j] == i) ans += sum[j];
}
cout << ans;
}
|
### Prompt
Create a solution in Cpp for the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
array<int, 200000> a, m, sum, g;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) g[i] = gcd(i, n);
long long ans = 0;
for (int i = 1; i < n; i++)
if (n % i == 0) {
m.fill(0);
for (int j = 0; j < n; j++) m[j % i] = max(m[j % i], a[j]);
int l = 0;
while (l < n && a[l] == m[l % i]) l++;
sum.fill(0);
if (l == n)
for (int j = 1; j < n; j++) sum[j] += n;
while (l < n) {
while (l < n && a[l] != m[l % i]) l++;
int r = l;
while (a[r % n] == m[r % i]) r++;
for (int j = l; j < r; j++) sum[j - l + 1] += r - j;
l = r;
}
for (int j = 1; j < n; j++)
if (g[j] == i) ans += sum[j];
}
cout << ans;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int arr[200005];
vector<int> vec[200005];
int crr[200005 << 1];
int f[200005 << 1];
int ma[200005];
int n;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", arr + i);
}
for (int i = 1; i < n; i++) {
vec[gcd(i, n)].push_back(i);
}
long long res = 0;
for (int i = 1; i < n; i++) {
if (vec[i].size() == 0) continue;
int d = gcd(i, n);
for (int j = 0; j < d; j++) {
ma[j] = 0;
}
for (int j = 0; j < n; j++) {
if (ma[j % d] < arr[j]) {
ma[j % d] = arr[j];
}
}
for (int j = 0; j < 2 * n - 1; j++) {
int val = j >= n ? arr[j - n] : arr[j];
if (val == ma[j % d]) {
crr[j] = 1;
} else {
crr[j] = 0;
}
}
f[2 * n - 1] = 0;
vector<int>::iterator it = vec[i].begin();
int cur = 0;
for (int j = 2 * n - 2; j >= 0; j--) {
if (crr[j] == 0)
f[j] = 0;
else
f[j] = f[j + 1] + 1;
if (f[j] == 0) {
cur = 0;
it = vec[i].begin();
} else {
if (it == vec[i].end()) {
} else {
if (f[j] == (*it)) {
cur++;
it++;
} else {
}
}
}
if (j < n) {
res += cur;
}
}
}
cout << res << endl;
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[200005];
vector<int> vec[200005];
int crr[200005 << 1];
int f[200005 << 1];
int ma[200005];
int n;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", arr + i);
}
for (int i = 1; i < n; i++) {
vec[gcd(i, n)].push_back(i);
}
long long res = 0;
for (int i = 1; i < n; i++) {
if (vec[i].size() == 0) continue;
int d = gcd(i, n);
for (int j = 0; j < d; j++) {
ma[j] = 0;
}
for (int j = 0; j < n; j++) {
if (ma[j % d] < arr[j]) {
ma[j % d] = arr[j];
}
}
for (int j = 0; j < 2 * n - 1; j++) {
int val = j >= n ? arr[j - n] : arr[j];
if (val == ma[j % d]) {
crr[j] = 1;
} else {
crr[j] = 0;
}
}
f[2 * n - 1] = 0;
vector<int>::iterator it = vec[i].begin();
int cur = 0;
for (int j = 2 * n - 2; j >= 0; j--) {
if (crr[j] == 0)
f[j] = 0;
else
f[j] = f[j + 1] + 1;
if (f[j] == 0) {
cur = 0;
it = vec[i].begin();
} else {
if (it == vec[i].end()) {
} else {
if (f[j] == (*it)) {
cur++;
it++;
} else {
}
}
}
if (j < n) {
res += cur;
}
}
}
cout << res << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long rdtsc() {
long long tmp;
asm("rdtsc" : "=A"(tmp));
return tmp;
}
inline int myrand() { return abs((rand() << 15) ^ rand()); }
inline int rnd(int x) { return myrand() % x; }
const int INF = (int)1.01e9;
const long double EPS = 1e-9;
void precalc() {}
const int maxn = (int)2e5 + 10;
int a[maxn];
int n;
bool read() {
if (scanf("%d", &n) < 1) {
return 0;
}
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
return 1;
}
int b[maxn * 2];
pair<int, int> st[maxn * 2];
void add(int &l, int &r, pair<int, int> toadd) {
while (r > l && st[r - 1].first >= toadd.first) {
--r;
}
st[r++] = toadd;
}
void kill(int &l, int &r, int id) {
if (l < r && st[l].second == id) {
++l;
}
}
int get(int &l, int &r) {
assert(l < r);
return st[l].first;
}
int prec[maxn];
int gcd(int a, int b) {
for (; b; a %= b, swap(a, b))
;
return a;
}
void solve() {
long long res = 0;
for (int d = 1; d < n; ++d) {
if (n % d) {
continue;
}
prec[0] = 0;
for (int i = 1; i <= n / d; ++i) {
prec[i] = prec[i - 1] + (gcd(i, n / d) == 1);
}
int mxcnt = n / d - 1;
for (int r = 0; r < d; ++r) {
int val = 0;
for (int i = r; i < n; i += d) {
val = max(val, a[i]);
}
int cnt = 0;
for (int iter = 0; iter < 2; ++iter) {
for (int i = n + r - d; i >= 0; i -= d) {
if (a[i] == val) {
cnt = min(mxcnt, cnt + 1);
} else {
cnt = 0;
}
if (iter) {
b[i] = cnt;
}
}
}
}
int l = 0, r = 0;
for (int i = 0; i < d; ++i) {
add(l, r, make_pair(b[i], i));
}
for (int i = 0; i < n; ++i) {
b[i + n] = b[i];
res += prec[get(l, r)];
kill(l, r, i);
add(l, r, make_pair(b[i + d], i + d));
}
}
printf(
"%lld"
"\n",
res);
}
int main() {
srand(rdtsc());
precalc();
while (1) {
if (!read()) {
break;
}
solve();
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long rdtsc() {
long long tmp;
asm("rdtsc" : "=A"(tmp));
return tmp;
}
inline int myrand() { return abs((rand() << 15) ^ rand()); }
inline int rnd(int x) { return myrand() % x; }
const int INF = (int)1.01e9;
const long double EPS = 1e-9;
void precalc() {}
const int maxn = (int)2e5 + 10;
int a[maxn];
int n;
bool read() {
if (scanf("%d", &n) < 1) {
return 0;
}
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
return 1;
}
int b[maxn * 2];
pair<int, int> st[maxn * 2];
void add(int &l, int &r, pair<int, int> toadd) {
while (r > l && st[r - 1].first >= toadd.first) {
--r;
}
st[r++] = toadd;
}
void kill(int &l, int &r, int id) {
if (l < r && st[l].second == id) {
++l;
}
}
int get(int &l, int &r) {
assert(l < r);
return st[l].first;
}
int prec[maxn];
int gcd(int a, int b) {
for (; b; a %= b, swap(a, b))
;
return a;
}
void solve() {
long long res = 0;
for (int d = 1; d < n; ++d) {
if (n % d) {
continue;
}
prec[0] = 0;
for (int i = 1; i <= n / d; ++i) {
prec[i] = prec[i - 1] + (gcd(i, n / d) == 1);
}
int mxcnt = n / d - 1;
for (int r = 0; r < d; ++r) {
int val = 0;
for (int i = r; i < n; i += d) {
val = max(val, a[i]);
}
int cnt = 0;
for (int iter = 0; iter < 2; ++iter) {
for (int i = n + r - d; i >= 0; i -= d) {
if (a[i] == val) {
cnt = min(mxcnt, cnt + 1);
} else {
cnt = 0;
}
if (iter) {
b[i] = cnt;
}
}
}
}
int l = 0, r = 0;
for (int i = 0; i < d; ++i) {
add(l, r, make_pair(b[i], i));
}
for (int i = 0; i < n; ++i) {
b[i + n] = b[i];
res += prec[get(l, r)];
kill(l, r, i);
add(l, r, make_pair(b[i + d], i + d));
}
}
printf(
"%lld"
"\n",
res);
}
int main() {
srand(rdtsc());
precalc();
while (1) {
if (!read()) {
break;
}
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
const int maxn = 400010;
using namespace std;
int n, f[maxn], a[maxn], g[maxn], cnt[maxn];
long long ans;
bool bo[maxn];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]), a[i + n] = a[i];
for (int d = 1; d < n; d++) {
if (n % d == 0) {
memset(bo, 0, sizeof(bo));
for (int k = 0; k < d; k++) {
g[k] = 0;
for (int i = k; i < (n << 1); i += d) g[k] = max(g[k], a[i]);
for (int i = k; i < (n << 1); i += d)
if (a[i] == g[k]) bo[i] = 1;
}
f[0] = bo[0];
for (int i = 1; i < (n << 1); i++) {
if (bo[i])
f[i] = f[i - 1] + 1;
else
f[i] = 0;
f[i] = min(f[i], n - 1);
}
cnt[0] = 0;
for (int i = 1; i < (n / d); i++)
cnt[i] = cnt[i - 1] + (gcd(i, n / d) == 1);
for (int i = n; i < (n << 1); i++) ans += cnt[f[i] / d];
}
}
printf("%I64d\n", ans);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
const int maxn = 400010;
using namespace std;
int n, f[maxn], a[maxn], g[maxn], cnt[maxn];
long long ans;
bool bo[maxn];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]), a[i + n] = a[i];
for (int d = 1; d < n; d++) {
if (n % d == 0) {
memset(bo, 0, sizeof(bo));
for (int k = 0; k < d; k++) {
g[k] = 0;
for (int i = k; i < (n << 1); i += d) g[k] = max(g[k], a[i]);
for (int i = k; i < (n << 1); i += d)
if (a[i] == g[k]) bo[i] = 1;
}
f[0] = bo[0];
for (int i = 1; i < (n << 1); i++) {
if (bo[i])
f[i] = f[i - 1] + 1;
else
f[i] = 0;
f[i] = min(f[i], n - 1);
}
cnt[0] = 0;
for (int i = 1; i < (n / d); i++)
cnt[i] = cnt[i - 1] + (gcd(i, n / d) == 1);
for (int i = n; i < (n << 1); i++) ans += cnt[f[i] / d];
}
}
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int N;
int A[200005];
vector<int> Max[200005], Aux[200005];
vector<int> Aux2[200005];
int Div[200005], cnt;
void Read() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
}
void precalcDiv() {
for (int i = 1; i <= N; i++) {
if (N % i != 0) continue;
Div[++cnt] = i;
int d = 0;
Max[i].resize(i);
for (int j = 0; j < N; j++) {
Max[i][d] = max(Max[i][d], A[j]);
++d;
if (d == i) d = 0;
}
}
}
void Solve(int d) {
Aux[d].resize(N);
int x = 0;
for (int i = 0; i < N; i++) {
int m = Max[d][x];
++x;
if (x == d) x = 0;
if (m == A[i]) {
if (i > 0)
Aux[d][i] = Aux[d][i - 1] + 1;
else
Aux[d][i] = 1;
} else
Aux[d][i] = 0;
}
x = 0;
if (Aux[d][Aux[d].size() - 1] != N) {
int p = Aux[d][Aux[d].size() - 1];
for (int i = 0; i < N; i++) {
int m = Max[d][x];
++x;
if (x == d) x = 0;
if (m == A[i]) {
if (i > 0)
Aux[d][i] = Aux[d][i - 1] + 1;
else
Aux[d][i] = 1 + p;
} else
break;
}
} else {
for (int i = 0; i < N; i++) Aux[d][i] = N;
}
Aux2[d].resize(N + 1);
for (int i = 0; i < Aux[d].size(); i++) Aux2[d][Aux[d][i]]++;
for (int i = Aux2[d].size() - 2; i >= 0; i--) Aux2[d][i] += Aux2[d][i + 1];
}
int getAns(int pos, int s) { return Aux2[pos][s]; }
int gcd(int a, int b) {
int r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
void findSol() {
for (int i = 1; i <= cnt; i++) {
Solve(Div[i]);
}
long long res = 0;
for (int s = N - 1; s > 0; s--) {
res += getAns(gcd(s, N), s);
}
printf("%I64d\n", res);
}
int main() {
Read();
precalcDiv();
findSol();
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N;
int A[200005];
vector<int> Max[200005], Aux[200005];
vector<int> Aux2[200005];
int Div[200005], cnt;
void Read() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
}
void precalcDiv() {
for (int i = 1; i <= N; i++) {
if (N % i != 0) continue;
Div[++cnt] = i;
int d = 0;
Max[i].resize(i);
for (int j = 0; j < N; j++) {
Max[i][d] = max(Max[i][d], A[j]);
++d;
if (d == i) d = 0;
}
}
}
void Solve(int d) {
Aux[d].resize(N);
int x = 0;
for (int i = 0; i < N; i++) {
int m = Max[d][x];
++x;
if (x == d) x = 0;
if (m == A[i]) {
if (i > 0)
Aux[d][i] = Aux[d][i - 1] + 1;
else
Aux[d][i] = 1;
} else
Aux[d][i] = 0;
}
x = 0;
if (Aux[d][Aux[d].size() - 1] != N) {
int p = Aux[d][Aux[d].size() - 1];
for (int i = 0; i < N; i++) {
int m = Max[d][x];
++x;
if (x == d) x = 0;
if (m == A[i]) {
if (i > 0)
Aux[d][i] = Aux[d][i - 1] + 1;
else
Aux[d][i] = 1 + p;
} else
break;
}
} else {
for (int i = 0; i < N; i++) Aux[d][i] = N;
}
Aux2[d].resize(N + 1);
for (int i = 0; i < Aux[d].size(); i++) Aux2[d][Aux[d][i]]++;
for (int i = Aux2[d].size() - 2; i >= 0; i--) Aux2[d][i] += Aux2[d][i + 1];
}
int getAns(int pos, int s) { return Aux2[pos][s]; }
int gcd(int a, int b) {
int r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
void findSol() {
for (int i = 1; i <= cnt; i++) {
Solve(Div[i]);
}
long long res = 0;
for (int s = N - 1; s > 0; s--) {
res += getAns(gcd(s, N), s);
}
printf("%I64d\n", res);
}
int main() {
Read();
precalcDiv();
findSol();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int maxn = 200010;
int n;
int a[maxn];
bool state[maxn];
void solve() {
scanf("%d", &n);
for (int i = (0); i < (n); i++) scanf("%d", a + i);
long long ans = 0;
for (int d = (1); d < (n); d++)
if (n % d == 0) {
memset(state, 0, sizeof(state));
int cnt = 0, t;
for (int r = (0); r < (d); r++) {
int dmax = 0;
for (int i = r; i < n; i += d) dmax = max(dmax, a[i]);
for (int i = r; i < n; i += d) {
state[i] = a[i] == dmax;
cnt += state[i];
if (!state[i]) t = i;
}
}
vector<int> v;
if (cnt == n)
v.push_back(n);
else {
cnt = 0;
for (int i = (0); i < (n); i++) {
if (!state[(i + t) % n]) {
if (cnt) v.push_back(cnt);
if (i >= n) break;
cnt = 0;
} else
cnt++;
if (i == n - 1 && cnt) v.push_back(cnt);
}
}
for (int i = (0); i < (int((v).size())); i++) {
int k = v[i];
if (k < n) {
for (int c = 1; c * d <= k; c++)
if (gcd(c, n / d) == 1) ans += k - c * d + 1;
} else {
for (int c = 1; c * d < n; c++)
if (gcd(c, n / d) == 1) ans += n;
}
}
}
printf("%I64d", ans);
}
int main() {
solve();
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int maxn = 200010;
int n;
int a[maxn];
bool state[maxn];
void solve() {
scanf("%d", &n);
for (int i = (0); i < (n); i++) scanf("%d", a + i);
long long ans = 0;
for (int d = (1); d < (n); d++)
if (n % d == 0) {
memset(state, 0, sizeof(state));
int cnt = 0, t;
for (int r = (0); r < (d); r++) {
int dmax = 0;
for (int i = r; i < n; i += d) dmax = max(dmax, a[i]);
for (int i = r; i < n; i += d) {
state[i] = a[i] == dmax;
cnt += state[i];
if (!state[i]) t = i;
}
}
vector<int> v;
if (cnt == n)
v.push_back(n);
else {
cnt = 0;
for (int i = (0); i < (n); i++) {
if (!state[(i + t) % n]) {
if (cnt) v.push_back(cnt);
if (i >= n) break;
cnt = 0;
} else
cnt++;
if (i == n - 1 && cnt) v.push_back(cnt);
}
}
for (int i = (0); i < (int((v).size())); i++) {
int k = v[i];
if (k < n) {
for (int c = 1; c * d <= k; c++)
if (gcd(c, n / d) == 1) ans += k - c * d + 1;
} else {
for (int c = 1; c * d < n; c++)
if (gcd(c, n / d) == 1) ans += n;
}
}
}
printf("%I64d", ans);
}
int main() {
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool rel_prime(int a, int b) {
if (b == 0) return a == 1;
return rel_prime(b, a % b);
}
int main() {
int n;
cin >> n;
vector<int> nums(n, 0);
int abs_max = 0;
for (int i = 0; i < n; i++) {
cin >> nums[i];
if (nums[i] > abs_max) abs_max = nums[i];
}
long long wins = 0;
for (int d = 1; d < n; d++) {
if (n % d == 0) {
vector<bool> acceptable(n, 0);
for (int i = 0; i < d; i++) {
int max = 0;
for (int k = 0; k < n / d; k++) {
if (nums[k * d + i] > max) max = nums[k * d + i];
}
for (int k = 0; k < n / d; k++) {
if (nums[k * d + i] == max) acceptable[k * d + i] = 1;
}
}
vector<int> streaks(1, 0);
for (int i = 0; i < n; i++) {
if (acceptable[i])
streaks.back()++;
else
streaks.push_back(0);
}
if (streaks.size() == 1) {
for (int i = 1; i <= n / d; i++) {
if (rel_prime(n / d, i)) wins += n;
}
}
streaks[0] += streaks.back();
streaks.pop_back();
for (int x : streaks) {
for (int i = 1; i <= x / d; i++) {
if (rel_prime(n / d, i)) {
wins += x + 1 - d * i;
}
}
}
}
}
cout << wins << endl;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool rel_prime(int a, int b) {
if (b == 0) return a == 1;
return rel_prime(b, a % b);
}
int main() {
int n;
cin >> n;
vector<int> nums(n, 0);
int abs_max = 0;
for (int i = 0; i < n; i++) {
cin >> nums[i];
if (nums[i] > abs_max) abs_max = nums[i];
}
long long wins = 0;
for (int d = 1; d < n; d++) {
if (n % d == 0) {
vector<bool> acceptable(n, 0);
for (int i = 0; i < d; i++) {
int max = 0;
for (int k = 0; k < n / d; k++) {
if (nums[k * d + i] > max) max = nums[k * d + i];
}
for (int k = 0; k < n / d; k++) {
if (nums[k * d + i] == max) acceptable[k * d + i] = 1;
}
}
vector<int> streaks(1, 0);
for (int i = 0; i < n; i++) {
if (acceptable[i])
streaks.back()++;
else
streaks.push_back(0);
}
if (streaks.size() == 1) {
for (int i = 1; i <= n / d; i++) {
if (rel_prime(n / d, i)) wins += n;
}
}
streaks[0] += streaks.back();
streaks.pop_back();
for (int x : streaks) {
for (int i = 1; i <= x / d; i++) {
if (rel_prime(n / d, i)) {
wins += x + 1 - d * i;
}
}
}
}
}
cout << wins << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int a[N], b[N], GCD[N], f[N], va[N];
inline int re() {
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar()) p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return p ? -x : x;
}
int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
inline int maxn(int x, int y) { return x > y ? x : y; }
inline int minn(int x, int y) { return x < y ? x : y; }
int main() {
int i, j, n, m, d;
long long s = 0;
n = re();
m = n << 1;
for (i = 1; i <= n; i++) a[i - 1] = a[i - 1 + n] = re(), GCD[i] = gcd(i, n);
for (d = 1; d < n; d++)
if (!(n % d)) {
for (i = 0; i < d; i++) {
b[i] = -1e9;
for (j = i; j < m; j += d) b[i] = maxn(b[i], a[j]);
for (j = i; j < m; j += d) f[j] = (a[j] == b[i]);
}
for (i = 1; i < m; i++)
if (f[i]) f[i] += f[i - 1];
for (i = 1; i < n; i++) va[i] = va[i - 1] + (GCD[i] == d);
for (i = n; i < m; i++) s += va[minn(n - 1, f[i])];
}
printf("%I64d", s);
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int a[N], b[N], GCD[N], f[N], va[N];
inline int re() {
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar()) p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return p ? -x : x;
}
int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
inline int maxn(int x, int y) { return x > y ? x : y; }
inline int minn(int x, int y) { return x < y ? x : y; }
int main() {
int i, j, n, m, d;
long long s = 0;
n = re();
m = n << 1;
for (i = 1; i <= n; i++) a[i - 1] = a[i - 1 + n] = re(), GCD[i] = gcd(i, n);
for (d = 1; d < n; d++)
if (!(n % d)) {
for (i = 0; i < d; i++) {
b[i] = -1e9;
for (j = i; j < m; j += d) b[i] = maxn(b[i], a[j]);
for (j = i; j < m; j += d) f[j] = (a[j] == b[i]);
}
for (i = 1; i < m; i++)
if (f[i]) f[i] += f[i - 1];
for (i = 1; i < n; i++) va[i] = va[i - 1] + (GCD[i] == d);
for (i = n; i < m; i++) s += va[minn(n - 1, f[i])];
}
printf("%I64d", s);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[201000], mark[201000], G[201000];
long long ans;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
scanf("%d", &n);
for (int i = 0; i <= n; i++) G[i] = gcd(i, n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
for (int t = 1; t < n; t++) {
if (n % t) continue;
for (int i = 0; i < n; i++) mark[i] = 0;
for (int i = 0; i < t; i++) {
int wp = 0;
for (int j = i; j < n; j += t) wp = max(wp, A[j]);
for (int j = i; j < n; j += t) mark[j] = (A[j] == wp);
}
int now = 0, wp = 0;
for (int i = n * 2 - 1; i >= 0; i--) {
if (mark[i % n]) {
now++;
if (now < n && now >= t && (G[now] == t)) wp++;
if (i < n) ans += (long long)wp;
} else
wp = now = 0;
}
}
cout << ans << endl;
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 β€ l < n, 1 β€ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.
A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β infinite array a, bottom β its periodic subarray (l, s)):
<image>
Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.
Input
The first line contains number n (1 β€ n β€ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 β€ ai β€ 106), separated by a space.
Output
Print a single integer β the sought number of pairs.
Examples
Input
4
7 1 2 3
Output
2
Input
2
2 1
Output
1
Input
3
1 1 1
Output
6
Note
In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as a0 β₯ a0, a0 β₯ a1, a0 β₯ a2, a0 β₯ a3, a0 β₯ a0, ...
Subarray (3, 2) is superior a3 β₯ a3, a0 β₯ a0, a3 β₯ a1, a0 β₯ a2, a3 β₯ a3, ...
In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[201000], mark[201000], G[201000];
long long ans;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
scanf("%d", &n);
for (int i = 0; i <= n; i++) G[i] = gcd(i, n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
for (int t = 1; t < n; t++) {
if (n % t) continue;
for (int i = 0; i < n; i++) mark[i] = 0;
for (int i = 0; i < t; i++) {
int wp = 0;
for (int j = i; j < n; j += t) wp = max(wp, A[j]);
for (int j = i; j < n; j += t) mark[j] = (A[j] == wp);
}
int now = 0, wp = 0;
for (int i = n * 2 - 1; i >= 0; i--) {
if (mark[i % n]) {
now++;
if (now < n && now >= t && (G[now] == t)) wp++;
if (i < n) ans += (long long)wp;
} else
wp = now = 0;
}
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
const int N = 1000 + 7;
int n;
double p[N][N], f[N], g[N], h[N][N];
int vis[N], top[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%lf", p[i] + j), p[i][j] /= 100;
vis[top[1] = n] = 1;
for (int j = 1; j <= n; ++j) h[j][1] = 1 - p[j][n];
for (int i = 2; i <= n; ++i) {
int k = 0;
for (int j = 1; j <= n; ++j) {
if (vis[j]) continue;
g[j] += f[top[i - 1]] * h[j][i - 2] * p[j][top[i - 1]];
f[j] = (g[j] + 1) / (1 - h[j][i - 1]);
if (!k || f[j] < f[k]) k = j;
}
vis[top[i] = k] = 1;
for (int j = 1; j <= n; ++j) h[j][i] = h[j][i - 1] * (1 - p[j][k]);
}
printf("%.15lf", f[1]);
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 1000 + 7;
int n;
double p[N][N], f[N], g[N], h[N][N];
int vis[N], top[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%lf", p[i] + j), p[i][j] /= 100;
vis[top[1] = n] = 1;
for (int j = 1; j <= n; ++j) h[j][1] = 1 - p[j][n];
for (int i = 2; i <= n; ++i) {
int k = 0;
for (int j = 1; j <= n; ++j) {
if (vis[j]) continue;
g[j] += f[top[i - 1]] * h[j][i - 2] * p[j][top[i - 1]];
f[j] = (g[j] + 1) / (1 - h[j][i - 1]);
if (!k || f[j] < f[k]) k = j;
}
vis[top[i] = k] = 1;
for (int j = 1; j <= n; ++j) h[j][i] = h[j][i - 1] * (1 - p[j][k]);
}
printf("%.15lf", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int N;
double P[MAXN][MAXN];
double ans[MAXN];
double noout[MAXN];
bool vis[MAXN];
int main() {
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%lf", &P[i][j]);
P[i][j] /= 100;
}
}
for (int i = 0; i < N; i++) {
ans[i] = INFINITY;
noout[i] = 1;
}
ans[N - 1] = 0;
for (int i = 0; i < N; i++) {
int opt = 0;
for (int j = 1; j < N; j++) {
if (!vis[j] && ans[j] < ans[opt]) {
opt = j;
}
}
vis[opt] = true;
for (int j = 0; j < N; j++) {
if (!vis[j] && P[j][opt]) {
if (ans[j] == INFINITY) {
ans[j] = 1;
} else {
ans[j] *= (1 - noout[j]);
}
ans[j] += noout[j] * P[j][opt] * ans[opt];
noout[j] *= (1 - P[j][opt]);
ans[j] /= (1 - noout[j]);
}
}
}
printf("%.10lf\n", ans[0]);
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int N;
double P[MAXN][MAXN];
double ans[MAXN];
double noout[MAXN];
bool vis[MAXN];
int main() {
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%lf", &P[i][j]);
P[i][j] /= 100;
}
}
for (int i = 0; i < N; i++) {
ans[i] = INFINITY;
noout[i] = 1;
}
ans[N - 1] = 0;
for (int i = 0; i < N; i++) {
int opt = 0;
for (int j = 1; j < N; j++) {
if (!vis[j] && ans[j] < ans[opt]) {
opt = j;
}
}
vis[opt] = true;
for (int j = 0; j < N; j++) {
if (!vis[j] && P[j][opt]) {
if (ans[j] == INFINITY) {
ans[j] = 1;
} else {
ans[j] *= (1 - noout[j]);
}
ans[j] += noout[j] * P[j][opt] * ans[opt];
noout[j] *= (1 - P[j][opt]);
ans[j] /= (1 - noout[j]);
}
}
}
printf("%.10lf\n", ans[0]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
bool out[maxn];
double p[maxn][maxn], d[maxn], cur_c[maxn], cur_p[maxn];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1, x; j <= n; j++) scanf("%d", &x), p[i][j] = x / 100.0;
for (int i = 1; i <= n; i++) d[i] = 1e20, cur_c[i] = 0, cur_p[i] = 1;
d[n] = 0;
for (int i = 1; i <= n; i++) {
int u = -1;
for (int j = 1; j <= n; j++)
if (!out[j] && (u == -1 || d[j] < d[u])) u = j;
out[u] = 1;
for (int j = 1; j <= n; j++)
if (!out[j] && p[j][u] > 0) {
double pr = cur_p[j] * p[j][u], np = cur_p[j] * (1 - p[j][u]);
double nd = (cur_c[j] + pr * (d[u] + 1) + np) / (1 - np);
if (nd < d[j]) {
d[j] = nd;
cur_c[j] += pr * (d[u] + 1), cur_p[j] *= 1 - p[j][u];
}
}
}
printf("%.10f\n", d[1]);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
bool out[maxn];
double p[maxn][maxn], d[maxn], cur_c[maxn], cur_p[maxn];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1, x; j <= n; j++) scanf("%d", &x), p[i][j] = x / 100.0;
for (int i = 1; i <= n; i++) d[i] = 1e20, cur_c[i] = 0, cur_p[i] = 1;
d[n] = 0;
for (int i = 1; i <= n; i++) {
int u = -1;
for (int j = 1; j <= n; j++)
if (!out[j] && (u == -1 || d[j] < d[u])) u = j;
out[u] = 1;
for (int j = 1; j <= n; j++)
if (!out[j] && p[j][u] > 0) {
double pr = cur_p[j] * p[j][u], np = cur_p[j] * (1 - p[j][u]);
double nd = (cur_c[j] + pr * (d[u] + 1) + np) / (1 - np);
if (nd < d[j]) {
d[j] = nd;
cur_c[j] += pr * (d[u] + 1), cur_p[j] *= 1 - p[j][u];
}
}
}
printf("%.10f\n", d[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int x = 0;
while (c < '0' || c > '9') {
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + (c ^ 48);
c = getchar();
}
return x;
}
int n;
double c[1001][1001];
double p[1001];
double sum[1001];
double E[1001];
bool vis[1001];
int main() {
n = read();
if (n == 1) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
c[i][j] = read() / 100.0;
}
}
for (int i = 1; i < n; i++) {
sum[i] = c[i][n];
p[i] = 1 - c[i][n];
E[i] = (p[i] + sum[i]) / (1 - p[i]);
}
for (int i = 1; i < n; i++) {
double mins = 1000000000;
int mini;
for (int j = 1; j < n; j++) {
if ((!vis[j]) && mins > E[j]) {
mins = E[j];
mini = j;
}
}
if (mini == 1) {
break;
}
vis[mini] = 1;
for (int j = 1; j < n; j++) {
sum[j] += p[j] * c[j][mini] * (E[mini] + 1);
p[j] *= (1 - c[j][mini]);
E[j] = (p[j] + sum[j]) / (1 - p[j]);
}
}
printf("%.15lf\n", E[1]);
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int x = 0;
while (c < '0' || c > '9') {
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + (c ^ 48);
c = getchar();
}
return x;
}
int n;
double c[1001][1001];
double p[1001];
double sum[1001];
double E[1001];
bool vis[1001];
int main() {
n = read();
if (n == 1) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
c[i][j] = read() / 100.0;
}
}
for (int i = 1; i < n; i++) {
sum[i] = c[i][n];
p[i] = 1 - c[i][n];
E[i] = (p[i] + sum[i]) / (1 - p[i]);
}
for (int i = 1; i < n; i++) {
double mins = 1000000000;
int mini;
for (int j = 1; j < n; j++) {
if ((!vis[j]) && mins > E[j]) {
mins = E[j];
mini = j;
}
}
if (mini == 1) {
break;
}
vis[mini] = 1;
for (int j = 1; j < n; j++) {
sum[j] += p[j] * c[j][mini] * (E[mini] + 1);
p[j] *= (1 - c[j][mini]);
E[j] = (p[j] + sum[j]) / (1 - p[j]);
}
}
printf("%.15lf\n", E[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int p[1005][1005];
double rem[1005];
double ans[1005];
double calcAns(int ind) {
return (ans[ind] + rem[ind]) / (1 - rem[ind] + 1e-15);
}
int main() {
ios::sync_with_stdio(false);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
ans[n - 1] = 0;
vector<pair<double, int> > bad, good;
for (int i = 0; i < n - 1; i++) bad.push_back(make_pair(1.0, i));
good.push_back(make_pair(0.0, n - 1));
while (!bad.empty()) {
double best = 1e9;
int choice = -1;
for (int i = 0; i < int(bad.size()); i++) {
int indI = bad[i].second;
int indJ = good.back().second;
ans[indI] +=
bad[i].first * (p[indI][indJ] / 100.0) * (1 + good.back().first);
bad[i].first *= (1.0 - (p[indI][indJ] / 100.0));
rem[indI] = bad[i].first;
double res = calcAns(indI);
if (res < best) best = res, choice = i;
}
int ind = bad[choice].second;
ans[ind] = best;
good.push_back(make_pair(ans[ind], ind));
bad.erase(bad.begin() + choice);
if (ind == 0) break;
}
printf("%.9lf\n", ans[0]);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int p[1005][1005];
double rem[1005];
double ans[1005];
double calcAns(int ind) {
return (ans[ind] + rem[ind]) / (1 - rem[ind] + 1e-15);
}
int main() {
ios::sync_with_stdio(false);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
ans[n - 1] = 0;
vector<pair<double, int> > bad, good;
for (int i = 0; i < n - 1; i++) bad.push_back(make_pair(1.0, i));
good.push_back(make_pair(0.0, n - 1));
while (!bad.empty()) {
double best = 1e9;
int choice = -1;
for (int i = 0; i < int(bad.size()); i++) {
int indI = bad[i].second;
int indJ = good.back().second;
ans[indI] +=
bad[i].first * (p[indI][indJ] / 100.0) * (1 + good.back().first);
bad[i].first *= (1.0 - (p[indI][indJ] / 100.0));
rem[indI] = bad[i].first;
double res = calcAns(indI);
if (res < best) best = res, choice = i;
}
int ind = bad[choice].second;
ans[ind] = best;
good.push_back(make_pair(ans[ind], ind));
bad.erase(bad.begin() + choice);
if (ind == 0) break;
}
printf("%.9lf\n", ans[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
double g[N][N];
bool used[N];
double e[N], un[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1, t; j <= n; j++) {
scanf("%d", &t);
g[i][j] = 1.0 * t / 100;
}
if (n == 1) return 0 & puts("0");
memset(used, false, sizeof used);
used[n] = true;
for (int i = 1; i < n; i++) e[i] = 1, un[i] = 1 - g[i][n];
for (int i = 1; i <= n; i++) {
double minn = 1e18;
int x = -1;
for (int j = 1; j <= n; j++)
if (!used[j] && e[j] / (1 - un[j]) < minn)
minn = e[j] / (1 - un[j]), x = j;
if (x == -1) break;
used[x] = true;
for (int j = 1; j <= n; j++)
e[j] += e[x] / (1 - un[x]) * g[j][x] * un[j], un[j] *= 1 - g[j][x];
}
printf("%.10lf", e[1] / (1 - un[1]));
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
double g[N][N];
bool used[N];
double e[N], un[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1, t; j <= n; j++) {
scanf("%d", &t);
g[i][j] = 1.0 * t / 100;
}
if (n == 1) return 0 & puts("0");
memset(used, false, sizeof used);
used[n] = true;
for (int i = 1; i < n; i++) e[i] = 1, un[i] = 1 - g[i][n];
for (int i = 1; i <= n; i++) {
double minn = 1e18;
int x = -1;
for (int j = 1; j <= n; j++)
if (!used[j] && e[j] / (1 - un[j]) < minn)
minn = e[j] / (1 - un[j]), x = j;
if (x == -1) break;
used[x] = true;
for (int j = 1; j <= n; j++)
e[j] += e[x] / (1 - un[x]) * g[j][x] * un[j], un[j] *= 1 - g[j][x];
}
printf("%.10lf", e[1] / (1 - un[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
const int maxn = 1005;
const double INF = 2e9;
using namespace std;
double p[maxn][maxn], c[maxn], dp[maxn];
bool mark[maxn];
int main() {
int n, x;
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", &x);
p[i][j] = 0.01 * x;
}
fill(dp, dp + n, INF);
dp[n - 1] = 0;
fill(c, c + n, 1);
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
pq;
pq.push({dp[n - 1], n - 1});
while (!pq.empty()) {
int x = pq.top().second;
pq.pop();
if (mark[x]) continue;
mark[x] = true;
for (int i = 0; i < n; i++) {
if (mark[i] || p[i][x] == 0) continue;
if (dp[i] == INF) {
dp[i] = (p[i][x] * (dp[x] + 1) + (1 - p[i][x])) / (p[i][x]);
c[i] = (1 - p[i][x]);
} else {
dp[i] = (dp[i] * (1 - c[i]) - c[i] + c[i] * p[i][x] * (dp[x] + 1));
c[i] = c[i] * (1 - p[i][x]);
dp[i] += c[i];
dp[i] /= (1 - c[i]);
}
pq.push({dp[i], i});
}
}
if (dp[0] == INF)
cout << -1 << '\n';
else
cout << setprecision(10) << fixed << dp[0] << '\n';
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int maxn = 1005;
const double INF = 2e9;
using namespace std;
double p[maxn][maxn], c[maxn], dp[maxn];
bool mark[maxn];
int main() {
int n, x;
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", &x);
p[i][j] = 0.01 * x;
}
fill(dp, dp + n, INF);
dp[n - 1] = 0;
fill(c, c + n, 1);
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
pq;
pq.push({dp[n - 1], n - 1});
while (!pq.empty()) {
int x = pq.top().second;
pq.pop();
if (mark[x]) continue;
mark[x] = true;
for (int i = 0; i < n; i++) {
if (mark[i] || p[i][x] == 0) continue;
if (dp[i] == INF) {
dp[i] = (p[i][x] * (dp[x] + 1) + (1 - p[i][x])) / (p[i][x]);
c[i] = (1 - p[i][x]);
} else {
dp[i] = (dp[i] * (1 - c[i]) - c[i] + c[i] * p[i][x] * (dp[x] + 1));
c[i] = c[i] * (1 - p[i][x]);
dp[i] += c[i];
dp[i] /= (1 - c[i]);
}
pq.push({dp[i], i});
}
}
if (dp[0] == INF)
cout << -1 << '\n';
else
cout << setprecision(10) << fixed << dp[0] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1145;
double g[N][N], pr[N], e[N], mn;
int n, v[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &g[i][j]), g[i][j] /= 100;
if (n == 1) {
puts("0");
exit(0);
}
v[n] = 1;
for (int i = 1; i < n; i++) {
e[i] = 1;
pr[i] = 1 - g[i][n];
}
for (int i = 1; i <= n; i++) {
mn = 1e18;
int k = 0;
for (int j = 1; j < n; j++)
if (!v[j] && e[j] / (1 - pr[j]) < mn) mn = e[j] / (1 - pr[j]), k = j;
if (k == 1) {
printf("%lf", mn);
return 0;
}
v[k] = 1;
for (int j = 1; j < n; j++)
e[j] += mn * g[j][k] * pr[j], pr[j] *= (1 - g[j][k]);
}
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1145;
double g[N][N], pr[N], e[N], mn;
int n, v[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &g[i][j]), g[i][j] /= 100;
if (n == 1) {
puts("0");
exit(0);
}
v[n] = 1;
for (int i = 1; i < n; i++) {
e[i] = 1;
pr[i] = 1 - g[i][n];
}
for (int i = 1; i <= n; i++) {
mn = 1e18;
int k = 0;
for (int j = 1; j < n; j++)
if (!v[j] && e[j] / (1 - pr[j]) < mn) mn = e[j] / (1 - pr[j]), k = j;
if (k == 1) {
printf("%lf", mn);
return 0;
}
v[k] = 1;
for (int j = 1; j < n; j++)
e[j] += mn * g[j][k] * pr[j], pr[j] *= (1 - g[j][k]);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double INF = 4e18;
const int N = 3100;
int n;
double p[N][N];
double prob[N];
bool used[N];
double ans[N], sum[N];
int main() {
srand(time(0));
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
p[i][j] = 1.0 * x / 100;
}
}
for (int i = 1; i <= n; i++) ans[i] = INF;
for (int i = 1; i <= n; i++) sum[i] = 1;
ans[n] = 0;
for (int i = 1; i <= n; i++) prob[i] = 1;
while (true) {
double e = INF;
int v = -1;
for (int i = 1; i <= n; i++) {
if (!used[i] && ans[i] + 1e-9 < e) {
e = ans[i];
v = i;
}
}
if (v == -1) break;
used[v] = true;
for (int i = 1; i <= n; i++) {
if (used[i]) continue;
sum[i] += prob[i] * p[i][v] * e;
prob[i] *= 1 - p[i][v];
if (abs(1 - prob[i]) > 1e-9) {
ans[i] = sum[i] / (1 - prob[i]);
}
}
}
cout << fixed << setprecision(9) << ans[1];
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double INF = 4e18;
const int N = 3100;
int n;
double p[N][N];
double prob[N];
bool used[N];
double ans[N], sum[N];
int main() {
srand(time(0));
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
p[i][j] = 1.0 * x / 100;
}
}
for (int i = 1; i <= n; i++) ans[i] = INF;
for (int i = 1; i <= n; i++) sum[i] = 1;
ans[n] = 0;
for (int i = 1; i <= n; i++) prob[i] = 1;
while (true) {
double e = INF;
int v = -1;
for (int i = 1; i <= n; i++) {
if (!used[i] && ans[i] + 1e-9 < e) {
e = ans[i];
v = i;
}
}
if (v == -1) break;
used[v] = true;
for (int i = 1; i <= n; i++) {
if (used[i]) continue;
sum[i] += prob[i] * p[i][v] * e;
prob[i] *= 1 - p[i][v];
if (abs(1 - prob[i]) > 1e-9) {
ans[i] = sum[i] / (1 - prob[i]);
}
}
}
cout << fixed << setprecision(9) << ans[1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 1011;
double bad[MN], f[MN], p[MN][MN];
bool done[MN];
int n;
int main() {
ios ::sync_with_stdio(0);
cin.tie(0);
cout << (fixed) << setprecision(9);
while (scanf("%d", &n) == 1) {
for (int i = (1), _b = (n); i <= _b; ++i)
for (int j = (1), _b = (n); j <= _b; ++j) {
int t;
scanf("%d", &t);
p[i][j] = t / (double)100.0;
if (i == j) p[i][j] = 1.0;
}
f[n] = 0.0;
for (int i = (1), _b = (n - 1); i <= _b; ++i) {
bad[i] = 1 - p[i][n];
f[i] = (double)1.0 / p[i][n];
}
memset(done, 0, sizeof done);
done[n] = true;
for (int turn = (1), _b = (n - 1); turn <= _b; ++turn) {
int best = -1;
for (int i = (1), _b = (n); i <= _b; ++i)
if (!done[i]) {
if (best < 0 || f[i] < f[best]) best = i;
}
done[best] = true;
for (int i = (1), _b = (n); i <= _b; ++i)
if (!done[i] && p[i][best] > 1e-9) {
if (fabs(1 - bad[i]) < 1e-9) {
f[i] = (p[i][best] * f[best] + 1) / p[i][best];
bad[i] = 1 - p[i][best];
} else {
double S = f[i] * (1 - bad[i]) - bad[i];
double bad2 = bad[i] * (1 - p[i][best]);
double f2 =
(S + bad[i] * p[i][best] * (f[best] + 1) + bad2) / (1 - bad2);
bad[i] = bad2;
f[i] = f2;
}
if (bad[i] < 1e-90) bad[i] = 0;
}
}
cout << f[1] << endl;
}
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MN = 1011;
double bad[MN], f[MN], p[MN][MN];
bool done[MN];
int n;
int main() {
ios ::sync_with_stdio(0);
cin.tie(0);
cout << (fixed) << setprecision(9);
while (scanf("%d", &n) == 1) {
for (int i = (1), _b = (n); i <= _b; ++i)
for (int j = (1), _b = (n); j <= _b; ++j) {
int t;
scanf("%d", &t);
p[i][j] = t / (double)100.0;
if (i == j) p[i][j] = 1.0;
}
f[n] = 0.0;
for (int i = (1), _b = (n - 1); i <= _b; ++i) {
bad[i] = 1 - p[i][n];
f[i] = (double)1.0 / p[i][n];
}
memset(done, 0, sizeof done);
done[n] = true;
for (int turn = (1), _b = (n - 1); turn <= _b; ++turn) {
int best = -1;
for (int i = (1), _b = (n); i <= _b; ++i)
if (!done[i]) {
if (best < 0 || f[i] < f[best]) best = i;
}
done[best] = true;
for (int i = (1), _b = (n); i <= _b; ++i)
if (!done[i] && p[i][best] > 1e-9) {
if (fabs(1 - bad[i]) < 1e-9) {
f[i] = (p[i][best] * f[best] + 1) / p[i][best];
bad[i] = 1 - p[i][best];
} else {
double S = f[i] * (1 - bad[i]) - bad[i];
double bad2 = bad[i] * (1 - p[i][best]);
double f2 =
(S + bad[i] * p[i][best] * (f[best] + 1) + bad2) / (1 - bad2);
bad[i] = bad2;
f[i] = f2;
}
if (bad[i] < 1e-90) bad[i] = 0;
}
}
cout << f[1] << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int Chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline int Chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
inline void proc_status() {
ifstream t("/proc/self/status");
cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
<< endl;
}
template <typename T>
T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}
const int Maxn = 1000 + 100;
int N, A[Maxn], Vis[Maxn];
double P[Maxn][Maxn], Dp[Maxn], Prod[Maxn], Sum[Maxn];
inline void Solve() {
for (int i = 1; i <= N; ++i) Prod[i] = Sum[i] = 1;
A[1] = N, Dp[N] = 0, Vis[N] = 1;
Dp[0] = 1e9;
for (int i = 2; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (Vis[j]) continue;
Sum[j] += Dp[A[i - 1]] * P[j][A[i - 1]] * Prod[j];
Prod[j] *= (1.0 - P[j][A[i - 1]]);
Dp[j] = Sum[j] / (1.0 - Prod[j]);
}
int pos = 0;
for (int j = 1; j <= N; ++j)
if (!Vis[j] && Dp[j] < Dp[pos]) pos = j;
Vis[A[i] = pos] = 1;
}
printf("%.10lf\n", Dp[1]);
}
inline void Input() {
N = read<int>();
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j) P[i][j] = 1.0 * read<int>() / 100;
}
int main() {
Input();
Solve();
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int Chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline int Chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
inline void proc_status() {
ifstream t("/proc/self/status");
cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
<< endl;
}
template <typename T>
T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}
const int Maxn = 1000 + 100;
int N, A[Maxn], Vis[Maxn];
double P[Maxn][Maxn], Dp[Maxn], Prod[Maxn], Sum[Maxn];
inline void Solve() {
for (int i = 1; i <= N; ++i) Prod[i] = Sum[i] = 1;
A[1] = N, Dp[N] = 0, Vis[N] = 1;
Dp[0] = 1e9;
for (int i = 2; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (Vis[j]) continue;
Sum[j] += Dp[A[i - 1]] * P[j][A[i - 1]] * Prod[j];
Prod[j] *= (1.0 - P[j][A[i - 1]]);
Dp[j] = Sum[j] / (1.0 - Prod[j]);
}
int pos = 0;
for (int j = 1; j <= N; ++j)
if (!Vis[j] && Dp[j] < Dp[pos]) pos = j;
Vis[A[i] = pos] = 1;
}
printf("%.10lf\n", Dp[1]);
}
inline void Input() {
N = read<int>();
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j) P[i][j] = 1.0 * read<int>() / 100;
}
int main() {
Input();
Solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &num) {
num = 0;
bool w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = 0;
c = getchar();
}
while (isdigit(c)) {
num = num * 10 + c - '0';
;
c = getchar();
}
num = w ? num : -num;
}
const int N = 1e3 + 10;
const double inf = 1e9, eps = 1e-9;
int n, vis[N];
double p[N][N], rest[N], dis[N];
signed main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) read(p[i][j]), p[i][j] /= 100;
for (int i = 1; i < n; i++) rest[i] = 1 - p[i][n], dis[i] = 1;
dis[0] = inf;
vis[n] = 1;
for (int i = 1; i <= n; i++) {
int x = 0;
for (int j = n; j; j--)
if (!vis[j] && dis[x] / (1 - rest[x]) - dis[j] / (1 - rest[j]) > eps)
x = j;
vis[x] = 1;
dis[x] /= (1 - rest[x]);
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
dis[j] += dis[x] * p[j][x] * rest[j];
rest[j] *= (1 - p[j][x]);
}
}
printf("%.15lf\n", dis[1]);
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &num) {
num = 0;
bool w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = 0;
c = getchar();
}
while (isdigit(c)) {
num = num * 10 + c - '0';
;
c = getchar();
}
num = w ? num : -num;
}
const int N = 1e3 + 10;
const double inf = 1e9, eps = 1e-9;
int n, vis[N];
double p[N][N], rest[N], dis[N];
signed main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) read(p[i][j]), p[i][j] /= 100;
for (int i = 1; i < n; i++) rest[i] = 1 - p[i][n], dis[i] = 1;
dis[0] = inf;
vis[n] = 1;
for (int i = 1; i <= n; i++) {
int x = 0;
for (int j = n; j; j--)
if (!vis[j] && dis[x] / (1 - rest[x]) - dis[j] / (1 - rest[j]) > eps)
x = j;
vis[x] = 1;
dis[x] /= (1 - rest[x]);
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
dis[j] += dis[x] * p[j][x] * rest[j];
rest[j] *= (1 - p[j][x]);
}
}
printf("%.15lf\n", dis[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
int getch() {
static const int len = 1 << 20;
static char buf[len], *st, *ed;
if (st == ed) ed = buf + fread(st = buf, 1, len, stdin);
return st == ed ? EOF : *st++;
}
template <typename T>
void read(T& x) {
static int c;
x = 0, c = getch();
while (isspace(c)) c = getch();
while (isdigit(c)) x = x * 10 + c - '0', c = getch();
return;
}
template <typename T, typename... Args>
void read(T& x, Args&... args) {
return read(x), read(args...);
}
const int maxn = 1e3 + 1;
int n, m, vis[maxn], cur;
double p[maxn][maxn], e[maxn], pdt[maxn], val;
int main() {
read(n), cur = n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) read(m), p[i][j] = m / 100.0;
for (int i = 1; i < n; i++) e[i] = 1, pdt[i] = 1 - p[i][n];
while (cur != 1) {
vis[cur] = 1, cur = 0, val = 1e100;
for (int i = 1; i <= n; i++)
if (!vis[i] && e[i] / (1 - pdt[i]) < val)
val = e[i] / (1 - pdt[i]), cur = i;
if (cur == 1) printf("%.10lf\n", val);
for (int i = 1; i <= n; i++)
e[i] += val * p[i][cur] * pdt[i], pdt[i] *= 1 - p[i][cur];
}
if (n == 1) printf("%.10lf\n", 0.0);
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
int getch() {
static const int len = 1 << 20;
static char buf[len], *st, *ed;
if (st == ed) ed = buf + fread(st = buf, 1, len, stdin);
return st == ed ? EOF : *st++;
}
template <typename T>
void read(T& x) {
static int c;
x = 0, c = getch();
while (isspace(c)) c = getch();
while (isdigit(c)) x = x * 10 + c - '0', c = getch();
return;
}
template <typename T, typename... Args>
void read(T& x, Args&... args) {
return read(x), read(args...);
}
const int maxn = 1e3 + 1;
int n, m, vis[maxn], cur;
double p[maxn][maxn], e[maxn], pdt[maxn], val;
int main() {
read(n), cur = n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) read(m), p[i][j] = m / 100.0;
for (int i = 1; i < n; i++) e[i] = 1, pdt[i] = 1 - p[i][n];
while (cur != 1) {
vis[cur] = 1, cur = 0, val = 1e100;
for (int i = 1; i <= n; i++)
if (!vis[i] && e[i] / (1 - pdt[i]) < val)
val = e[i] / (1 - pdt[i]), cur = i;
if (cur == 1) printf("%.10lf\n", val);
for (int i = 1; i <= n; i++)
e[i] += val * p[i][cur] * pdt[i], pdt[i] *= 1 - p[i][cur];
}
if (n == 1) printf("%.10lf\n", 0.0);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double p[1111][1111], E[1111], son[1111], mo[1111];
int vst[1111];
int main() {
int n, i, j;
cin >> n;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
for (i = 0; i < n; i++) E[i] = 1e9, mo[i] = 1, son[i] = 1;
E[n - 1] = 0;
for (int k = 0; k < n; k++) {
int u;
double mn = 1e20;
for (i = 0; i < n; i++) {
if (vst[i]) continue;
if (E[i] < mn) {
mn = E[i];
u = i;
}
}
vst[u] = 1;
if (u == 0) break;
for (i = 0; i < n; i++) {
if (vst[i]) continue;
son[i] += mo[i] * p[i][u] * E[u];
mo[i] *= 1 - p[i][u];
E[i] = son[i] / (1 - mo[i]);
}
}
printf("%.10lf\n", E[0]);
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double p[1111][1111], E[1111], son[1111], mo[1111];
int vst[1111];
int main() {
int n, i, j;
cin >> n;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
for (i = 0; i < n; i++) E[i] = 1e9, mo[i] = 1, son[i] = 1;
E[n - 1] = 0;
for (int k = 0; k < n; k++) {
int u;
double mn = 1e20;
for (i = 0; i < n; i++) {
if (vst[i]) continue;
if (E[i] < mn) {
mn = E[i];
u = i;
}
}
vst[u] = 1;
if (u == 0) break;
for (i = 0; i < n; i++) {
if (vst[i]) continue;
son[i] += mo[i] * p[i][u] * E[u];
mo[i] *= 1 - p[i][u];
E[i] = son[i] / (1 - mo[i]);
}
}
printf("%.10lf\n", E[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
long long ago;
using namespace std;
template <class T = long long int>
T nxt() {
T x;
cin >> x;
return x;
}
long long int pw(long long int a, long long int b, long long int mod) {
if (!b) return 1;
if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod;
return pw(a * a % mod, b / 2, mod) % mod;
}
const long long int N = 1000 + 10;
const long long int INF = 8e18;
const long long int MOD = 1e9 + 7;
long long int n, m, mark[N];
double dist[N], S[N], Z[N];
vector<pair<long long int, double>> adj[N];
priority_queue<pair<double, long long int>, vector<pair<double, long long int>>,
greater<pair<double, long long int>>>
pq;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << setprecision(15) << fixed;
cin >> n;
for (long long int i = 1; i <= n; i++) {
for (long long int j = 1; j <= n; j++) {
long long int p = nxt();
if (i == j || p == 0) continue;
adj[j].emplace_back(i, double(p) / 100);
}
}
fill(Z, Z + N, 1.0);
fill(dist, dist + N, INF);
dist[n] = 0;
pq.push({0, n});
while (!pq.empty()) {
long long int u = pq.top().second;
pq.pop();
if (mark[u]) continue;
mark[u] = 1;
for (auto [v, p] : adj[u]) {
if (mark[v]) continue;
S[v] += Z[v] * p * (dist[u] + 1);
Z[v] *= (1 - p);
dist[v] = (S[v] + Z[v]) / (1 - Z[v]);
pq.push({dist[v], v});
}
}
cout << dist[1] << '\n';
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
long long ago;
using namespace std;
template <class T = long long int>
T nxt() {
T x;
cin >> x;
return x;
}
long long int pw(long long int a, long long int b, long long int mod) {
if (!b) return 1;
if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod;
return pw(a * a % mod, b / 2, mod) % mod;
}
const long long int N = 1000 + 10;
const long long int INF = 8e18;
const long long int MOD = 1e9 + 7;
long long int n, m, mark[N];
double dist[N], S[N], Z[N];
vector<pair<long long int, double>> adj[N];
priority_queue<pair<double, long long int>, vector<pair<double, long long int>>,
greater<pair<double, long long int>>>
pq;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << setprecision(15) << fixed;
cin >> n;
for (long long int i = 1; i <= n; i++) {
for (long long int j = 1; j <= n; j++) {
long long int p = nxt();
if (i == j || p == 0) continue;
adj[j].emplace_back(i, double(p) / 100);
}
}
fill(Z, Z + N, 1.0);
fill(dist, dist + N, INF);
dist[n] = 0;
pq.push({0, n});
while (!pq.empty()) {
long long int u = pq.top().second;
pq.pop();
if (mark[u]) continue;
mark[u] = 1;
for (auto [v, p] : adj[u]) {
if (mark[v]) continue;
S[v] += Z[v] * p * (dist[u] + 1);
Z[v] *= (1 - p);
dist[v] = (S[v] + Z[v]) / (1 - Z[v]);
pq.push({dist[v], v});
}
}
cout << dist[1] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int u = 0, f = 1;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
u = (u << 1) + (u << 3) + ch - 48;
ch = getchar();
}
return u * f;
}
const int maxn = 1e3 + 10;
const double oo = 1e30;
double f[maxn], g[maxn], h[maxn];
double p[maxn][maxn];
int a[maxn];
bool flag[maxn];
int main() {
int n = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = read() / 100.0;
for (int i = 1; i <= n; ++i) f[i] = oo, g[i] = h[i] = 1;
f[n] = 0;
f[0] = oo;
for (int x = 1; x <= n; ++x) {
for (int i = 1; i <= n; ++i)
if (!flag[i] && f[a[x]] > f[i]) a[x] = i;
flag[a[x]] = 1;
for (int i = 1; i <= n; ++i)
if (!flag[i]) {
g[i] += h[i] * p[i][a[x]] * f[a[x]];
h[i] *= 1 - p[i][a[x]];
f[i] = h[i] == 1 ? oo : g[i] / (1 - h[i]);
}
}
printf("%.8lf", f[1]);
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int u = 0, f = 1;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
u = (u << 1) + (u << 3) + ch - 48;
ch = getchar();
}
return u * f;
}
const int maxn = 1e3 + 10;
const double oo = 1e30;
double f[maxn], g[maxn], h[maxn];
double p[maxn][maxn];
int a[maxn];
bool flag[maxn];
int main() {
int n = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = read() / 100.0;
for (int i = 1; i <= n; ++i) f[i] = oo, g[i] = h[i] = 1;
f[n] = 0;
f[0] = oo;
for (int x = 1; x <= n; ++x) {
for (int i = 1; i <= n; ++i)
if (!flag[i] && f[a[x]] > f[i]) a[x] = i;
flag[a[x]] = 1;
for (int i = 1; i <= n; ++i)
if (!flag[i]) {
g[i] += h[i] * p[i][a[x]] * f[a[x]];
h[i] *= 1 - p[i][a[x]];
f[i] = h[i] == 1 ? oo : g[i] / (1 - h[i]);
}
}
printf("%.8lf", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
double dis[N], pr[N], di[N], p[N][N];
int n, vis[N];
int main() {
cin >> n;
cout << fixed << setprecision(12);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
int x;
cin >> x;
p[i][j] = x / 100.;
}
for (int i = 1; i <= n; ++i) dis[i] = INFINITY;
dis[n] = 0;
for (int i = 1; i <= n; ++i) pr[i] = 1, di[i] = 1;
for (int i = 1; i <= n; ++i) {
double mn = INFINITY;
int mnid = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dis[j] < mn) mn = dis[j], mnid = j;
if (mnid == 1) {
cout << mn << '\n';
return 0;
}
vis[mnid] = 1;
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
di[j] += pr[j] * p[j][mnid] * mn;
pr[j] *= 1 - p[j][mnid];
dis[j] = di[j] / (1 - pr[j]);
}
}
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
double dis[N], pr[N], di[N], p[N][N];
int n, vis[N];
int main() {
cin >> n;
cout << fixed << setprecision(12);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
int x;
cin >> x;
p[i][j] = x / 100.;
}
for (int i = 1; i <= n; ++i) dis[i] = INFINITY;
dis[n] = 0;
for (int i = 1; i <= n; ++i) pr[i] = 1, di[i] = 1;
for (int i = 1; i <= n; ++i) {
double mn = INFINITY;
int mnid = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dis[j] < mn) mn = dis[j], mnid = j;
if (mnid == 1) {
cout << mn << '\n';
return 0;
}
vis[mnid] = 1;
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
di[j] += pr[j] * p[j][mnid] * mn;
pr[j] *= 1 - p[j][mnid];
dis[j] = di[j] / (1 - pr[j]);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1020][1020];
double m[1020], A[1020], B[1020];
bool vis[1020];
int dcmp(double x) {
if (fabs(x) < 1e-12) return 0;
return x < 0 ? -1 : 1;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
for (int i = 1; i <= n; ++i) A[i] = B[i] = 1, m[i] = 1e30;
m[n] = 0;
for (int i = 1; i <= n; ++i) {
int u = -1;
double tmp = 1e10;
for (int j = 1; j <= n; ++j) {
if (m[j] < tmp && !vis[j]) tmp = m[j], u = j;
}
vis[u] = 1;
for (int x = 1; x <= n; ++x) {
if (vis[x]) continue;
B[x] += A[x] * p[x][u] * m[u];
A[x] *= 1 - p[x][u];
}
for (int x = 1; x <= n; ++x) {
if (vis[x]) continue;
if (dcmp(1 - A[x]) == 0)
m[x] = 1e30;
else
m[x] = B[x] / (1 - A[x]);
}
}
printf("%.12lf\n", m[1]);
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1020][1020];
double m[1020], A[1020], B[1020];
bool vis[1020];
int dcmp(double x) {
if (fabs(x) < 1e-12) return 0;
return x < 0 ? -1 : 1;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
for (int i = 1; i <= n; ++i) A[i] = B[i] = 1, m[i] = 1e30;
m[n] = 0;
for (int i = 1; i <= n; ++i) {
int u = -1;
double tmp = 1e10;
for (int j = 1; j <= n; ++j) {
if (m[j] < tmp && !vis[j]) tmp = m[j], u = j;
}
vis[u] = 1;
for (int x = 1; x <= n; ++x) {
if (vis[x]) continue;
B[x] += A[x] * p[x][u] * m[u];
A[x] *= 1 - p[x][u];
}
for (int x = 1; x <= n; ++x) {
if (vis[x]) continue;
if (dcmp(1 - A[x]) == 0)
m[x] = 1e30;
else
m[x] = B[x] / (1 - A[x]);
}
}
printf("%.12lf\n", m[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char ch = getchar();
long long s = 0, w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
inline int lowbit(int x) { return x & (-x); }
double f[1010], p[1010][1010], g[1010];
int n, vis[1010];
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
Q;
int main() {
n = read();
for (register int i = 1; i <= n; i++)
for (register int j = 1; j <= n; j++) p[i][j] = (double)read() / 100.0;
for (register int i = 1; i <= n; i++) g[i] = 1;
int x = n, T = n - 1;
vis[n] = 1;
while (T--) {
for (register int i = 1; i <= n; i++)
if (!vis[i]) f[i] += g[i] * p[i][x] * (f[x] + 1), g[i] *= (1 - p[i][x]);
double Minn = 1e12;
int minx = 0;
for (register int i = 1; i <= n; i++)
if (!vis[i] && (f[i] + g[i]) / (1 - g[i]) < Minn)
Minn = (f[i] + g[i]) / (1 - g[i]), minx = i;
x = minx;
vis[x] = 1;
f[x] = (f[x] + g[x]) / (1 - g[x]);
}
printf("%.10lf\n", f[1]);
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char ch = getchar();
long long s = 0, w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
inline int lowbit(int x) { return x & (-x); }
double f[1010], p[1010][1010], g[1010];
int n, vis[1010];
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
Q;
int main() {
n = read();
for (register int i = 1; i <= n; i++)
for (register int j = 1; j <= n; j++) p[i][j] = (double)read() / 100.0;
for (register int i = 1; i <= n; i++) g[i] = 1;
int x = n, T = n - 1;
vis[n] = 1;
while (T--) {
for (register int i = 1; i <= n; i++)
if (!vis[i]) f[i] += g[i] * p[i][x] * (f[x] + 1), g[i] *= (1 - p[i][x]);
double Minn = 1e12;
int minx = 0;
for (register int i = 1; i <= n; i++)
if (!vis[i] && (f[i] + g[i]) / (1 - g[i]) < Minn)
Minn = (f[i] + g[i]) / (1 - g[i]), minx = i;
x = minx;
vis[x] = 1;
f[x] = (f[x] + g[x]) / (1 - g[x]);
}
printf("%.10lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long inp[1005][1005];
vector<bool> visited(1005);
vector<long double> score(1005);
int main() {
long long n;
cin >> n;
for (long long i = 0; i < (n); ++i) {
for (long long j = 0; j < (n); ++j) {
cin >> inp[i][j];
}
}
for (long long i = 0; i < (n); ++i) {
visited[i] = false;
score[i] = 0;
}
map<long long, long double> curDist;
map<long long, long double> curPercent;
for (long long i = 0; i < (n); ++i) {
curPercent[i] = 0;
}
set<pair<long double, long long> > queue;
queue.insert(make_pair(0, n - 1));
cout << fixed << setprecision(9);
while (!queue.empty()) {
pair<long double, long long> st = *queue.begin();
long long idx = st.second;
long double dist = st.first;
queue.erase(queue.begin());
if (visited[idx]) {
continue;
}
visited[idx] = true;
score[idx] = dist;
for (long long i = 0; i < (n); ++i) {
if (!visited[i]) {
long double morePercent = (long double)inp[i][idx] / 100.0;
if (morePercent == 0) {
continue;
}
curDist[i] += morePercent * (1.0 - curPercent[i]) * (dist + 1.0);
curPercent[i] += morePercent * (1.0 - curPercent[i]);
queue.insert(
make_pair((curDist[i] + 1.0 - curPercent[i]) / curPercent[i], i));
}
}
}
cout << score[0];
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long inp[1005][1005];
vector<bool> visited(1005);
vector<long double> score(1005);
int main() {
long long n;
cin >> n;
for (long long i = 0; i < (n); ++i) {
for (long long j = 0; j < (n); ++j) {
cin >> inp[i][j];
}
}
for (long long i = 0; i < (n); ++i) {
visited[i] = false;
score[i] = 0;
}
map<long long, long double> curDist;
map<long long, long double> curPercent;
for (long long i = 0; i < (n); ++i) {
curPercent[i] = 0;
}
set<pair<long double, long long> > queue;
queue.insert(make_pair(0, n - 1));
cout << fixed << setprecision(9);
while (!queue.empty()) {
pair<long double, long long> st = *queue.begin();
long long idx = st.second;
long double dist = st.first;
queue.erase(queue.begin());
if (visited[idx]) {
continue;
}
visited[idx] = true;
score[idx] = dist;
for (long long i = 0; i < (n); ++i) {
if (!visited[i]) {
long double morePercent = (long double)inp[i][idx] / 100.0;
if (morePercent == 0) {
continue;
}
curDist[i] += morePercent * (1.0 - curPercent[i]) * (dist + 1.0);
curPercent[i] += morePercent * (1.0 - curPercent[i]);
queue.insert(
make_pair((curDist[i] + 1.0 - curPercent[i]) / curPercent[i], i));
}
}
}
cout << score[0];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
int n;
double G[maxn][maxn];
int vis[maxn];
double f[maxn], s[maxn];
int ri() {
int s = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
s = (s << 1) + (s << 3) + (c ^ 48);
c = getchar();
}
return s * w;
}
int main() {
n = ri();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
G[i][j] = 1.0 * ri() / 100.0;
}
}
if (n == 1) return puts("0"), 0;
f[n] = 0.0;
vis[n] = 1;
for (int i = 1; i <= n; i++) {
f[i] = 1;
s[i] = 1 - G[i][n];
}
for (int i = 1; i <= n; i++) {
int pos = 0;
double Min = 1e12;
for (int j = 1; j <= n; j++) {
if (vis[j] == 0 && 1.0 * f[j] / (1 - s[j]) < Min) {
Min = 1.0 * f[j] / (1 - s[j]);
pos = j;
}
}
vis[pos] = 1;
if (pos == 1) {
printf("%.6lf\n", Min);
return 0;
}
for (int j = 1; j <= n; j++) {
f[j] = f[j] + f[pos] / (1 - s[pos]) * G[j][pos] * s[j],
s[j] *= (1 - G[j][pos]);
}
}
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
int n;
double G[maxn][maxn];
int vis[maxn];
double f[maxn], s[maxn];
int ri() {
int s = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
s = (s << 1) + (s << 3) + (c ^ 48);
c = getchar();
}
return s * w;
}
int main() {
n = ri();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
G[i][j] = 1.0 * ri() / 100.0;
}
}
if (n == 1) return puts("0"), 0;
f[n] = 0.0;
vis[n] = 1;
for (int i = 1; i <= n; i++) {
f[i] = 1;
s[i] = 1 - G[i][n];
}
for (int i = 1; i <= n; i++) {
int pos = 0;
double Min = 1e12;
for (int j = 1; j <= n; j++) {
if (vis[j] == 0 && 1.0 * f[j] / (1 - s[j]) < Min) {
Min = 1.0 * f[j] / (1 - s[j]);
pos = j;
}
}
vis[pos] = 1;
if (pos == 1) {
printf("%.6lf\n", Min);
return 0;
}
for (int j = 1; j <= n; j++) {
f[j] = f[j] + f[pos] / (1 - s[pos]) * G[j][pos] * s[j],
s[j] *= (1 - G[j][pos]);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void upmax(T& a, T b) {
if (a < b) a = b;
}
template <class T>
inline void upmin(T& a, T b) {
if (a > b) a = b;
}
const int maxn = 1007;
const int maxm = 200007;
const int mod = 1000000007;
const int inf = 0x7fffffff;
const double eps = 1e-7;
typedef int arr[maxn];
typedef int adj[maxm];
inline int add(int a, int b) { return ((long long)a + b) % mod; }
inline int mul(int a, int b) { return ((long long)a * b) % mod; }
inline int dec(int a, int b) { return add(a, mod - b % mod); }
inline int Pow(int a, int b) {
int t = 1;
while (b) {
if (b & 1) t = mul(t, a);
a = mul(a, a), b >>= 1;
}
return t;
}
template <typename Type>
inline Type RD() {
char c = getchar();
Type x = 0, flag = 1;
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
flag = -1;
else
x = c - '0';
while (isdigit(c = getchar())) x = x * 10 + c - '0';
return x * flag;
}
inline char rdch() {
char c = getchar();
while (!isalpha(c)) c = getchar();
return c;
}
inline int fcmp(double a, double b) {
if (fabs(a - b) < eps) return 0;
if (a < b - eps) return -1;
return 1;
}
int n, vis[maxn];
double p[maxn][maxn], E[maxn], A[maxn], B[maxn];
void input() {
n = RD<int>();
for (int i = 1, _ = n; i <= _; i++)
for (int j = 1, _ = n; j <= _; j++) p[i][j] = RD<int>() * 0.01;
}
void solve() {
for (int i = 0, _ = n; i <= _; i++) E[i] = 2147483647, A[i] = 1, B[i] = 1;
E[n] = 0;
for (int k = 1, _ = n; k <= _; k++) {
int u = 0;
for (int i = 1, _ = n; i <= _; i++)
if (!vis[i] && fcmp(E[i], E[u]) < 0) u = i;
vis[u] = 1;
for (int v = 1, _ = n; v <= _; v++)
if (!vis[v]) {
A[v] += p[v][u] * E[u] * B[v];
B[v] *= (1 - p[v][u]);
if (fcmp(B[v], 1)) upmin(E[v], A[v] / (1 - B[v]));
}
}
printf("%.10lf\n", E[1]);
}
int main() {
input();
solve();
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void upmax(T& a, T b) {
if (a < b) a = b;
}
template <class T>
inline void upmin(T& a, T b) {
if (a > b) a = b;
}
const int maxn = 1007;
const int maxm = 200007;
const int mod = 1000000007;
const int inf = 0x7fffffff;
const double eps = 1e-7;
typedef int arr[maxn];
typedef int adj[maxm];
inline int add(int a, int b) { return ((long long)a + b) % mod; }
inline int mul(int a, int b) { return ((long long)a * b) % mod; }
inline int dec(int a, int b) { return add(a, mod - b % mod); }
inline int Pow(int a, int b) {
int t = 1;
while (b) {
if (b & 1) t = mul(t, a);
a = mul(a, a), b >>= 1;
}
return t;
}
template <typename Type>
inline Type RD() {
char c = getchar();
Type x = 0, flag = 1;
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
flag = -1;
else
x = c - '0';
while (isdigit(c = getchar())) x = x * 10 + c - '0';
return x * flag;
}
inline char rdch() {
char c = getchar();
while (!isalpha(c)) c = getchar();
return c;
}
inline int fcmp(double a, double b) {
if (fabs(a - b) < eps) return 0;
if (a < b - eps) return -1;
return 1;
}
int n, vis[maxn];
double p[maxn][maxn], E[maxn], A[maxn], B[maxn];
void input() {
n = RD<int>();
for (int i = 1, _ = n; i <= _; i++)
for (int j = 1, _ = n; j <= _; j++) p[i][j] = RD<int>() * 0.01;
}
void solve() {
for (int i = 0, _ = n; i <= _; i++) E[i] = 2147483647, A[i] = 1, B[i] = 1;
E[n] = 0;
for (int k = 1, _ = n; k <= _; k++) {
int u = 0;
for (int i = 1, _ = n; i <= _; i++)
if (!vis[i] && fcmp(E[i], E[u]) < 0) u = i;
vis[u] = 1;
for (int v = 1, _ = n; v <= _; v++)
if (!vis[v]) {
A[v] += p[v][u] * E[u] * B[v];
B[v] *= (1 - p[v][u]);
if (fcmp(B[v], 1)) upmin(E[v], A[v] / (1 - B[v]));
}
}
printf("%.10lf\n", E[1]);
}
int main() {
input();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
i >> p.first >> p.second;
return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
for (auto& v : t) {
i >> v;
}
return i;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
o << p.first << ' ' << p.second;
return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
if (t.empty()) o << '\n';
for (size_t i = 0; i < t.size(); ++i) {
o << t[i] << " \n"[i == t.size() - 1];
}
return o;
}
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T>
bool in(T a, T b, T c) {
return a <= b && b < c;
}
unsigned int logceil(int first) {
return 8 * sizeof(int) - __builtin_clz(first);
}
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
hash<T> t;
hash<U> u;
size_t operator()(const pair<T, U>& p) const {
return t(p.first) ^ (u(p.second) << 7);
}
};
} // namespace std
template <typename T, typename F>
T bsh(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
l = m + 1;
r = m;
} else {
h = m - 1;
}
}
return r;
}
template <typename F>
double bshd(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
l = m;
} else {
h = m;
}
}
return (l + h) / 2;
}
template <typename T, typename F>
T bsl(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
h = m - 1;
r = m;
} else {
l = m + 1;
}
}
return r;
}
template <typename F>
double bsld(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
h = m;
} else {
l = m;
}
}
return (l + h) / 2;
}
template <typename T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
template <typename T>
class vector2 : public vector<vector<T>> {
public:
vector2() {}
vector2(size_t a, size_t b, T t = T())
: vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T>
class vector3 : public vector<vector2<T>> {
public:
vector3() {}
vector3(size_t a, size_t b, size_t c, T t = T())
: vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T>
class vector4 : public vector<vector3<T>> {
public:
vector4() {}
vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
: vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T>
class vector5 : public vector<vector4<T>> {
public:
vector5() {}
vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
: vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};
class intergalaxytrips {
public:
void solve(istream& cin, ostream& cout) {
int N;
cin >> N;
vector2<int> W(N, N, 0);
cin >> W;
vector2<long double> Z(N, N, 0);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
Z[i][j] = W[i][j] / 100.0;
}
}
vector<long double> P(N, 1e10), A(N, 0), B(N, 1);
P[N - 1] = 0;
for (int i = 0; i < N - 1; ++i) B[i] = 1 - Z[i][N - 1];
for (int t = 0; t < N - 1; ++t) {
pair<long double, int> Q(1e20, -1);
for (int i = 0; i < N; ++i) {
if (P[i] > 1e9 && B[i] < 1 - 1e-9) {
Q = min(Q, {(1 + A[i]) / (1 - B[i]), i});
}
}
int j = Q.second;
if (j != -1) {
P[j] = Q.first;
for (int i = 0; i < N; ++i) {
if (P[i] > 1e9) {
A[i] += B[i] * P[j] * Z[i][j];
B[i] *= 1 - Z[i][j];
if (B[i] < 1e-10) {
B[i] = 0;
}
}
}
}
}
cout << fixed << setprecision(15) << P[0] << endl;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
intergalaxytrips solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
i >> p.first >> p.second;
return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
for (auto& v : t) {
i >> v;
}
return i;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
o << p.first << ' ' << p.second;
return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
if (t.empty()) o << '\n';
for (size_t i = 0; i < t.size(); ++i) {
o << t[i] << " \n"[i == t.size() - 1];
}
return o;
}
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T>
bool in(T a, T b, T c) {
return a <= b && b < c;
}
unsigned int logceil(int first) {
return 8 * sizeof(int) - __builtin_clz(first);
}
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
hash<T> t;
hash<U> u;
size_t operator()(const pair<T, U>& p) const {
return t(p.first) ^ (u(p.second) << 7);
}
};
} // namespace std
template <typename T, typename F>
T bsh(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
l = m + 1;
r = m;
} else {
h = m - 1;
}
}
return r;
}
template <typename F>
double bshd(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
l = m;
} else {
h = m;
}
}
return (l + h) / 2;
}
template <typename T, typename F>
T bsl(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
h = m - 1;
r = m;
} else {
l = m + 1;
}
}
return r;
}
template <typename F>
double bsld(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
h = m;
} else {
l = m;
}
}
return (l + h) / 2;
}
template <typename T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
template <typename T>
class vector2 : public vector<vector<T>> {
public:
vector2() {}
vector2(size_t a, size_t b, T t = T())
: vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T>
class vector3 : public vector<vector2<T>> {
public:
vector3() {}
vector3(size_t a, size_t b, size_t c, T t = T())
: vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T>
class vector4 : public vector<vector3<T>> {
public:
vector4() {}
vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
: vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T>
class vector5 : public vector<vector4<T>> {
public:
vector5() {}
vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
: vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};
class intergalaxytrips {
public:
void solve(istream& cin, ostream& cout) {
int N;
cin >> N;
vector2<int> W(N, N, 0);
cin >> W;
vector2<long double> Z(N, N, 0);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
Z[i][j] = W[i][j] / 100.0;
}
}
vector<long double> P(N, 1e10), A(N, 0), B(N, 1);
P[N - 1] = 0;
for (int i = 0; i < N - 1; ++i) B[i] = 1 - Z[i][N - 1];
for (int t = 0; t < N - 1; ++t) {
pair<long double, int> Q(1e20, -1);
for (int i = 0; i < N; ++i) {
if (P[i] > 1e9 && B[i] < 1 - 1e-9) {
Q = min(Q, {(1 + A[i]) / (1 - B[i]), i});
}
}
int j = Q.second;
if (j != -1) {
P[j] = Q.first;
for (int i = 0; i < N; ++i) {
if (P[i] > 1e9) {
A[i] += B[i] * P[j] * Z[i][j];
B[i] *= 1 - Z[i][j];
if (B[i] < 1e-10) {
B[i] = 0;
}
}
}
}
}
cout << fixed << setprecision(15) << P[0] << endl;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
intergalaxytrips solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int s[N][N];
bool w[N], us[N];
double p[N], f[N], g[N];
inline int gi() {
int x = 0, o = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) s[i][j] = gi();
w[n] = 1, f[0] = 1e100;
for (int t = 1; t <= n; t++) {
int x = 0;
for (int i = 1; i <= n; i++)
if (!us[i] && w[i] && f[i] < f[x]) x = i;
us[x] = 1;
for (int y = 1; y <= n; y++)
if (s[y][x] && !us[y]) {
if (!w[y]) w[y] = 1, p[y] = 1;
g[y] += p[y] * s[y][x] / 100 * (f[x] + 1), p[y] *= 1 - s[y][x] / 100.0;
f[y] = (g[y] + p[y]) / (1 - p[y]);
}
}
printf("%.10f", f[1]);
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int s[N][N];
bool w[N], us[N];
double p[N], f[N], g[N];
inline int gi() {
int x = 0, o = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) s[i][j] = gi();
w[n] = 1, f[0] = 1e100;
for (int t = 1; t <= n; t++) {
int x = 0;
for (int i = 1; i <= n; i++)
if (!us[i] && w[i] && f[i] < f[x]) x = i;
us[x] = 1;
for (int y = 1; y <= n; y++)
if (s[y][x] && !us[y]) {
if (!w[y]) w[y] = 1, p[y] = 1;
g[y] += p[y] * s[y][x] / 100 * (f[x] + 1), p[y] *= 1 - s[y][x] / 100.0;
f[y] = (g[y] + p[y]) / (1 - p[y]);
}
}
printf("%.10f", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, ta;
long double x[1005][1005], tb, tc, td;
vector<int> y;
vector<long double> z;
bool used[1005];
int main() {
scanf("%d", &n);
for (int a = 0; a < n; a++)
for (int b = 0; b < n; b++) {
scanf("%d", &ta);
x[a][b] = ta;
x[a][b] /= 100;
}
y.push_back(n - 1);
z.push_back(0);
used[n - 1] = 1;
while (y.back() != 0) {
ta = -1;
for (int a = 0; a < n; a++)
if (!used[a]) {
tb = 0;
tc = 1;
for (int b = 0; b < y.size(); b++) {
tb += (z[b] + 1) * tc * x[a][y[b]];
tc *= 1 - x[a][y[b]];
}
if (abs(tc - 1) < 1e-11) continue;
tb = (tb + tc) / (1 - tc);
if (ta == -1 || tb < td) {
ta = a;
td = tb;
}
}
y.push_back(ta);
z.push_back(td);
for (int a = z.size() - 1; a >= 1; a--) {
if (z[a] >= z[a - 1]) break;
swap(z[a], z[a - 1]);
swap(y[a], y[a - 1]);
}
used[ta] = 1;
}
printf("%.15lf\n", (double)z.back());
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, ta;
long double x[1005][1005], tb, tc, td;
vector<int> y;
vector<long double> z;
bool used[1005];
int main() {
scanf("%d", &n);
for (int a = 0; a < n; a++)
for (int b = 0; b < n; b++) {
scanf("%d", &ta);
x[a][b] = ta;
x[a][b] /= 100;
}
y.push_back(n - 1);
z.push_back(0);
used[n - 1] = 1;
while (y.back() != 0) {
ta = -1;
for (int a = 0; a < n; a++)
if (!used[a]) {
tb = 0;
tc = 1;
for (int b = 0; b < y.size(); b++) {
tb += (z[b] + 1) * tc * x[a][y[b]];
tc *= 1 - x[a][y[b]];
}
if (abs(tc - 1) < 1e-11) continue;
tb = (tb + tc) / (1 - tc);
if (ta == -1 || tb < td) {
ta = a;
td = tb;
}
}
y.push_back(ta);
z.push_back(td);
for (int a = z.size() - 1; a >= 1; a--) {
if (z[a] >= z[a - 1]) break;
swap(z[a], z[a - 1]);
swap(y[a], y[a - 1]);
}
used[ta] = 1;
}
printf("%.15lf\n", (double)z.back());
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1013;
double p[N][N], f[N], g[N];
bool done[N];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
scanf("%lf", &p[i][j]);
p[i][j] *= 0.01;
}
for (int i = 1; i < n; ++i) f[i] = g[i] = 1.0;
for (int ct = 1; ct <= n; ++ct) {
int x = 0;
double mi = 1e9;
for (int i = 1; i <= n; ++i)
if (!done[i] and g[i] < 1)
if (f[i] / (1 - g[i]) < mi) mi = f[i] / (1 - g[i]), x = i;
if (!x) break;
done[x] = true;
f[x] /= (1 - g[x]);
for (int i = 1; i <= n; ++i)
if (!done[i]) {
f[i] += g[i] * p[i][x] * f[x];
g[i] *= (1 - p[i][x]);
}
}
printf("%.7lf\n", f[1]);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1013;
double p[N][N], f[N], g[N];
bool done[N];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
scanf("%lf", &p[i][j]);
p[i][j] *= 0.01;
}
for (int i = 1; i < n; ++i) f[i] = g[i] = 1.0;
for (int ct = 1; ct <= n; ++ct) {
int x = 0;
double mi = 1e9;
for (int i = 1; i <= n; ++i)
if (!done[i] and g[i] < 1)
if (f[i] / (1 - g[i]) < mi) mi = f[i] / (1 - g[i]), x = i;
if (!x) break;
done[x] = true;
f[x] /= (1 - g[x]);
for (int i = 1; i <= n; ++i)
if (!done[i]) {
f[i] += g[i] * p[i][x] * f[x];
g[i] *= (1 - p[i][x]);
}
}
printf("%.7lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 1005, inf = 1000000005, mod = 1000000007;
const long long INF = 1000000000000000005LL;
double pstwo[MN][MN], A[MN], B[MN], W[MN];
int done[MN];
priority_queue<pair<double, int> > Q;
int n;
void dij() {
Q.push({0.0, n});
while (!Q.empty()) {
int cur = Q.top().second;
Q.pop();
if (done[cur]) continue;
done[cur] = 1;
for (int i = 1; i <= n; ++i)
if (!done[i]) {
A[i] += B[i] * pstwo[i][cur] * W[cur];
B[i] *= (1.0 - pstwo[i][cur]);
W[i] = ((1.0 + A[i]) / (1.0 - B[i]));
Q.push({-W[i], i});
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
B[i] = 1.0;
for (int j = 1; j <= n; ++j) {
int p;
scanf("%d", &p);
pstwo[i][j] = (double)p / 100.0;
}
}
dij();
printf("%.9lf", W[1]);
}
|
### Prompt
Create a solution in Cpp for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MN = 1005, inf = 1000000005, mod = 1000000007;
const long long INF = 1000000000000000005LL;
double pstwo[MN][MN], A[MN], B[MN], W[MN];
int done[MN];
priority_queue<pair<double, int> > Q;
int n;
void dij() {
Q.push({0.0, n});
while (!Q.empty()) {
int cur = Q.top().second;
Q.pop();
if (done[cur]) continue;
done[cur] = 1;
for (int i = 1; i <= n; ++i)
if (!done[i]) {
A[i] += B[i] * pstwo[i][cur] * W[cur];
B[i] *= (1.0 - pstwo[i][cur]);
W[i] = ((1.0 + A[i]) / (1.0 - B[i]));
Q.push({-W[i], i});
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
B[i] = 1.0;
for (int j = 1; j <= n; ++j) {
int p;
scanf("%d", &p);
pstwo[i][j] = (double)p / 100.0;
}
}
dij();
printf("%.9lf", W[1]);
}
```
|
#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const long long INF = 0x7fffffffffffffff;
const double eps = 1e-10;
template <typename _Tp>
_Tp gcd(const _Tp &a, const _Tp &b) {
return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) {
return a >= 0 ? a : -a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) {
return a < b ? b : a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) {
return a < b ? a : b;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) {
(a < b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) {
(b < a) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) bool _cmp(const _Tp &a,
const _Tp &b) {
return abs(a - b) <= eps;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void read(_Tp &x) {
register char ch(getchar());
bool f(false);
while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (ch >= 48 && ch <= 57)
x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
if (f) x = -x;
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
__inline__ __attribute__((always_inline)) int read_str(char *s) {
register char ch(getchar());
while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar();
register char *tar = s;
*tar = ch, ch = getchar();
while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
*(++tar) = ch, ch = getchar();
return tar - s + 1;
}
const int N = 1005;
double a[N][N];
double p[N], E[N];
bool vis[N];
int main() {
int n;
read(n);
for (int i = 1; i <= n; ++i) {
p[i] = 1.0;
E[i] = 1.0;
for (int j = 1; j <= n; ++j) {
scanf("%lf", &a[i][j]);
a[i][j] /= 100.0;
}
}
p[n] = 0;
E[n] = 0.0;
while (true) {
double minn = 1e18;
int pos = 0;
for (int i = 1; i <= n; ++i) {
if (vis[i] || p[i] == 1.0) continue;
double tmp = E[i] / (1.0 - p[i]);
if (tmp < minn) {
minn = tmp;
pos = i;
}
}
if (!pos) break;
vis[pos] = true;
E[pos] = minn;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
E[i] += minn * p[i] * a[i][pos];
p[i] *= (1.0 - a[i][pos]);
}
}
}
printf("%.10lf\n", E[1]);
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const long long INF = 0x7fffffffffffffff;
const double eps = 1e-10;
template <typename _Tp>
_Tp gcd(const _Tp &a, const _Tp &b) {
return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) {
return a >= 0 ? a : -a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) {
return a < b ? b : a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) {
return a < b ? a : b;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) {
(a < b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) {
(b < a) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) bool _cmp(const _Tp &a,
const _Tp &b) {
return abs(a - b) <= eps;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void read(_Tp &x) {
register char ch(getchar());
bool f(false);
while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (ch >= 48 && ch <= 57)
x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
if (f) x = -x;
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
__inline__ __attribute__((always_inline)) int read_str(char *s) {
register char ch(getchar());
while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar();
register char *tar = s;
*tar = ch, ch = getchar();
while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
*(++tar) = ch, ch = getchar();
return tar - s + 1;
}
const int N = 1005;
double a[N][N];
double p[N], E[N];
bool vis[N];
int main() {
int n;
read(n);
for (int i = 1; i <= n; ++i) {
p[i] = 1.0;
E[i] = 1.0;
for (int j = 1; j <= n; ++j) {
scanf("%lf", &a[i][j]);
a[i][j] /= 100.0;
}
}
p[n] = 0;
E[n] = 0.0;
while (true) {
double minn = 1e18;
int pos = 0;
for (int i = 1; i <= n; ++i) {
if (vis[i] || p[i] == 1.0) continue;
double tmp = E[i] / (1.0 - p[i]);
if (tmp < minn) {
minn = tmp;
pos = i;
}
}
if (!pos) break;
vis[pos] = true;
E[pos] = minn;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
E[i] += minn * p[i] * a[i][pos];
p[i] *= (1.0 - a[i][pos]);
}
}
}
printf("%.10lf\n", E[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
inline void write(int x) {
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const int N = 1005;
int n;
long double a[N][N], f[N], p[N];
bool vis[N];
int main() {
int i, j, k, x;
read(n);
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j) read(k), a[i][j] = k * 1.0 / 100;
for (i = 1; i <= n; ++i) a[i][i] = 0, p[i] = 1;
f[n] = 0;
vis[n] = 1;
for (i = 1; i < n; ++i)
f[i] += p[i] * a[i][n] * (1 + f[n]), p[i] *= (1 - a[i][n]);
for (k = 1; k < n; ++k) {
x = -1;
for (i = 1; i <= n; ++i)
if (!vis[i]) {
if (fabs(p[i] - 1) <= 1e-8) continue;
if (x == -1)
x = i;
else {
if (f[x] / (1 - p[x]) + 1 / (1 - p[x]) >
f[i] / (1 - p[i]) + 1 / (1 - p[i]))
x = i;
}
}
if (x == -1) continue;
vis[x] = 1, f[x] /= 1 - p[x], f[x] += 1 / (1 - p[x]) - 1;
for (i = 1; i <= n; ++i)
if (!vis[i]) f[i] += p[i] * a[i][x] * (1 + f[x]), p[i] *= 1 - a[i][x];
}
cout << fixed << setprecision(20) << f[1] << '\n';
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
inline void write(int x) {
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const int N = 1005;
int n;
long double a[N][N], f[N], p[N];
bool vis[N];
int main() {
int i, j, k, x;
read(n);
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j) read(k), a[i][j] = k * 1.0 / 100;
for (i = 1; i <= n; ++i) a[i][i] = 0, p[i] = 1;
f[n] = 0;
vis[n] = 1;
for (i = 1; i < n; ++i)
f[i] += p[i] * a[i][n] * (1 + f[n]), p[i] *= (1 - a[i][n]);
for (k = 1; k < n; ++k) {
x = -1;
for (i = 1; i <= n; ++i)
if (!vis[i]) {
if (fabs(p[i] - 1) <= 1e-8) continue;
if (x == -1)
x = i;
else {
if (f[x] / (1 - p[x]) + 1 / (1 - p[x]) >
f[i] / (1 - p[i]) + 1 / (1 - p[i]))
x = i;
}
}
if (x == -1) continue;
vis[x] = 1, f[x] /= 1 - p[x], f[x] += 1 / (1 - p[x]) - 1;
for (i = 1; i <= n; ++i)
if (!vis[i]) f[i] += p[i] * a[i][x] * (1 + f[x]), p[i] *= 1 - a[i][x];
}
cout << fixed << setprecision(20) << f[1] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int maxN = 1 << 10;
const long double INF = 1e30;
long double dist[maxN], p[maxN], P[maxN][maxN];
bool juz[maxN];
long double eval(int v) {
if (p[v] > 0.99999) return INF;
return (dist[v] + p[v]) / (1.0 - p[v]);
}
void solve() {
int n;
scanf("%d", &n);
for (int i = (1); i < (int)(n + 1); i++)
for (int j = (1); j < (int)(n + 1); j++)
scanf("%Lf", &P[i][j]), P[i][j] /= 100;
fill(p, p + n, 1.0);
dist[0] = INF * 10;
for (int _ = (0); _ < (int)(n); _++) {
int v = 0;
for (int u = (1); u < (int)(n + 1); u++)
if (!juz[u] and eval(u) < eval(v)) v = u;
juz[v] = true;
dist[v] = eval(v);
for (int u = (1); u < (int)(n + 1); u++)
if (!juz[u]) {
dist[u] += (dist[v] + 1.0) * p[u] * P[u][v];
p[u] *= 1.0 - P[u][v];
}
0;
}
printf("%.9Lf\n", dist[1]);
}
int main() {
int t;
t = 1;
while (t--) solve();
}
|
### Prompt
In cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int maxN = 1 << 10;
const long double INF = 1e30;
long double dist[maxN], p[maxN], P[maxN][maxN];
bool juz[maxN];
long double eval(int v) {
if (p[v] > 0.99999) return INF;
return (dist[v] + p[v]) / (1.0 - p[v]);
}
void solve() {
int n;
scanf("%d", &n);
for (int i = (1); i < (int)(n + 1); i++)
for (int j = (1); j < (int)(n + 1); j++)
scanf("%Lf", &P[i][j]), P[i][j] /= 100;
fill(p, p + n, 1.0);
dist[0] = INF * 10;
for (int _ = (0); _ < (int)(n); _++) {
int v = 0;
for (int u = (1); u < (int)(n + 1); u++)
if (!juz[u] and eval(u) < eval(v)) v = u;
juz[v] = true;
dist[v] = eval(v);
for (int u = (1); u < (int)(n + 1); u++)
if (!juz[u]) {
dist[u] += (dist[v] + 1.0) * p[u] * P[u][v];
p[u] *= 1.0 - P[u][v];
}
0;
}
printf("%.9Lf\n", dist[1]);
}
int main() {
int t;
t = 1;
while (t--) solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
const int maxn = 1050;
int p[maxn][maxn];
double a[maxn], b[maxn];
int vis[maxn], inf[maxn], n, m;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &p[i][j]);
for (int i = 1; i <= n - 1; i++) a[i] = b[i] = inf[i] = 1;
for (int ii = 1; ii <= n; ii++) {
int x = 1;
while (vis[x] || inf[x]) {
x++;
if (x > n) break;
}
if (x > n) break;
int u = x;
for (int i = u + 1; i <= n; i++)
if (!vis[i] && !inf[i] &&
(((b[x] / (1.0 - a[x])) > (b[i] / (1.0 - a[i])))))
x = i;
vis[x] = 1;
double trans = b[x] / (1 - a[x]);
for (int i = 1; i <= n; i++)
if (!vis[i]) {
inf[i] = 0;
b[i] += a[i] * p[i][x] * 0.01 * trans;
a[i] *= (1.0 - p[i][x] * 0.01);
}
}
printf("%.12lf\n", b[1] / (1 - a[1]));
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
const int maxn = 1050;
int p[maxn][maxn];
double a[maxn], b[maxn];
int vis[maxn], inf[maxn], n, m;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &p[i][j]);
for (int i = 1; i <= n - 1; i++) a[i] = b[i] = inf[i] = 1;
for (int ii = 1; ii <= n; ii++) {
int x = 1;
while (vis[x] || inf[x]) {
x++;
if (x > n) break;
}
if (x > n) break;
int u = x;
for (int i = u + 1; i <= n; i++)
if (!vis[i] && !inf[i] &&
(((b[x] / (1.0 - a[x])) > (b[i] / (1.0 - a[i])))))
x = i;
vis[x] = 1;
double trans = b[x] / (1 - a[x]);
for (int i = 1; i <= n; i++)
if (!vis[i]) {
inf[i] = 0;
b[i] += a[i] * p[i][x] * 0.01 * trans;
a[i] *= (1.0 - p[i][x] * 0.01);
}
}
printf("%.12lf\n", b[1] / (1 - a[1]));
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2020;
int n, a[N];
bool vis[N];
double p[N][N], f[N], g[N], pr[N];
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = read() * 1.0 / 100;
for (int i = 1; i <= n; i++) pr[i] = f[i] = 1.0;
vis[n] = 1;
a[1] = n;
for (int i = 2; i <= n; i++) {
double mi = 1e18;
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
f[j] += g[a[i - 1]] * p[j][a[i - 1]] * pr[j];
pr[j] *= 1.0 - p[j][a[i - 1]];
if ((g[j] = f[j] / (1.0 - pr[j])) < mi) mi = g[j], a[i] = j;
}
vis[a[i]] = 1;
}
printf("%0.10lf", g[1]);
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2020;
int n, a[N];
bool vis[N];
double p[N][N], f[N], g[N], pr[N];
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = read() * 1.0 / 100;
for (int i = 1; i <= n; i++) pr[i] = f[i] = 1.0;
vis[n] = 1;
a[1] = n;
for (int i = 2; i <= n; i++) {
double mi = 1e18;
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
f[j] += g[a[i - 1]] * p[j][a[i - 1]] * pr[j];
pr[j] *= 1.0 - p[j][a[i - 1]];
if ((g[j] = f[j] / (1.0 - pr[j])) < mi) mi = g[j], a[i] = j;
}
vis[a[i]] = 1;
}
printf("%0.10lf", g[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int read() {
int a = 0;
bool opt = 0;
char c = getchar();
while (c < '0' || c > '9') opt |= c == '-', c = getchar();
while (c >= '0' && c <= '9')
a = (a << 3) + (a << 1) + (c ^ 48), c = getchar();
return opt ? -a : a;
}
int n, num, vis[1005];
double f[1005], t[1005], p[1005][1005], Max;
int main() {
n = read();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
p[i][j] = read() / 100.0;
}
f[i] = t[i] = 1;
}
f[n] = t[n] = 0;
for (int i = 1; i <= n; ++i) {
Max = 1e18;
for (int j = 1; j <= n; ++j) {
if (!vis[j] && f[j] / (1 - t[j]) < Max) {
Max = f[j] / (1 - t[j]);
num = j;
}
}
f[num] = f[num] / (1 - t[num]);
if (num == 1) break;
vis[num] = 1;
for (int j = 1; j <= n; ++j) {
if (!vis[j]) {
f[j] += t[j] * p[j][num] * f[num];
t[j] *= (1 - p[j][num]);
}
}
}
printf("%.10lf\n", f[1]);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int read() {
int a = 0;
bool opt = 0;
char c = getchar();
while (c < '0' || c > '9') opt |= c == '-', c = getchar();
while (c >= '0' && c <= '9')
a = (a << 3) + (a << 1) + (c ^ 48), c = getchar();
return opt ? -a : a;
}
int n, num, vis[1005];
double f[1005], t[1005], p[1005][1005], Max;
int main() {
n = read();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
p[i][j] = read() / 100.0;
}
f[i] = t[i] = 1;
}
f[n] = t[n] = 0;
for (int i = 1; i <= n; ++i) {
Max = 1e18;
for (int j = 1; j <= n; ++j) {
if (!vis[j] && f[j] / (1 - t[j]) < Max) {
Max = f[j] / (1 - t[j]);
num = j;
}
}
f[num] = f[num] / (1 - t[num]);
if (num == 1) break;
vis[num] = 1;
for (int j = 1; j <= n; ++j) {
if (!vis[j]) {
f[j] += t[j] * p[j][num] * f[num];
t[j] *= (1 - p[j][num]);
}
}
}
printf("%.10lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
const int MaxN = 1e3 + 10;
int n, vis[MaxN], a[MaxN];
double p[MaxN][MaxN], d[MaxN], sum[MaxN], pr[MaxN];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int now = 0;
sum[i] = pr[i] = 1.0;
for (int j = 1; j <= n; j++) scanf("%d", &now), p[i][j] = now * 0.01L;
}
vis[n] = 1, a[1] = n, d[0] = 1e18;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
sum[j] += d[a[i - 1]] * p[j][a[i - 1]] * pr[j];
pr[j] *= (1 - p[j][a[i - 1]]), d[j] = sum[j] / (1 - pr[j]);
}
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && d[pos] > d[j]) pos = j;
vis[pos] = 1, a[i] = pos;
}
printf("%.10lf\n", d[1]);
std::cerr << "tiger0132 /qq";
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int MaxN = 1e3 + 10;
int n, vis[MaxN], a[MaxN];
double p[MaxN][MaxN], d[MaxN], sum[MaxN], pr[MaxN];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int now = 0;
sum[i] = pr[i] = 1.0;
for (int j = 1; j <= n; j++) scanf("%d", &now), p[i][j] = now * 0.01L;
}
vis[n] = 1, a[1] = n, d[0] = 1e18;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
sum[j] += d[a[i - 1]] * p[j][a[i - 1]] * pr[j];
pr[j] *= (1 - p[j][a[i - 1]]), d[j] = sum[j] / (1 - pr[j]);
}
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && d[pos] > d[j]) pos = j;
vis[pos] = 1, a[i] = pos;
}
printf("%.10lf\n", d[1]);
std::cerr << "tiger0132 /qq";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
template <class T>
void read(T &x) {
x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
}
void file() {}
double p[N][N];
int n;
int a[N];
void init() {
read(n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) read(p[i][j]), p[i][j] /= 100;
}
double f[N], pre[N];
int vis[N];
void solve() {
a[1] = n;
vis[n] = 1;
for (int i = 1; i <= n - 1; ++i) f[i] = 1, pre[i] = 1;
for (int i = 2; i <= n; ++i) {
int u = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j])
f[j] += pre[j] * p[j][a[i - 1]] * f[a[i - 1]],
u = j, pre[j] *= (1 - p[j][a[i - 1]]);
for (int j = 1; j <= n; ++j)
if (!vis[j])
if (f[j] / (1 - pre[j]) < f[u] / (1 - pre[u])) u = j;
a[i] = u;
f[u] /= (1 - pre[u]), vis[u] = 1;
}
printf("%.7lf\n", f[1]);
}
int main() {
file();
init();
solve();
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
template <class T>
void read(T &x) {
x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
}
void file() {}
double p[N][N];
int n;
int a[N];
void init() {
read(n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) read(p[i][j]), p[i][j] /= 100;
}
double f[N], pre[N];
int vis[N];
void solve() {
a[1] = n;
vis[n] = 1;
for (int i = 1; i <= n - 1; ++i) f[i] = 1, pre[i] = 1;
for (int i = 2; i <= n; ++i) {
int u = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j])
f[j] += pre[j] * p[j][a[i - 1]] * f[a[i - 1]],
u = j, pre[j] *= (1 - p[j][a[i - 1]]);
for (int j = 1; j <= n; ++j)
if (!vis[j])
if (f[j] / (1 - pre[j]) < f[u] / (1 - pre[u])) u = j;
a[i] = u;
f[u] /= (1 - pre[u]), vis[u] = 1;
}
printf("%.7lf\n", f[1]);
}
int main() {
file();
init();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double Inf = 1e50;
const int Maxn = 1020;
double d[Maxn];
bool done[Maxn];
int n;
int pro[Maxn][Maxn];
double nowp[Maxn], nowval[Maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) scanf("%d", &pro[i][j]);
}
for (int i = 1; i <= n; i++) d[i] = Inf;
for (int i = 1; i <= n; i++) nowp[i] = 1;
d[n] = 0;
for (int i = 1; i <= n; i++) {
int cs = -1;
for (int j = 1; j <= n; j++) {
if (done[j]) continue;
if (cs == -1 || d[j] < d[cs]) cs = j;
}
done[cs] = 1;
for (int j = 1; j <= n; j++) {
if (done[j]) continue;
nowval[j] += nowp[j] * pro[j][cs] / 100.0 * (d[cs] + 1.0);
nowp[j] *= 1.0 - pro[j][cs] / 100.0;
d[j] = (nowval[j] + nowp[j]) / (1.0 - nowp[j]);
}
}
printf("%.12f\n", d[1]);
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double Inf = 1e50;
const int Maxn = 1020;
double d[Maxn];
bool done[Maxn];
int n;
int pro[Maxn][Maxn];
double nowp[Maxn], nowval[Maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) scanf("%d", &pro[i][j]);
}
for (int i = 1; i <= n; i++) d[i] = Inf;
for (int i = 1; i <= n; i++) nowp[i] = 1;
d[n] = 0;
for (int i = 1; i <= n; i++) {
int cs = -1;
for (int j = 1; j <= n; j++) {
if (done[j]) continue;
if (cs == -1 || d[j] < d[cs]) cs = j;
}
done[cs] = 1;
for (int j = 1; j <= n; j++) {
if (done[j]) continue;
nowval[j] += nowp[j] * pro[j][cs] / 100.0 * (d[cs] + 1.0);
nowp[j] *= 1.0 - pro[j][cs] / 100.0;
d[j] = (nowval[j] + nowp[j]) / (1.0 - nowp[j]);
}
}
printf("%.12f\n", d[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, i, j;
double f[1002], p[1002][1002], p1[1002];
bool vis[1002];
int read() {
char c = getchar();
int w = 0;
while (c < '0' || c > '9') c = getchar();
while (c <= '9' && c >= '0') {
w = w * 10 + c - '0';
c = getchar();
}
return w;
}
int main() {
n = read();
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
int x = read();
p[i][j] = 0.01 * x;
}
}
for (i = 1; i <= n; i++) p1[i] = 1;
p1[n] = 0;
for (i = 1; i <= n; i++) {
double minx = 1e20;
int id;
for (j = 1; j <= n; j++) {
if (!vis[j] && (f[j] + p1[j]) / (1 - p1[j]) < minx)
minx = (f[j] + p1[j]) / (1 - p1[j]), id = j;
}
vis[id] = 1;
f[id] = minx;
if (id == 1) {
printf("%.10lf\n", minx);
break;
}
for (j = 1; j <= n; j++) {
if (!vis[j])
f[j] += p[j][id] * p1[j] * (f[id] + 1), p1[j] *= (1 - p[j][id]);
}
}
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, i, j;
double f[1002], p[1002][1002], p1[1002];
bool vis[1002];
int read() {
char c = getchar();
int w = 0;
while (c < '0' || c > '9') c = getchar();
while (c <= '9' && c >= '0') {
w = w * 10 + c - '0';
c = getchar();
}
return w;
}
int main() {
n = read();
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
int x = read();
p[i][j] = 0.01 * x;
}
}
for (i = 1; i <= n; i++) p1[i] = 1;
p1[n] = 0;
for (i = 1; i <= n; i++) {
double minx = 1e20;
int id;
for (j = 1; j <= n; j++) {
if (!vis[j] && (f[j] + p1[j]) / (1 - p1[j]) < minx)
minx = (f[j] + p1[j]) / (1 - p1[j]), id = j;
}
vis[id] = 1;
f[id] = minx;
if (id == 1) {
printf("%.10lf\n", minx);
break;
}
for (j = 1; j <= n; j++) {
if (!vis[j])
f[j] += p[j][id] * p1[j] * (f[id] + 1), p1[j] *= (1 - p[j][id]);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool checkMax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMax(T &a, const Args... arg) {
checkMax(a, max(arg...));
}
template <class T>
inline bool checkMin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMin(T &a, const Args... arg) {
checkMin(a, min(arg...));
}
const int INF = 0x3f3f3f3f;
const long long llINF = 1e18;
const int MAXN = 1e3 + 5;
int n;
int vis[MAXN], a[MAXN];
double p[MAXN][MAXN], E[MAXN], prod[MAXN];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100;
if (n == 1) {
printf("0\n");
return 0;
}
for (int i = 1; i <= n; i++) E[i] = 1, prod[i] = 1 - p[i][n];
vis[n] = 1;
a[1] = n;
for (int i = 1; i <= n; i++) {
double minval = llINF;
int pos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && E[j] / (1 - prod[j]) < minval) {
pos = j;
minval = E[j] / (1 - prod[j]);
}
}
vis[pos] = 1;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - prod[pos]) * p[j][pos] * prod[j],
prod[j] *= (1 - p[j][pos]);
}
printf("%.8f\n", E[1] / (1 - prod[1]));
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool checkMax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMax(T &a, const Args... arg) {
checkMax(a, max(arg...));
}
template <class T>
inline bool checkMin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMin(T &a, const Args... arg) {
checkMin(a, min(arg...));
}
const int INF = 0x3f3f3f3f;
const long long llINF = 1e18;
const int MAXN = 1e3 + 5;
int n;
int vis[MAXN], a[MAXN];
double p[MAXN][MAXN], E[MAXN], prod[MAXN];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100;
if (n == 1) {
printf("0\n");
return 0;
}
for (int i = 1; i <= n; i++) E[i] = 1, prod[i] = 1 - p[i][n];
vis[n] = 1;
a[1] = n;
for (int i = 1; i <= n; i++) {
double minval = llINF;
int pos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && E[j] / (1 - prod[j]) < minval) {
pos = j;
minval = E[j] / (1 - prod[j]);
}
}
vis[pos] = 1;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - prod[pos]) * p[j][pos] * prod[j],
prod[j] *= (1 - p[j][pos]);
}
printf("%.8f\n", E[1] / (1 - prod[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
long double p[1005][1005];
long double E[1005];
long double prob[1005];
bool bio[1005];
int pp[1005][1005];
int main() {
cin >> n;
for (int i = (int)(0); i < (int)(n); ++i)
for (int j = (int)(0); j < (int)(n); ++j)
scanf("%d", pp[i] + j), p[i][j] = pp[i][j] / 100.0;
vector<int> V = {n - 1};
vector<int> V2;
for (int i = (int)(0); i < (int)(n - 1); ++i) V2.push_back(i);
bio[n - 1] = true;
for (int i = (int)(0); i < (int)(n); ++i) {
if (bio[i]) continue;
E[i] = 1;
prob[i] = 1;
prob[i] *= 1 - p[i][n - 1];
}
while (!V2.empty()) {
int id = -1;
long double cal = 1e9;
for (int i = (int)(0); i < (int)(V2.size()); ++i) {
if (prob[V2[i]] > 0.999) continue;
long double cc = E[V2[i]] / (1 - prob[V2[i]]);
if (cc < cal) cal = cc, id = i;
}
if (id == -1) break;
int first = V2[id];
V2.erase(V2.begin() + id);
bio[first] = true;
E[first] /= 1 - prob[first];
for (int i = (int)(0); i < (int)(n); ++i) {
if (bio[i]) continue;
if (pp[i][first] == 0) continue;
E[i] += prob[i] * p[i][first] * E[first];
prob[i] *= 1 - p[i][first];
}
V.push_back(first);
}
printf("%.15lf\n", (double)E[0]);
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
long double p[1005][1005];
long double E[1005];
long double prob[1005];
bool bio[1005];
int pp[1005][1005];
int main() {
cin >> n;
for (int i = (int)(0); i < (int)(n); ++i)
for (int j = (int)(0); j < (int)(n); ++j)
scanf("%d", pp[i] + j), p[i][j] = pp[i][j] / 100.0;
vector<int> V = {n - 1};
vector<int> V2;
for (int i = (int)(0); i < (int)(n - 1); ++i) V2.push_back(i);
bio[n - 1] = true;
for (int i = (int)(0); i < (int)(n); ++i) {
if (bio[i]) continue;
E[i] = 1;
prob[i] = 1;
prob[i] *= 1 - p[i][n - 1];
}
while (!V2.empty()) {
int id = -1;
long double cal = 1e9;
for (int i = (int)(0); i < (int)(V2.size()); ++i) {
if (prob[V2[i]] > 0.999) continue;
long double cc = E[V2[i]] / (1 - prob[V2[i]]);
if (cc < cal) cal = cc, id = i;
}
if (id == -1) break;
int first = V2[id];
V2.erase(V2.begin() + id);
bio[first] = true;
E[first] /= 1 - prob[first];
for (int i = (int)(0); i < (int)(n); ++i) {
if (bio[i]) continue;
if (pp[i][first] == 0) continue;
E[i] += prob[i] * p[i][first] * E[first];
prob[i] *= 1 - p[i][first];
}
V.push_back(first);
}
printf("%.15lf\n", (double)E[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
const int N = 2005;
double p[N][N], dis[N], pre[N];
int n, vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%lf", &p[i][j]), p[i][j] = p[i][j] / 100;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i < n; i++) dis[i] = 1, pre[i] = 1 - p[i][n];
vis[n] = 1;
for (int i = 1; i < n; i++) {
double Min = 1000000000;
int u = 0;
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
if (dis[j] / (1 - pre[j]) < Min) Min = dis[j] / (1 - pre[j]), u = j;
}
if (u == 1) {
printf("%.7lf", Min);
return 0;
}
vis[u] = 1;
for (int j = 1; j <= n; j++)
if (!vis[j])
dis[j] = dis[j] + Min * pre[j] * p[j][u],
pre[j] = pre[j] * (1 - p[j][u]);
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 2005;
double p[N][N], dis[N], pre[N];
int n, vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%lf", &p[i][j]), p[i][j] = p[i][j] / 100;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i < n; i++) dis[i] = 1, pre[i] = 1 - p[i][n];
vis[n] = 1;
for (int i = 1; i < n; i++) {
double Min = 1000000000;
int u = 0;
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
if (dis[j] / (1 - pre[j]) < Min) Min = dis[j] / (1 - pre[j]), u = j;
}
if (u == 1) {
printf("%.7lf", Min);
return 0;
}
vis[u] = 1;
for (int j = 1; j <= n; j++)
if (!vis[j])
dis[j] = dis[j] + Min * pre[j] * p[j][u],
pre[j] = pre[j] * (1 - p[j][u]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
long long mypow(long long _a, long long _x, long long _mod) {
if (_x == 0) return 1ll;
long long _tmp = mypow(_a, _x / 2, _mod);
_tmp = (_tmp * _tmp) % _mod;
if (_x & 1) _tmp = (_tmp * _a) % _mod;
return _tmp;
}
inline long long add(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x + _y;
if (_ >= _mod) _ -= _mod;
return _;
}
inline long long sub(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x - _y;
if (_ < 0) _ += _mod;
return _;
}
inline long long mul(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x * _y;
if (_ >= _mod) _ %= _mod;
return _;
}
inline bool equal(double _x, double _y) {
return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, cs;
int n;
double p[1021][1021], a[1021], b[1021], ans[1021];
void build() {}
void init() {
n = getint();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = getint() / 100.0;
for (int i = 1; i <= n; i++) a[i] = 0, b[i] = 1;
}
bool got[1021];
inline double ep(int idx) { return (a[idx] + b[idx]) / (1.0 - b[idx]); }
void solve() {
a[n] = b[n] = 0;
for (int i = 1; i <= n; i++) {
double bst = 1.0 / 0.0;
int bsti = -1;
for (int j = 1; j <= n; j++)
if (!got[j]) {
if (ep(j) < bst) bst = ep(j), bsti = j;
}
ans[bsti] = ep(bsti);
got[bsti] = true;
for (int j = 1; j <= n; j++)
if (!got[j]) {
a[j] += b[j] * p[j][bsti] * (ans[bsti] + 1.0);
b[j] *= (1.0 - p[j][bsti]);
}
}
printf("%.12f\n", ans[1]);
}
int main() {
build();
while (__--) {
init();
solve();
}
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
long long mypow(long long _a, long long _x, long long _mod) {
if (_x == 0) return 1ll;
long long _tmp = mypow(_a, _x / 2, _mod);
_tmp = (_tmp * _tmp) % _mod;
if (_x & 1) _tmp = (_tmp * _a) % _mod;
return _tmp;
}
inline long long add(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x + _y;
if (_ >= _mod) _ -= _mod;
return _;
}
inline long long sub(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x - _y;
if (_ < 0) _ += _mod;
return _;
}
inline long long mul(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x * _y;
if (_ >= _mod) _ %= _mod;
return _;
}
inline bool equal(double _x, double _y) {
return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, cs;
int n;
double p[1021][1021], a[1021], b[1021], ans[1021];
void build() {}
void init() {
n = getint();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = getint() / 100.0;
for (int i = 1; i <= n; i++) a[i] = 0, b[i] = 1;
}
bool got[1021];
inline double ep(int idx) { return (a[idx] + b[idx]) / (1.0 - b[idx]); }
void solve() {
a[n] = b[n] = 0;
for (int i = 1; i <= n; i++) {
double bst = 1.0 / 0.0;
int bsti = -1;
for (int j = 1; j <= n; j++)
if (!got[j]) {
if (ep(j) < bst) bst = ep(j), bsti = j;
}
ans[bsti] = ep(bsti);
got[bsti] = true;
for (int j = 1; j <= n; j++)
if (!got[j]) {
a[j] += b[j] * p[j][bsti] * (ans[bsti] + 1.0);
b[j] *= (1.0 - p[j][bsti]);
}
}
printf("%.12f\n", ans[1]);
}
int main() {
build();
while (__--) {
init();
solve();
}
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma G++ optimize(2)
using namespace std;
char nc() { return getchar(); }
int read() {
int x = 0, y = 1;
char c = nc();
while (!isdigit(c)) {
if (c == '-') y = -1;
c = nc();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = nc();
}
return x * y;
}
int n, vst[1005];
double p[1005][1005], a[1005], b[1005];
void find(int &id, double &mn) {
mn = (1e100);
for (int i = 1; i <= n; i++)
if (!vst[i]) {
double tp = a[i] / (1.0 - b[i]);
if (tp < mn) mn = tp, id = i;
}
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = read(), p[i][j] *= 0.01;
for (int i = 1; i <= n; i++) a[i] = b[i] = 1;
if (n == 1) return puts("0"), 0;
int id = n;
double mn = 0;
vst[n] = 1;
for (int i = 1; i < n; i++) {
a[i] += b[i] * p[i][id] * mn;
b[i] *= (1 - p[i][id]);
}
while (true) {
find(id, mn);
if (id == 1) return cout << fixed << setprecision(15) << mn, 0;
vst[id] = 1;
for (int i = 1; i <= n; i++)
if (!vst[i]) {
a[i] += b[i] * p[i][id] * mn;
b[i] *= (1.0 - p[i][id]);
}
}
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma G++ optimize(2)
using namespace std;
char nc() { return getchar(); }
int read() {
int x = 0, y = 1;
char c = nc();
while (!isdigit(c)) {
if (c == '-') y = -1;
c = nc();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = nc();
}
return x * y;
}
int n, vst[1005];
double p[1005][1005], a[1005], b[1005];
void find(int &id, double &mn) {
mn = (1e100);
for (int i = 1; i <= n; i++)
if (!vst[i]) {
double tp = a[i] / (1.0 - b[i]);
if (tp < mn) mn = tp, id = i;
}
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = read(), p[i][j] *= 0.01;
for (int i = 1; i <= n; i++) a[i] = b[i] = 1;
if (n == 1) return puts("0"), 0;
int id = n;
double mn = 0;
vst[n] = 1;
for (int i = 1; i < n; i++) {
a[i] += b[i] * p[i][id] * mn;
b[i] *= (1 - p[i][id]);
}
while (true) {
find(id, mn);
if (id == 1) return cout << fixed << setprecision(15) << mn, 0;
vst[id] = 1;
for (int i = 1; i <= n; i++)
if (!vst[i]) {
a[i] += b[i] * p[i][id] * mn;
b[i] *= (1.0 - p[i][id]);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 400005;
long long n, d[1005], vis[1005] = {0};
double p[1005][1005], ans[1005], sum[1005], mul[1005];
int main() {
ios::sync_with_stdio(false);
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] = p[i][j] / 100.0;
}
}
vis[n] = 1;
d[1] = n;
ans[1] = 0.0;
for (long long i = 1; i <= n; i++) sum[i] = 1.0, mul[i] = 1.0;
for (long long i = 2; i <= n; i++) {
long long id = 0;
for (long long j = 1; j <= n; j++) {
if (vis[j]) continue;
sum[j] += mul[j] * ans[d[i - 1]] * p[j][d[i - 1]];
mul[j] *= (1.0 - p[j][d[i - 1]]);
ans[j] = sum[j] / (1.0 - mul[j]);
if (!id || ans[id] > ans[j]) id = j;
}
d[i] = id;
vis[id] = 1;
}
printf("%.12lf\n", ans[1]);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 400005;
long long n, d[1005], vis[1005] = {0};
double p[1005][1005], ans[1005], sum[1005], mul[1005];
int main() {
ios::sync_with_stdio(false);
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] = p[i][j] / 100.0;
}
}
vis[n] = 1;
d[1] = n;
ans[1] = 0.0;
for (long long i = 1; i <= n; i++) sum[i] = 1.0, mul[i] = 1.0;
for (long long i = 2; i <= n; i++) {
long long id = 0;
for (long long j = 1; j <= n; j++) {
if (vis[j]) continue;
sum[j] += mul[j] * ans[d[i - 1]] * p[j][d[i - 1]];
mul[j] *= (1.0 - p[j][d[i - 1]]);
ans[j] = sum[j] / (1.0 - mul[j]);
if (!id || ans[id] > ans[j]) id = j;
}
d[i] = id;
vis[id] = 1;
}
printf("%.12lf\n", ans[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
inline int gi() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
int n;
double p[maxn][maxn], dis[maxn], P[maxn];
int vis[maxn];
int main() {
n = gi();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = gi() / 100.;
fill(P + 1, P + n + 1, 1);
dis[n] = P[n] = 0;
for (int i = 1; i <= n; ++i) {
int u = -1;
for (int j = 1; j <= n; ++j)
if (!vis[j] && (u == -1 || (dis[u] + P[u]) / (1 - P[u]) >
(dis[j] + P[j]) / (1 - P[j])))
u = j;
vis[u] = 1;
dis[u] = (dis[u] + P[u]) / (1 - P[u]);
if (u == 1) break;
for (int j = 1; j <= n; ++j)
if (!vis[j]) dis[j] += P[j] * p[j][u] * (dis[u] + 1), P[j] *= 1 - p[j][u];
}
printf("%.15lf", dis[1]);
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
inline int gi() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
int n;
double p[maxn][maxn], dis[maxn], P[maxn];
int vis[maxn];
int main() {
n = gi();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = gi() / 100.;
fill(P + 1, P + n + 1, 1);
dis[n] = P[n] = 0;
for (int i = 1; i <= n; ++i) {
int u = -1;
for (int j = 1; j <= n; ++j)
if (!vis[j] && (u == -1 || (dis[u] + P[u]) / (1 - P[u]) >
(dis[j] + P[j]) / (1 - P[j])))
u = j;
vis[u] = 1;
dis[u] = (dis[u] + P[u]) / (1 - P[u]);
if (u == 1) break;
for (int j = 1; j <= n; ++j)
if (!vis[j]) dis[j] += P[j] * p[j][u] * (dis[u] + 1), P[j] *= 1 - p[j][u];
}
printf("%.15lf", dis[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
register long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return (f == 1) ? x : -x;
}
inline void file() {
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
}
int n;
double f[1010], P[1010], p[1010][1010];
bool vis[1010];
signed main() {
n = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
p[i][j] = 1.0 * read() / 100.0;
}
}
f[n] = 0, P[n] = 0, vis[n] = 1;
for (int i = 1; i < n; i++) f[i] = 1, P[i] = 1.0 - p[i][n];
for (int i = 1; i <= n; i++) {
int k = 0;
double mi = 1e18;
for (int j = 1; j <= n; j++) {
if (!vis[j] && f[j] / (1.0 - P[j]) < mi) k = j, mi = f[j] / (1.0 - P[j]);
}
vis[k] = 1;
if (k == 1) break;
for (int j = 1; j <= n; j++) {
if (!vis[j])
f[j] += (f[k] / (1.0 - P[k])) * p[j][k] * P[j], P[j] *= (1.0 - p[j][k]);
}
}
printf("%.10lf\n", f[1] / (1.0 - P[1]));
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
register long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return (f == 1) ? x : -x;
}
inline void file() {
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
}
int n;
double f[1010], P[1010], p[1010][1010];
bool vis[1010];
signed main() {
n = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
p[i][j] = 1.0 * read() / 100.0;
}
}
f[n] = 0, P[n] = 0, vis[n] = 1;
for (int i = 1; i < n; i++) f[i] = 1, P[i] = 1.0 - p[i][n];
for (int i = 1; i <= n; i++) {
int k = 0;
double mi = 1e18;
for (int j = 1; j <= n; j++) {
if (!vis[j] && f[j] / (1.0 - P[j]) < mi) k = j, mi = f[j] / (1.0 - P[j]);
}
vis[k] = 1;
if (k == 1) break;
for (int j = 1; j <= n; j++) {
if (!vis[j])
f[j] += (f[k] / (1.0 - P[k])) * p[j][k] * P[j], P[j] *= (1.0 - p[j][k]);
}
}
printf("%.10lf\n", f[1] / (1.0 - P[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
int p[1013][1013];
bool done[1013];
double ans[1013];
double tot[1013];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) {
ans[i] = 1 / 0.;
tot[i] = 1;
}
ans[n - 1] = 0;
for (int r = 0; r < n; r++) {
int i = -1;
for (int j = 0; j < n; j++)
if (!done[j] && (i == -1 || ans[j] < ans[i])) i = j;
done[i] = 1;
for (int j = 0; j < n; j++)
if (!done[j] && p[j][i]) {
ans[j] = (ans[j] == 1 / 0.) ? 1 : ans[j] * (1 - tot[j]);
ans[j] += tot[j] * p[j][i] / 100. * ans[i];
tot[j] *= (1 - p[j][i] / 100.);
ans[j] /= (1 - tot[j]);
}
}
printf("%1.9f\n", ans[0]);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int p[1013][1013];
bool done[1013];
double ans[1013];
double tot[1013];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) {
ans[i] = 1 / 0.;
tot[i] = 1;
}
ans[n - 1] = 0;
for (int r = 0; r < n; r++) {
int i = -1;
for (int j = 0; j < n; j++)
if (!done[j] && (i == -1 || ans[j] < ans[i])) i = j;
done[i] = 1;
for (int j = 0; j < n; j++)
if (!done[j] && p[j][i]) {
ans[j] = (ans[j] == 1 / 0.) ? 1 : ans[j] * (1 - tot[j]);
ans[j] += tot[j] * p[j][i] / 100. * ans[i];
tot[j] *= (1 - p[j][i] / 100.);
ans[j] /= (1 - tot[j]);
}
}
printf("%1.9f\n", ans[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
double f[N], c[N];
int a[N][N], n;
int d[N], l, r, vis[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
}
c[i] = 1;
}
d[++r] = n;
vis[n] = 1;
for (int i = 1; i <= n; i++) {
int x = d[i], z = 0;
double nx = 1e20;
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
f[j] += f[x] * c[j] * a[j][x] * 0.01;
c[j] *= 1 - a[j][x] * 0.01;
if ((f[j] + 1) / (1 - c[j]) < nx) nx = (f[j] + 1) / (1 - c[j]), z = j;
}
f[z] = (f[z] + 1) / (1 - c[z]);
d[++r] = z;
vis[z] = 1;
}
printf("%.12lf\n", f[1]);
}
|
### Prompt
Please formulate a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
double f[N], c[N];
int a[N][N], n;
int d[N], l, r, vis[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
}
c[i] = 1;
}
d[++r] = n;
vis[n] = 1;
for (int i = 1; i <= n; i++) {
int x = d[i], z = 0;
double nx = 1e20;
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
f[j] += f[x] * c[j] * a[j][x] * 0.01;
c[j] *= 1 - a[j][x] * 0.01;
if ((f[j] + 1) / (1 - c[j]) < nx) nx = (f[j] + 1) / (1 - c[j]), z = j;
}
f[z] = (f[z] + 1) / (1 - c[z]);
d[++r] = z;
vis[z] = 1;
}
printf("%.12lf\n", f[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int nega = 1;
while (!isdigit(ch)) {
if (ch == '-') nega = -1;
ch = getchar();
}
int ans = 0;
while (isdigit(ch)) {
ans = ans * 10 + ch - 48;
ch = getchar();
}
if (nega == -1) return -ans;
return ans;
}
inline int min(int x, int y, int z) { return min(x, min(y, z)); }
inline int max(int x, int y, int z) { return max(x, max(y, z)); }
inline int add(int x, int y) {
return x + y >= 998244353 ? x + y - 998244353 : x + y;
}
inline int add(int x, int y, int z) { return add(add(x, y), z); }
inline int sub(int x, int y) { return x - y < 0 ? x - y + 998244353 : x - y; }
inline int mul(int x, int y) { return 1LL * x * y % 998244353; }
inline int mul(int x, int y, int z) { return mul(mul(x, y), z); }
int n;
double p[1005][1005];
double dis[1005];
double sum[1005];
bool vis[1005];
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) p[i][j] = read() / 100.0;
}
for (int i = 1; i <= n; i++) dis[i] = 1, sum[i] = 1 - p[i][n];
dis[n] = 0;
vis[n] = 1;
for (int i = 1; i <= n; i++) {
int u = 0;
double v = 0x3f3f3f3f;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (dis[j] / (1 - sum[j])) < v)
v = (dis[j] / (1 - sum[j])), u = j;
}
if (!u) break;
vis[u] = 1;
dis[u] /= (1 - sum[u]);
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
dis[j] += dis[u] * p[j][u] * sum[j];
sum[j] *= (1 - p[j][u]);
}
}
printf("%.7lf\n", dis[1]);
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int nega = 1;
while (!isdigit(ch)) {
if (ch == '-') nega = -1;
ch = getchar();
}
int ans = 0;
while (isdigit(ch)) {
ans = ans * 10 + ch - 48;
ch = getchar();
}
if (nega == -1) return -ans;
return ans;
}
inline int min(int x, int y, int z) { return min(x, min(y, z)); }
inline int max(int x, int y, int z) { return max(x, max(y, z)); }
inline int add(int x, int y) {
return x + y >= 998244353 ? x + y - 998244353 : x + y;
}
inline int add(int x, int y, int z) { return add(add(x, y), z); }
inline int sub(int x, int y) { return x - y < 0 ? x - y + 998244353 : x - y; }
inline int mul(int x, int y) { return 1LL * x * y % 998244353; }
inline int mul(int x, int y, int z) { return mul(mul(x, y), z); }
int n;
double p[1005][1005];
double dis[1005];
double sum[1005];
bool vis[1005];
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) p[i][j] = read() / 100.0;
}
for (int i = 1; i <= n; i++) dis[i] = 1, sum[i] = 1 - p[i][n];
dis[n] = 0;
vis[n] = 1;
for (int i = 1; i <= n; i++) {
int u = 0;
double v = 0x3f3f3f3f;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (dis[j] / (1 - sum[j])) < v)
v = (dis[j] / (1 - sum[j])), u = j;
}
if (!u) break;
vis[u] = 1;
dis[u] /= (1 - sum[u]);
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
dis[j] += dis[u] * p[j][u] * sum[j];
sum[j] *= (1 - p[j][u]);
}
}
printf("%.7lf\n", dis[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, vis[N];
double p[N][N], A[N], B[N];
double dis(int u) { return B[u] < 1 ? A[u] / (1 - B[u]) : 1e9; }
int main() {
cin >> n;
for (int i = (1); i <= (n); i++)
for (int j = (1); j <= (n); j++) scanf("%lf", &p[i][j]), p[i][j] /= 100;
for (int i = (0); i <= (n); i++) A[i] = 1, B[i] = 1;
A[n] = B[n] = 0;
for (int i = (1); i <= (n); i++) {
int u = 0;
for (int j = (1); j <= (n); j++)
if (!vis[j] && dis(j) < dis(u)) u = j;
vis[u] = 1;
for (int j = (1); j <= (n); j++)
if (!vis[j]) A[j] += p[j][u] * dis(u) * B[j], B[j] *= 1 - p[j][u];
}
printf("%.15lf\n", dis(1));
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, vis[N];
double p[N][N], A[N], B[N];
double dis(int u) { return B[u] < 1 ? A[u] / (1 - B[u]) : 1e9; }
int main() {
cin >> n;
for (int i = (1); i <= (n); i++)
for (int j = (1); j <= (n); j++) scanf("%lf", &p[i][j]), p[i][j] /= 100;
for (int i = (0); i <= (n); i++) A[i] = 1, B[i] = 1;
A[n] = B[n] = 0;
for (int i = (1); i <= (n); i++) {
int u = 0;
for (int j = (1); j <= (n); j++)
if (!vis[j] && dis(j) < dis(u)) u = j;
vis[u] = 1;
for (int j = (1); j <= (n); j++)
if (!vis[j]) A[j] += p[j][u] * dis(u) * B[j], B[j] *= 1 - p[j][u];
}
printf("%.15lf\n", dis(1));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1505 * 1505, df = 1505;
double a[df], b[df], dis[df][df], sum[df][df];
long long n, vis[df];
inline long long read() {
long long x = 0, y = 1;
char ch = getchar();
while (ch > '9' || ch < '0') y = (ch == '-') ? -1 : 1, ch = getchar();
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * y;
}
void upd(long long x, long long y) {
for (long long i = (1); i <= (n); ++i) {
if (!vis[i]) {
a[i] += (a[y] / b[y]) * dis[i][y] * sum[i][x - 1];
b[i] += -sum[i][x] + sum[i][x - 1];
}
}
}
void dij() {
for (long long i = (1); i <= (n); ++i) {
long long ty = 0;
double mn = inf;
for (long long j = (1); j <= (n); ++j) {
if (!vis[j]) {
if (a[j] / b[j] < mn) mn = a[j] / b[j], ty = j;
}
}
vis[ty] = 1;
for (long long j = (1); j <= (n); ++j) {
if (!vis[j]) sum[j][i] = sum[j][i - 1] * (1.000000 - dis[j][ty]);
}
upd(i, ty);
}
return;
}
int main() {
n = read();
for (long long i = (1); i <= (n); ++i)
for (long long j = (1); j <= (n); ++j)
dis[i][j] = 1.0000 * read() / (1.0000 * 100);
for (long long i = (1); i <= (n); ++i) sum[i][0] = 1.0000;
for (long long i = (1); i <= (n); ++i) a[i] = 1, b[i] = 0;
a[n] = 0, b[n] = 1;
dij();
printf("%.7lf", a[1] / b[1]);
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1505 * 1505, df = 1505;
double a[df], b[df], dis[df][df], sum[df][df];
long long n, vis[df];
inline long long read() {
long long x = 0, y = 1;
char ch = getchar();
while (ch > '9' || ch < '0') y = (ch == '-') ? -1 : 1, ch = getchar();
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * y;
}
void upd(long long x, long long y) {
for (long long i = (1); i <= (n); ++i) {
if (!vis[i]) {
a[i] += (a[y] / b[y]) * dis[i][y] * sum[i][x - 1];
b[i] += -sum[i][x] + sum[i][x - 1];
}
}
}
void dij() {
for (long long i = (1); i <= (n); ++i) {
long long ty = 0;
double mn = inf;
for (long long j = (1); j <= (n); ++j) {
if (!vis[j]) {
if (a[j] / b[j] < mn) mn = a[j] / b[j], ty = j;
}
}
vis[ty] = 1;
for (long long j = (1); j <= (n); ++j) {
if (!vis[j]) sum[j][i] = sum[j][i - 1] * (1.000000 - dis[j][ty]);
}
upd(i, ty);
}
return;
}
int main() {
n = read();
for (long long i = (1); i <= (n); ++i)
for (long long j = (1); j <= (n); ++j)
dis[i][j] = 1.0000 * read() / (1.0000 * 100);
for (long long i = (1); i <= (n); ++i) sum[i][0] = 1.0000;
for (long long i = (1); i <= (n); ++i) a[i] = 1, b[i] = 0;
a[n] = 0, b[n] = 1;
dij();
printf("%.7lf", a[1] / b[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long nr = 2010;
inline long long read() {
long long ret = 0;
long long x = 1;
char ch;
ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') x = -1;
ch = getchar();
}
while (isdigit(ch)) {
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * x;
}
double p[nr][nr];
long long n;
double g[nr];
bool been[nr] = {0};
double BAN[nr];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
long long x;
cin >> x;
p[i][j] = 1.0 * x * 0.01;
}
}
for (int i = 1; i <= n; i++) {
g[i] = 1;
BAN[i] = 1 - p[i][n];
}
g[n] = 0;
been[n] = 1;
if (n == 1) {
printf("%.10lf\n", 0);
return 0;
}
for (int tim = 1; tim <= n; tim++) {
double minn = 1e18;
long long id = 0;
for (int i = 1; i <= n; i++) {
if (been[i] == 0) {
if (g[i] / (1 - BAN[i]) < minn) {
minn = g[i] / (1 - BAN[i]);
id = i;
}
}
}
if (id == 1) break;
been[id] = 1;
for (int j = 1; j <= n; j++) {
g[j] += g[id] / (1 - BAN[id]) * p[j][id] * BAN[j];
BAN[j] *= (1 - p[j][id]);
}
}
printf("%.10lf\n", g[1] / (1 - BAN[1]));
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long nr = 2010;
inline long long read() {
long long ret = 0;
long long x = 1;
char ch;
ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') x = -1;
ch = getchar();
}
while (isdigit(ch)) {
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * x;
}
double p[nr][nr];
long long n;
double g[nr];
bool been[nr] = {0};
double BAN[nr];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
long long x;
cin >> x;
p[i][j] = 1.0 * x * 0.01;
}
}
for (int i = 1; i <= n; i++) {
g[i] = 1;
BAN[i] = 1 - p[i][n];
}
g[n] = 0;
been[n] = 1;
if (n == 1) {
printf("%.10lf\n", 0);
return 0;
}
for (int tim = 1; tim <= n; tim++) {
double minn = 1e18;
long long id = 0;
for (int i = 1; i <= n; i++) {
if (been[i] == 0) {
if (g[i] / (1 - BAN[i]) < minn) {
minn = g[i] / (1 - BAN[i]);
id = i;
}
}
}
if (id == 1) break;
been[id] = 1;
for (int j = 1; j <= n; j++) {
g[j] += g[id] / (1 - BAN[id]) * p[j][id] * BAN[j];
BAN[j] *= (1 - p[j][id]);
}
}
printf("%.10lf\n", g[1] / (1 - BAN[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double inf = 1e15;
int n, p[1050][1050];
double val[1050], A[1050], B[1050];
bool vis[1050];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &p[i][j]);
for (int i = 1; i <= n - 1; i++) val[i] = inf;
val[n] = 0;
for (int i = 1; i <= n - 1; i++) A[i] = 1, B[i] = 1;
while (!vis[1]) {
double mn = inf;
int k;
for (int i = 1; i <= n; i++)
if (!vis[i] && val[i] < mn) mn = val[i], k = i;
vis[k] = 1;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
B[i] += A[i] * (p[i][k] / 100.0) * val[k];
A[i] *= (1 - p[i][k] / 100.0);
val[i] = B[i] / (1 - A[i]);
}
}
printf("%.15lf\n", val[1]);
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double inf = 1e15;
int n, p[1050][1050];
double val[1050], A[1050], B[1050];
bool vis[1050];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &p[i][j]);
for (int i = 1; i <= n - 1; i++) val[i] = inf;
val[n] = 0;
for (int i = 1; i <= n - 1; i++) A[i] = 1, B[i] = 1;
while (!vis[1]) {
double mn = inf;
int k;
for (int i = 1; i <= n; i++)
if (!vis[i] && val[i] < mn) mn = val[i], k = i;
vis[k] = 1;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
B[i] += A[i] * (p[i][k] / 100.0) * val[k];
A[i] *= (1 - p[i][k] / 100.0);
val[i] = B[i] / (1 - A[i]);
}
}
printf("%.15lf\n", val[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, a[N][N];
double dis[N], e[N], p[N];
bool vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", a[i] + j);
fill(dis, dis + n, 1e9);
for (int i = 1; i <= n; ++i) {
int u = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dis[j] < dis[u]) u = j;
vis[u] = 1;
for (int j = 1; j <= n; ++j)
if (!vis[j] && a[j][u])
e[j] += dis[u] * (1 - p[j]) * a[j][u] * .01,
p[j] += (1 - p[j]) * a[j][u] * .01, dis[j] = (e[j] + 1) / p[j];
}
printf("%.10lf\n", dis[1]);
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, a[N][N];
double dis[N], e[N], p[N];
bool vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", a[i] + j);
fill(dis, dis + n, 1e9);
for (int i = 1; i <= n; ++i) {
int u = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dis[j] < dis[u]) u = j;
vis[u] = 1;
for (int j = 1; j <= n; ++j)
if (!vis[j] && a[j][u])
e[j] += dis[u] * (1 - p[j]) * a[j][u] * .01,
p[j] += (1 - p[j]) * a[j][u] * .01, dis[j] = (e[j] + 1) / p[j];
}
printf("%.10lf\n", dis[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
const double INF = 1e8;
const int N = 1003;
int n, g[N][N], used[N];
double d[N], t[N];
int main() {
int u, v;
scanf("%d", &n);
for (u = 1; u <= n; u++)
for (v = 1; v <= n; v++) scanf("%d", &g[u][v]);
for (u = 1; u <= n; u++) d[u] = INF;
d[n] = 0;
for (int j = 1; j <= n; j++) {
u = 0;
for (v = 1; v <= n; v++)
if (!used[v] && d[v] < INF - 1 &&
(!u || d[v] * (1 - t[u]) < d[u] * (1 - t[v])))
u = v;
used[u] = 1;
d[u] /= 1 - t[u];
for (v = 1; v <= n; v++)
if (g[v][u] && !used[v])
if (d[v] == INF) {
d[v] = 1 + d[u] * (g[v][u] / 100.0);
t[v] = 1 - g[v][u] / 100.0;
} else {
d[v] += d[u] * (g[v][u] / 100.0) * t[v];
t[v] *= 1 - g[v][u] / 100.0;
}
}
printf("%.12lf\n", d[1]);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const double INF = 1e8;
const int N = 1003;
int n, g[N][N], used[N];
double d[N], t[N];
int main() {
int u, v;
scanf("%d", &n);
for (u = 1; u <= n; u++)
for (v = 1; v <= n; v++) scanf("%d", &g[u][v]);
for (u = 1; u <= n; u++) d[u] = INF;
d[n] = 0;
for (int j = 1; j <= n; j++) {
u = 0;
for (v = 1; v <= n; v++)
if (!used[v] && d[v] < INF - 1 &&
(!u || d[v] * (1 - t[u]) < d[u] * (1 - t[v])))
u = v;
used[u] = 1;
d[u] /= 1 - t[u];
for (v = 1; v <= n; v++)
if (g[v][u] && !used[v])
if (d[v] == INF) {
d[v] = 1 + d[u] * (g[v][u] / 100.0);
t[v] = 1 - g[v][u] / 100.0;
} else {
d[v] += d[u] * (g[v][u] / 100.0) * t[v];
t[v] *= 1 - g[v][u] / 100.0;
}
}
printf("%.12lf\n", d[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007LL;
int n;
double p[1000 + 3][1000 + 3];
double r[1000 + 3];
double a[1000 + 3];
double b[1000 + 3];
bool done[1000 + 3];
int main() {
scanf("%d", &n);
for (int i = 0, _n = n; i < _n; i++)
for (int j = 0, _n = n; j < _n; j++) scanf("%lf", &p[i][j]);
for (int i = 0, _n = n; i < _n; i++)
for (int j = 0, _n = n; j < _n; j++) p[i][j] /= 100.0;
for (int i = 0, _n = n; i < _n; i++) r[i] = 1000000001;
for (int i = 0, _n = n; i < _n; i++) a[i] = 0;
for (int i = 0, _n = n; i < _n; i++) b[i] = 1;
for (int i = 0, _n = n; i < _n; i++) done[i] = 0;
priority_queue<pair<double, int> > q;
q.push(make_pair(0, n - 1));
while (!q.empty()) {
pair<double, int> tr = q.top();
q.pop();
tr.first = -tr.first;
if (done[tr.second]) continue;
done[tr.second] = 1;
r[tr.second] = tr.first;
for (int i = 0, _n = n; i < _n; i++) {
if (i == tr.second) continue;
if (done[i]) continue;
a[i] += b[i] * p[i][tr.second] * tr.first;
b[i] *= (1 - p[i][tr.second]);
r[i] = (a[i] + 1) / (1 - b[i]);
q.push(make_pair(-r[i], i));
}
}
printf("%0.15lf\n", r[0]);
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007LL;
int n;
double p[1000 + 3][1000 + 3];
double r[1000 + 3];
double a[1000 + 3];
double b[1000 + 3];
bool done[1000 + 3];
int main() {
scanf("%d", &n);
for (int i = 0, _n = n; i < _n; i++)
for (int j = 0, _n = n; j < _n; j++) scanf("%lf", &p[i][j]);
for (int i = 0, _n = n; i < _n; i++)
for (int j = 0, _n = n; j < _n; j++) p[i][j] /= 100.0;
for (int i = 0, _n = n; i < _n; i++) r[i] = 1000000001;
for (int i = 0, _n = n; i < _n; i++) a[i] = 0;
for (int i = 0, _n = n; i < _n; i++) b[i] = 1;
for (int i = 0, _n = n; i < _n; i++) done[i] = 0;
priority_queue<pair<double, int> > q;
q.push(make_pair(0, n - 1));
while (!q.empty()) {
pair<double, int> tr = q.top();
q.pop();
tr.first = -tr.first;
if (done[tr.second]) continue;
done[tr.second] = 1;
r[tr.second] = tr.first;
for (int i = 0, _n = n; i < _n; i++) {
if (i == tr.second) continue;
if (done[i]) continue;
a[i] += b[i] * p[i][tr.second] * tr.first;
b[i] *= (1 - p[i][tr.second]);
r[i] = (a[i] + 1) / (1 - b[i]);
q.push(make_pair(-r[i], i));
}
}
printf("%0.15lf\n", r[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int f = 1, ans = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ans = ans * 10 + c - '0';
c = getchar();
}
return f * ans;
}
const int MAXN = 1e3 + 11;
int N, vis[MAXN];
double p[MAXN][MAXN];
double f[MAXN], now[MAXN];
double calc(int u) { return (f[u] + now[u]) / (1 - now[u]); }
void update(int u) {
vis[u] = 1;
for (int i = 1; i <= N; i++)
if (!vis[i])
f[i] += (calc(u) + 1) * p[i][u] * now[i], now[i] *= (1 - p[i][u]);
return;
}
int main() {
N = read();
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) {
int x = read();
p[i][j] = (double)x / 100;
}
if (N == 1) {
printf("0\n");
return 0;
}
for (int i = 1; i < N; i++) now[i] = 1;
update(N);
while (1) {
int ps = 0;
for (int i = 1; i <= N; i++)
if (!vis[i])
if (!ps || calc(i) < calc(ps)) ps = i;
update(ps);
if (ps == 1) {
printf("%.10lf\n", calc(1));
return 0;
}
}
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int f = 1, ans = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ans = ans * 10 + c - '0';
c = getchar();
}
return f * ans;
}
const int MAXN = 1e3 + 11;
int N, vis[MAXN];
double p[MAXN][MAXN];
double f[MAXN], now[MAXN];
double calc(int u) { return (f[u] + now[u]) / (1 - now[u]); }
void update(int u) {
vis[u] = 1;
for (int i = 1; i <= N; i++)
if (!vis[i])
f[i] += (calc(u) + 1) * p[i][u] * now[i], now[i] *= (1 - p[i][u]);
return;
}
int main() {
N = read();
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) {
int x = read();
p[i][j] = (double)x / 100;
}
if (N == 1) {
printf("0\n");
return 0;
}
for (int i = 1; i < N; i++) now[i] = 1;
update(N);
while (1) {
int ps = 0;
for (int i = 1; i <= N; i++)
if (!vis[i])
if (!ps || calc(i) < calc(ps)) ps = i;
update(ps);
if (ps == 1) {
printf("%.10lf\n", calc(1));
return 0;
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, f[N];
double p[N][N], s[N], E[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (int i = 1; i < n; i++) E[i] = 1, s[i] = 1.0 - p[i][n];
f[n] = 1;
for (int t = 1; t < n; t++) {
double Min = 1e18;
int j = 0;
for (int i = 1; i <= n; i++)
if (!f[i] && E[i] / (1.0 - s[i]) < Min) Min = E[i] / (1.0 - s[i]), j = i;
f[j] = 1;
for (int i = 1; i <= n; i++)
if (!f[i])
E[i] += E[j] / (1.0 - s[j]) * p[i][j] * s[i], s[i] *= 1.0 - p[i][j];
}
printf("%.10f\n", E[1] / (1.0 - s[1]));
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, f[N];
double p[N][N], s[N], E[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (int i = 1; i < n; i++) E[i] = 1, s[i] = 1.0 - p[i][n];
f[n] = 1;
for (int t = 1; t < n; t++) {
double Min = 1e18;
int j = 0;
for (int i = 1; i <= n; i++)
if (!f[i] && E[i] / (1.0 - s[i]) < Min) Min = E[i] / (1.0 - s[i]), j = i;
f[j] = 1;
for (int i = 1; i <= n; i++)
if (!f[i])
E[i] += E[j] / (1.0 - s[j]) * p[i][j] * s[i], s[i] *= 1.0 - p[i][j];
}
printf("%.10f\n", E[1] / (1.0 - s[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, p[N][N], vis[N];
long double E[N], prob[N], coef[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) E[i] = 1e30, prob[i] = 1, coef[i] = 0;
E[n - 1] = 0;
for (int k = 0; k < n; k++) {
long double mv = 1e30;
int ps = -1;
for (int i = 0; i < n; i++)
if (!vis[i] && E[i] < mv) mv = E[i], ps = i;
vis[ps] = 1;
if (ps == 0) {
printf("%.15f\n", (double)E[0]);
return 0;
}
for (int i = 0; i < n; i++)
if (!vis[i]) {
coef[i] += E[ps] * prob[i] * 0.01 * p[i][ps];
prob[i] *= (1 - 0.01 * p[i][ps]);
if (prob[i] < 1 - 1e-6) E[i] = (1 + coef[i]) / (1 - prob[i]);
}
}
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, p[N][N], vis[N];
long double E[N], prob[N], coef[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) E[i] = 1e30, prob[i] = 1, coef[i] = 0;
E[n - 1] = 0;
for (int k = 0; k < n; k++) {
long double mv = 1e30;
int ps = -1;
for (int i = 0; i < n; i++)
if (!vis[i] && E[i] < mv) mv = E[i], ps = i;
vis[ps] = 1;
if (ps == 0) {
printf("%.15f\n", (double)E[0]);
return 0;
}
for (int i = 0; i < n; i++)
if (!vis[i]) {
coef[i] += E[ps] * prob[i] * 0.01 * p[i][ps];
prob[i] *= (1 - 0.01 * p[i][ps]);
if (prob[i] < 1 - 1e-6) E[i] = (1 + coef[i]) / (1 - prob[i]);
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 18;
const int inf = 1e9;
const int N = 1005;
int n, m, x, y, z, t;
double all[N], prob[N], arr[N][N], SP[N];
int h[N];
void dijkstra() {
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
q;
q.push(make_pair(0, n));
for (int i = 1; i <= n; i++) SP[i] = inf;
SP[n] = 0;
while (q.size()) {
double cost = q.top().first;
int node = q.top().second;
q.pop();
if (h[node]) continue;
SP[node] = cost;
h[node] = 1;
for (int i = 1; i <= n; i++)
if (!h[i] && arr[i][node] && cost < SP[i]) {
all[i] += (cost + 1) * arr[i][node] * prob[i];
prob[i] *= 1 - arr[i][node];
SP[i] = (all[i] + prob[i]) / (1 - prob[i]);
q.push(make_pair(SP[i], i));
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
prob[i] = 1;
for (int j = 1; j <= n; j++) {
scanf("%lf", &arr[i][j]);
arr[i][j] /= 100.0;
}
}
dijkstra();
printf("%.12lf\n", SP[1]);
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 18;
const int inf = 1e9;
const int N = 1005;
int n, m, x, y, z, t;
double all[N], prob[N], arr[N][N], SP[N];
int h[N];
void dijkstra() {
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
q;
q.push(make_pair(0, n));
for (int i = 1; i <= n; i++) SP[i] = inf;
SP[n] = 0;
while (q.size()) {
double cost = q.top().first;
int node = q.top().second;
q.pop();
if (h[node]) continue;
SP[node] = cost;
h[node] = 1;
for (int i = 1; i <= n; i++)
if (!h[i] && arr[i][node] && cost < SP[i]) {
all[i] += (cost + 1) * arr[i][node] * prob[i];
prob[i] *= 1 - arr[i][node];
SP[i] = (all[i] + prob[i]) / (1 - prob[i]);
q.push(make_pair(SP[i], i));
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
prob[i] = 1;
for (int j = 1; j <= n; j++) {
scanf("%lf", &arr[i][j]);
arr[i][j] /= 100.0;
}
}
dijkstra();
printf("%.12lf\n", SP[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool vis[1005];
double p[1005][1005], now[1005], e[1005];
int read() {
int A;
bool K;
char C;
C = A = K = 0;
while (C < '0' || C > '9') K |= C == '-', C = getchar();
while (C > '/' && C < ':') A = (A << 3) + (A << 1) + (C ^ 48), C = getchar();
return (K ? -A : A);
}
int main() {
int n, i, j, o;
n = read();
for (i = 1; i <= (n); i++)
for (j = 1; j <= (n); j++) p[i][j] = double(read()) / 100.0;
vis[n] = 1;
for (i = 1; i <= (n); i++) now[i] = 1.0 - p[i][n];
j = n;
for (o = 1; o <= (n - 1); o++) {
j = 1;
for (i = 2; i <= (n); i++)
if (!vis[i] &&
(e[i] + 1.0) / (1.0 - now[i]) < (e[j] + 1.0) / (1.0 - now[j]))
j = i;
vis[j] = 1;
e[j] = (1.0 + e[j]) / (1.0 - now[j]);
for (i = 1; i <= (n); i++)
if (!vis[i]) e[i] += e[j] * p[i][j] * now[i];
if (j == 1) cout << fixed << setprecision(7) << e[1], exit(0);
for (i = 1; i <= (n); i++)
if (!vis[i]) now[i] *= 1.0 - p[i][j];
}
cout << fixed << setprecision(7) << e[1];
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool vis[1005];
double p[1005][1005], now[1005], e[1005];
int read() {
int A;
bool K;
char C;
C = A = K = 0;
while (C < '0' || C > '9') K |= C == '-', C = getchar();
while (C > '/' && C < ':') A = (A << 3) + (A << 1) + (C ^ 48), C = getchar();
return (K ? -A : A);
}
int main() {
int n, i, j, o;
n = read();
for (i = 1; i <= (n); i++)
for (j = 1; j <= (n); j++) p[i][j] = double(read()) / 100.0;
vis[n] = 1;
for (i = 1; i <= (n); i++) now[i] = 1.0 - p[i][n];
j = n;
for (o = 1; o <= (n - 1); o++) {
j = 1;
for (i = 2; i <= (n); i++)
if (!vis[i] &&
(e[i] + 1.0) / (1.0 - now[i]) < (e[j] + 1.0) / (1.0 - now[j]))
j = i;
vis[j] = 1;
e[j] = (1.0 + e[j]) / (1.0 - now[j]);
for (i = 1; i <= (n); i++)
if (!vis[i]) e[i] += e[j] * p[i][j] * now[i];
if (j == 1) cout << fixed << setprecision(7) << e[1], exit(0);
for (i = 1; i <= (n); i++)
if (!vis[i]) now[i] *= 1.0 - p[i][j];
}
cout << fixed << setprecision(7) << e[1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Maxx = 1000005, Mod = 998244353;
inline int read() {
int res = 0, bj = 0;
char ch = getchar();
while (ch < '0' || ch > '9') bj |= (ch == '-'), ch = getchar();
while (ch >= '0' && ch <= '9')
res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return bj ? -res : res;
}
double R, K[1005], B[1005], p[1005][1005];
char v[1005];
int main() {
int n = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = read() / 100.0;
for (int i = 1; i < n; ++i) B[i] = K[i] = 1;
for (int i = 1, j, k; i <= n; ++i) {
for (j = 1, k = 0; j <= n; ++j)
if (!v[j] && K[j] != 1 && (!k || B[j] * (1 - K[k]) < B[k] * (1 - K[j])))
k = j;
for (v[k] = j = 1, R = B[k] / (1 - K[k]); j <= n; ++j)
if (!v[j]) B[j] += K[j] * p[j][k] * R, K[j] *= 1 - p[j][k];
}
printf("%.10lf", B[1] / (1 - K[1]));
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Maxx = 1000005, Mod = 998244353;
inline int read() {
int res = 0, bj = 0;
char ch = getchar();
while (ch < '0' || ch > '9') bj |= (ch == '-'), ch = getchar();
while (ch >= '0' && ch <= '9')
res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return bj ? -res : res;
}
double R, K[1005], B[1005], p[1005][1005];
char v[1005];
int main() {
int n = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = read() / 100.0;
for (int i = 1; i < n; ++i) B[i] = K[i] = 1;
for (int i = 1, j, k; i <= n; ++i) {
for (j = 1, k = 0; j <= n; ++j)
if (!v[j] && K[j] != 1 && (!k || B[j] * (1 - K[k]) < B[k] * (1 - K[j])))
k = j;
for (v[k] = j = 1, R = B[k] / (1 - K[k]); j <= n; ++j)
if (!v[j]) B[j] += K[j] * p[j][k] * R, K[j] *= 1 - p[j][k];
}
printf("%.10lf", B[1] / (1 - K[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmin(T &a, T b) {
if (a > b) a = b;
}
template <class T>
inline void chkmax(T &a, T b) {
if (a < b) a = b;
}
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') ch = getchar(), f = -1;
while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar();
return ~f ? s : -s;
}
const int maxn = 1e3 + 20;
int n;
double p[maxn][maxn];
inline void init() {
n = read();
for (int i = (1), _end_ = (n); i <= _end_; i++)
for (int j = (1), _end_ = (n); j <= _end_; j++)
p[i][j] = (double)read() / 100;
}
double gl[maxn];
double val[maxn], f[maxn];
int vis[maxn];
inline void doing() {
for (int i = (1), _end_ = (n - 1); i <= _end_; i++) gl[i] = 1;
memset(val, 100, sizeof(val));
val[n] = 0;
for (int i = (1), _end_ = (n); i <= _end_; i++) f[i] = 1;
for (int th = (1), _end_ = (n); th <= _end_; th++) {
int u = -1;
for (int j = (1), _end_ = (n); j <= _end_; j++)
if (!vis[j] && (u == -1 || val[u] > val[j])) u = j;
vis[u] = 1;
for (int j = (1), _end_ = (n); j <= _end_; j++)
if (!vis[j] && p[j][u] > 0)
f[j] += gl[j] * p[j][u] * (val[u]), gl[j] *= 1 - p[j][u],
val[j] = f[j] / (1 - gl[j]);
}
printf("%.10lf\n", val[1]);
}
int main() {
init();
doing();
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmin(T &a, T b) {
if (a > b) a = b;
}
template <class T>
inline void chkmax(T &a, T b) {
if (a < b) a = b;
}
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') ch = getchar(), f = -1;
while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar();
return ~f ? s : -s;
}
const int maxn = 1e3 + 20;
int n;
double p[maxn][maxn];
inline void init() {
n = read();
for (int i = (1), _end_ = (n); i <= _end_; i++)
for (int j = (1), _end_ = (n); j <= _end_; j++)
p[i][j] = (double)read() / 100;
}
double gl[maxn];
double val[maxn], f[maxn];
int vis[maxn];
inline void doing() {
for (int i = (1), _end_ = (n - 1); i <= _end_; i++) gl[i] = 1;
memset(val, 100, sizeof(val));
val[n] = 0;
for (int i = (1), _end_ = (n); i <= _end_; i++) f[i] = 1;
for (int th = (1), _end_ = (n); th <= _end_; th++) {
int u = -1;
for (int j = (1), _end_ = (n); j <= _end_; j++)
if (!vis[j] && (u == -1 || val[u] > val[j])) u = j;
vis[u] = 1;
for (int j = (1), _end_ = (n); j <= _end_; j++)
if (!vis[j] && p[j][u] > 0)
f[j] += gl[j] * p[j][u] * (val[u]), gl[j] *= 1 - p[j][u],
val[j] = f[j] / (1 - gl[j]);
}
printf("%.10lf\n", val[1]);
}
int main() {
init();
doing();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
void ckmin(T1 &a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
void ckmax(T1 &a, T2 b) {
if (a < b) a = b;
}
int read() {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
void print(T x, char let) {
print(x), putchar(let);
}
const int N = 1005;
double p[N][N], coef[N], E[N];
int vis[N], n;
int main() {
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = read() / 100.0;
for (int i = 1; i <= n; i++) coef[i] = 1, E[i] = 1;
coef[n] = 0, E[n] = 0;
for (int i = 1; i <= n; i++) {
double minv = 1e9;
int x = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && E[j] / (1 - coef[j]) < minv) {
minv = E[j] / (1 - coef[j]);
x = j;
}
}
vis[x] = 1;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
E[j] += p[j][x] * E[x] / (1 - coef[x]) * coef[j];
coef[j] *= 1 - p[j][x];
}
}
}
printf("%.10f\n", E[1] / (1 - coef[1]));
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
void ckmin(T1 &a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
void ckmax(T1 &a, T2 b) {
if (a < b) a = b;
}
int read() {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
void print(T x, char let) {
print(x), putchar(let);
}
const int N = 1005;
double p[N][N], coef[N], E[N];
int vis[N], n;
int main() {
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = read() / 100.0;
for (int i = 1; i <= n; i++) coef[i] = 1, E[i] = 1;
coef[n] = 0, E[n] = 0;
for (int i = 1; i <= n; i++) {
double minv = 1e9;
int x = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && E[j] / (1 - coef[j]) < minv) {
minv = E[j] / (1 - coef[j]);
x = j;
}
}
vis[x] = 1;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
E[j] += p[j][x] * E[x] / (1 - coef[x]) * coef[j];
coef[j] *= 1 - p[j][x];
}
}
}
printf("%.10f\n", E[1] / (1 - coef[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int v[N], n;
double d[N], te[N], a[N][N];
int main() {
scanf("%d", &n);
if (n == 1) return 0 * puts("0");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
a[i][j] = 0.01 * x;
}
for (int i = 1; i <= n; i++) d[i] = 1, te[i] = 1 - a[i][n];
d[n] = 0;
v[n] = 1;
for (int i = 1; i <= n; i++) {
int x = 0;
double mn = 1e18;
for (int j = 1; j <= n; j++)
if (!v[j] && d[j] / (1 - te[j]) < mn) mn = d[j] / (1 - te[j]), x = j;
if (x == 1) return printf("%.15lf", mn), 0;
v[x] = 1;
for (int j = 1; j <= n; j++) {
d[j] += mn * a[j][x] * te[j], te[j] *= 1 - a[j][x];
}
}
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int v[N], n;
double d[N], te[N], a[N][N];
int main() {
scanf("%d", &n);
if (n == 1) return 0 * puts("0");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
a[i][j] = 0.01 * x;
}
for (int i = 1; i <= n; i++) d[i] = 1, te[i] = 1 - a[i][n];
d[n] = 0;
v[n] = 1;
for (int i = 1; i <= n; i++) {
int x = 0;
double mn = 1e18;
for (int j = 1; j <= n; j++)
if (!v[j] && d[j] / (1 - te[j]) < mn) mn = d[j] / (1 - te[j]), x = j;
if (x == 1) return printf("%.15lf", mn), 0;
v[x] = 1;
for (int j = 1; j <= n; j++) {
d[j] += mn * a[j][x] * te[j], te[j] *= 1 - a[j][x];
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Max_N(1050);
int N;
double P[Max_N][Max_N], F[Max_N], G[Max_N], V[Max_N];
bool done[Max_N];
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
V[i] = 1.0;
for (int j = 1; j <= N; ++j) scanf("%lf", P[i] + j), P[i][j] /= 100.0;
if (i < N) F[i] = 1E100;
}
for (int u;;) {
u = -1;
for (int x = 1; x <= N; ++x)
if (!done[x] && (u == -1 || F[x] < F[u])) u = x;
if (u == -1) break;
done[u] = true;
for (int v = 1; v <= N; ++v)
if (!done[v] && P[v][u] != 0.0)
G[v] += V[v] * P[v][u] * F[u], V[v] *= (1.0 - P[v][u]),
F[v] = 1.0 / (1.0 - V[v]) + G[v] / (1.0 - V[v]);
}
printf("%.10lf", F[1]);
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Max_N(1050);
int N;
double P[Max_N][Max_N], F[Max_N], G[Max_N], V[Max_N];
bool done[Max_N];
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
V[i] = 1.0;
for (int j = 1; j <= N; ++j) scanf("%lf", P[i] + j), P[i][j] /= 100.0;
if (i < N) F[i] = 1E100;
}
for (int u;;) {
u = -1;
for (int x = 1; x <= N; ++x)
if (!done[x] && (u == -1 || F[x] < F[u])) u = x;
if (u == -1) break;
done[u] = true;
for (int v = 1; v <= N; ++v)
if (!done[v] && P[v][u] != 0.0)
G[v] += V[v] * P[v][u] * F[u], V[v] *= (1.0 - P[v][u]),
F[v] = 1.0 / (1.0 - V[v]) + G[v] / (1.0 - V[v]);
}
printf("%.10lf", F[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool getmin(T *a, const T &b) {
if (b < *a) {
*a = b;
return true;
}
return false;
}
template <class T>
inline bool getmax(T *a, const T &b) {
if (b > *a) {
*a = b;
return true;
}
return false;
}
template <class T>
inline void read(T *a) {
char c;
while (isspace(c = getchar())) {
}
bool flag = 0;
if (c == '-')
flag = 1, *a = 0;
else
*a = c - 48;
while (isdigit(c = getchar())) *a = *a * 10 + c - 48;
if (flag) *a = -*a;
}
const int mo = 1000000007;
template <class T>
T pow(T a, T b, int c = mo) {
T res = 1;
for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c)
if (b & i) res = 1LL * res * a % c;
return res;
}
const int N = 1001;
double a[N][N], f[N], s[N], pre[N];
bool vis[N];
int n;
int main() {
cin >> n;
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (n); ++j) read(&a[i][j]), a[i][j] /= 100;
for (int i = (0); i <= (n); ++i) f[i] = 1e30, s[i] = 1;
f[n] = 0;
for (int tt = (1); tt <= (n); ++tt) {
int p = 0;
for (int i = (1); i <= (n); ++i)
if (!vis[i] && f[p] > f[i]) p = i;
vis[p] = 1;
for (int i = (1); i <= (n); ++i)
if (i != p && !vis[i]) {
pre[i] += s[i] * a[i][p] * f[p];
s[i] *= (1.0 - a[i][p]);
f[i] = (pre[i] + 1) / (1.0 - s[i]);
}
}
printf("%.10lf\n", f[1]);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool getmin(T *a, const T &b) {
if (b < *a) {
*a = b;
return true;
}
return false;
}
template <class T>
inline bool getmax(T *a, const T &b) {
if (b > *a) {
*a = b;
return true;
}
return false;
}
template <class T>
inline void read(T *a) {
char c;
while (isspace(c = getchar())) {
}
bool flag = 0;
if (c == '-')
flag = 1, *a = 0;
else
*a = c - 48;
while (isdigit(c = getchar())) *a = *a * 10 + c - 48;
if (flag) *a = -*a;
}
const int mo = 1000000007;
template <class T>
T pow(T a, T b, int c = mo) {
T res = 1;
for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c)
if (b & i) res = 1LL * res * a % c;
return res;
}
const int N = 1001;
double a[N][N], f[N], s[N], pre[N];
bool vis[N];
int n;
int main() {
cin >> n;
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (n); ++j) read(&a[i][j]), a[i][j] /= 100;
for (int i = (0); i <= (n); ++i) f[i] = 1e30, s[i] = 1;
f[n] = 0;
for (int tt = (1); tt <= (n); ++tt) {
int p = 0;
for (int i = (1); i <= (n); ++i)
if (!vis[i] && f[p] > f[i]) p = i;
vis[p] = 1;
for (int i = (1); i <= (n); ++i)
if (i != p && !vis[i]) {
pre[i] += s[i] * a[i][p] * f[p];
s[i] *= (1.0 - a[i][p]);
f[i] = (pre[i] + 1) / (1.0 - s[i]);
}
}
printf("%.10lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-10;
inline int read() {
static char ch;
bool sgn = false;
while (ch = getchar(), ch < '0' || ch > '9')
if (ch == '-') sgn = true;
int res = ch - 48;
while (ch = getchar(), ch >= '0' && ch <= '9') res = res * 10 + ch - 48;
return sgn ? -res : res;
}
const int N = 1e3 + 5;
double A[N], B[N], P[N][N], dis[N];
bool vis[N];
int main() {
int n, i, j, k;
n = read();
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) scanf("%lf", &P[i][j]), P[i][j] /= 100.0;
for (i = 1; i <= n; i++) dis[i] = 1e60, B[i] = 1.0;
dis[n] = 0;
for (i = 1; i < n; i++) {
k = -1;
for (j = 1; j <= n; j++)
if (!vis[j] && (k == -1 || dis[j] < dis[k])) k = j;
vis[k] = 1;
for (j = 1; j < n; j++)
if (!vis[j]) {
A[j] += B[j] * P[j][k] * dis[k];
B[j] *= (1.0 - P[j][k]);
if (B[j] < 1) dis[j] = (A[j] + 1.0) / (1.0 - B[j]);
}
}
printf("%.15f", dis[1]);
}
|
### Prompt
Create a solution in cpp for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-10;
inline int read() {
static char ch;
bool sgn = false;
while (ch = getchar(), ch < '0' || ch > '9')
if (ch == '-') sgn = true;
int res = ch - 48;
while (ch = getchar(), ch >= '0' && ch <= '9') res = res * 10 + ch - 48;
return sgn ? -res : res;
}
const int N = 1e3 + 5;
double A[N], B[N], P[N][N], dis[N];
bool vis[N];
int main() {
int n, i, j, k;
n = read();
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) scanf("%lf", &P[i][j]), P[i][j] /= 100.0;
for (i = 1; i <= n; i++) dis[i] = 1e60, B[i] = 1.0;
dis[n] = 0;
for (i = 1; i < n; i++) {
k = -1;
for (j = 1; j <= n; j++)
if (!vis[j] && (k == -1 || dis[j] < dis[k])) k = j;
vis[k] = 1;
for (j = 1; j < n; j++)
if (!vis[j]) {
A[j] += B[j] * P[j][k] * dis[k];
B[j] *= (1.0 - P[j][k]);
if (B[j] < 1) dis[j] = (A[j] + 1.0) / (1.0 - B[j]);
}
}
printf("%.15f", dis[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + c - '0';
x *= f;
}
int n;
double g[N][N];
double E[N], prod[N];
bool vis[N];
int main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
read(x);
g[i][j] = 1. * x / 100;
}
if (n == 1) return puts("0");
vis[n] = true;
for (int i = 1; i < n; i++) {
E[i] = 1;
prod[i] = 1 - g[i][n];
}
for (int i = 1; i <= n; i++) {
double low = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && E[j] / (1 - prod[j]) < low)
low = E[j] / (1 - prod[j]), pos = j;
if (pos == 1) return printf("%.10lf\n", E[1] / (1 - prod[1])), 0;
vis[pos] = true;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - prod[pos]) * g[j][pos] * prod[j],
prod[j] *= (1 - g[j][pos]);
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + c - '0';
x *= f;
}
int n;
double g[N][N];
double E[N], prod[N];
bool vis[N];
int main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
read(x);
g[i][j] = 1. * x / 100;
}
if (n == 1) return puts("0");
vis[n] = true;
for (int i = 1; i < n; i++) {
E[i] = 1;
prod[i] = 1 - g[i][n];
}
for (int i = 1; i <= n; i++) {
double low = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && E[j] / (1 - prod[j]) < low)
low = E[j] / (1 - prod[j]), pos = j;
if (pos == 1) return printf("%.10lf\n", E[1] / (1 - prod[1])), 0;
vis[pos] = true;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - prod[pos]) * g[j][pos] * prod[j],
prod[j] *= (1 - g[j][pos]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1006][1006];
double dp[1006], tmp[1006], f[1006];
int vis[1006];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1, q; j <= n; ++j) scanf("%d", &q), p[i][j] = 1.0 * q / 100;
for (int i = 1; i <= n; ++i) dp[i] = 3e18, tmp[i] = 1;
dp[n] = 0;
for (int i = 1, u = 0; i <= n; ++i) {
double mn = 3e18;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dp[j] < mn) mn = dp[j], u = j;
vis[u] = 1;
for (int v = 1; v <= n; ++v)
if (!vis[v] && p[v][u] > 1e-8) {
double ls = 1 - tmp[v];
tmp[v] *= 1.0 - p[v][u];
double t = ls / (1 - tmp[v]);
double r1 = t * f[v], r2 = (1 - t) * dp[u];
f[v] = r1 + r2;
dp[v] = min(dp[v], f[v] + 1 / (1 - tmp[v]));
}
}
printf("%.7lf", dp[1]);
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1006][1006];
double dp[1006], tmp[1006], f[1006];
int vis[1006];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1, q; j <= n; ++j) scanf("%d", &q), p[i][j] = 1.0 * q / 100;
for (int i = 1; i <= n; ++i) dp[i] = 3e18, tmp[i] = 1;
dp[n] = 0;
for (int i = 1, u = 0; i <= n; ++i) {
double mn = 3e18;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dp[j] < mn) mn = dp[j], u = j;
vis[u] = 1;
for (int v = 1; v <= n; ++v)
if (!vis[v] && p[v][u] > 1e-8) {
double ls = 1 - tmp[v];
tmp[v] *= 1.0 - p[v][u];
double t = ls / (1 - tmp[v]);
double r1 = t * f[v], r2 = (1 - t) * dp[u];
f[v] = r1 + r2;
dp[v] = min(dp[v], f[v] + 1 / (1 - tmp[v]));
}
}
printf("%.7lf", dp[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
int mat[1003][1003];
double f[1003], p[1003];
bool vis[1003];
bool cmp(int, int);
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
p[i] = 1;
for (int j = 1; j <= n; ++j) scanf("%d", mat[i] + j);
}
f[n] = p[n] = 0;
while (true) {
int pos = 0;
double mn = 9999999999999;
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
double x = (f[i] + p[i]) / (1 - p[i]);
if (x < mn) mn = x, pos = i;
}
if (!pos) break;
vis[pos] = true;
f[pos] = (f[pos] + p[pos]) / (1 - p[pos]);
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
double x = mat[i][pos] / 100.0;
f[i] += (f[pos] + 1) * p[i] * x;
p[i] *= (1 - x);
}
}
printf("%.10lf\n", f[1]);
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int mat[1003][1003];
double f[1003], p[1003];
bool vis[1003];
bool cmp(int, int);
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
p[i] = 1;
for (int j = 1; j <= n; ++j) scanf("%d", mat[i] + j);
}
f[n] = p[n] = 0;
while (true) {
int pos = 0;
double mn = 9999999999999;
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
double x = (f[i] + p[i]) / (1 - p[i]);
if (x < mn) mn = x, pos = i;
}
if (!pos) break;
vis[pos] = true;
f[pos] = (f[pos] + p[pos]) / (1 - p[pos]);
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
double x = mat[i][pos] / 100.0;
f[i] += (f[pos] + 1) * p[i] * x;
p[i] *= (1 - x);
}
}
printf("%.10lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1002;
int n, m, q[N], vis[N];
double p[N][N], f[N], prod[N], sum[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
p[i][j] = x / 100.0;
}
prod[i] = 1;
}
f[n] = 0;
vis[n] = 1;
q[++m] = n;
while (m < n) {
double g = 0;
int v = 0;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
sum[i] += f[q[m]] * p[i][q[m]] * prod[i];
prod[i] *= 1 - p[i][q[m]];
double cur = (sum[i] + 1) / (1 - prod[i]);
if (!v || cur < g) {
g = cur;
v = i;
}
}
f[v] = g;
vis[v] = 1;
q[++m] = v;
}
printf("%.10lf\n", f[1]);
}
|
### Prompt
Please create a solution in CPP to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1002;
int n, m, q[N], vis[N];
double p[N][N], f[N], prod[N], sum[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
p[i][j] = x / 100.0;
}
prod[i] = 1;
}
f[n] = 0;
vis[n] = 1;
q[++m] = n;
while (m < n) {
double g = 0;
int v = 0;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
sum[i] += f[q[m]] * p[i][q[m]] * prod[i];
prod[i] *= 1 - p[i][q[m]];
double cur = (sum[i] + 1) / (1 - prod[i]);
if (!v || cur < g) {
g = cur;
v = i;
}
}
f[v] = g;
vis[v] = 1;
q[++m] = v;
}
printf("%.10lf\n", f[1]);
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.