Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int reader_pt = 1048576, reader_last;
char reader_buf[1048576];
int reader_nonneg_int() {
int r;
for (;;) {
if (reader_pt == 1048576)
reader_pt = 0,
reader_last = fread(reader_buf, sizeof(char), 1048576, stdin);
if (('0' <= (reader_buf[reader_pt]) && (reader_buf[reader_pt]) <= '9'))
break;
reader_pt++;
}
r = reader_buf[reader_pt++] - '0';
for (;;) {
if (reader_pt == 1048576)
reader_pt = 0,
reader_last = fread(reader_buf, sizeof(char), 1048576, stdin);
if (!('0' <= (reader_buf[reader_pt]) && (reader_buf[reader_pt]) <= '9'))
break;
r = r * 10 + (reader_buf[reader_pt++] - '0');
}
reader_pt++;
return r;
}
double solve_brute(int n, int in[]) {
int i, j, k, s;
int arr[100];
double res = 0, tmp;
for (i = 0; i < n; i++)
for (j = i; j < n; j++) {
s = 0;
for (k = i; k < j + 1; k++) arr[s++] = in[k];
tmp = 0;
sort(arr, arr + s);
for (k = 0; k < s; k++) tmp = (tmp + arr[k]) / 2;
res += tmp;
}
res /= n;
res /= n;
return res;
}
int main() {
int i, j, k, kk, l, m, n, x, y;
static int a[500000];
static pair<int, int> srt[500000];
set<int> ind;
set<int>::iterator it, it1, it2;
int ss[25], ls[25];
double pw[100], res;
pw[0] = 1;
for (i = 1; i < 100; i++) pw[i] = pw[i - 1] * 0.5;
n = reader_nonneg_int();
for (i = 0; i < n; i++) a[i] = reader_nonneg_int();
for (i = 0; i < n; i++) srt[i] = make_pair(a[i], i);
sort(srt, srt + n);
res = 0;
ind.clear();
ind.insert(-1);
ind.insert(n);
for (kk = n - 1; kk >= 0; kk--) {
k = srt[kk].second;
ind.insert(k);
it = ind.lower_bound(k);
it1 = it2 = it;
for (i = 0; i < 25; i++) ss[i] = ls[i] = 0;
x = 0;
for (i = 0; i < 25; i++) {
it1--;
y = ss[i] = k - (*it1);
ss[i] -= x;
x = y;
if ((*it1) == -1) break;
}
x = 0;
for (i = 0; i < 25; i++) {
it2++;
y = ls[i] = (*it2) - k;
ls[i] -= x;
x = y;
if ((*it2) == n) break;
}
for (i = 0; i < 25; i++)
if (ss[i])
for (j = 0; j < 25; j++)
if (ls[j]) {
res += pw[i + j + 1] * a[k] * ss[i] * ls[j];
}
}
res /= n;
res /= n;
printf("%.15f\n", res);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[300005];
set<int> s;
long double ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i + 1;
}
sort(a, a + n);
reverse(a, a + n);
s.insert(0);
s.insert(n + 1);
for (int i = 0; i < n; ++i) {
long double l = 0, r = 0;
set<int>::iterator it = s.upper_bound(a[i].second);
int last = a[i].second;
for (int i = 0; i < 60 && it != s.end(); ++i, ++it) {
l += (*it - last) * 1.0 / (1LL << i);
last = *it;
}
last = a[i].second;
it = s.upper_bound(a[i].second);
--it;
for (int i = 0; i < 60; ++i, --it) {
r += (last - *it) * 1.0 / (1LL << i);
last = *it;
if (it == s.begin()) break;
}
ans += l * r * a[i].first / 2;
s.insert(a[i].second);
}
cout << setprecision(7) << fixed << ans / n / n << '\n';
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long n, pre[((long long)301 * 1000)], nex[((long long)301 * 1000)];
pair<long long, long long> a[((long long)301 * 1000)];
set<long long> s;
long double ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i].first, a[i].second = i, nex[i] = n, pre[i] = -1;
pre[n] = -1;
s.insert(n);
sort(a, a + n);
for (int i = n - 1; i >= 0; i--) {
long long id = a[i].second, x = *s.lower_bound(id);
long double rght = 0, now = 1;
for (int j = 0; j < 50; j++) {
rght += now * ((long double)x - id);
if (x == n) break;
now *= (long double)1 / 2;
id = x;
x = nex[x];
}
long double lft = 0;
now = 1;
id = a[i].second;
x = pre[*s.lower_bound(id)];
for (int j = 0; j < 50; j++) {
lft += now * ((long double)id - x);
if (x == -1) break;
now *= (long double)1 / 2;
id = x;
x = pre[x];
}
ans += lft * rght * 1 / 2 * a[i].first;
id = a[i].second;
x = *s.lower_bound(id);
pre[id] = pre[x];
nex[pre[x]] = id;
pre[x] = id;
nex[id] = x;
s.insert(id);
}
ans /= (long double)n * n;
cout << fixed << setprecision(16) << ans << "\n";
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long double p[300300];
int n;
pair<int, int> b[300300];
void pre(void) {
p[0] = 1.0;
for (int i = 1; i < 300300; ++i) p[i] = p[i - 1] / 2;
}
struct node {
long double l, r;
int c;
} t[4 * 300300], one{0.5, 1.0, 1}, zero{1.0, 1.0, 0};
node comb(const node &l, const node &r) {
node ans;
ans.c = l.c + r.c;
ans.l = l.l * p[r.c] + r.l;
ans.r = l.r + p[l.c] * r.r;
return ans;
}
void upd(int v, int l, int r, int p) {
if (l + 1 == r) {
t[v] = one;
return;
}
int m = (l + r) / 2;
if (p < m)
upd(2 * v, l, m, p);
else
upd(2 * v + 1, m, r, p);
t[v] = comb(t[2 * v], t[2 * v + 1]);
}
void build(int v, int l, int r) {
if (l + 1 == r) {
t[v] = zero;
return;
}
int m = (l + r) / 2;
build(2 * v, l, m);
build(2 * v + 1, m, r);
t[v] = comb(t[2 * v], t[2 * v + 1]);
}
node get(int v, int l, int r, int le, int re) {
if (l == le && r == re) return t[v];
int m = (l + r) / 2;
if (re <= m) return get(2 * v, l, m, le, re);
if (m <= le) return get(2 * v + 1, m, r, le, re);
return comb(get(2 * v, l, m, le, m), get(2 * v + 1, m, r, m, re));
}
void read(void) {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> b[i].first;
b[i].second = i;
}
sort(b, b + n);
}
void kill(void) {
long double ans = 0.0;
node l, r;
for (int i = n - 1; i >= 0; --i) {
int pos = b[i].second;
l = get(1, 0, n + 1, 0, pos + 1);
r = get(1, 0, n + 1, pos + 1, n + 1);
ans = ans + b[i].first * l.l * r.r;
upd(1, 0, n + 1, pos);
}
cout.precision(10);
cout << fixed;
cout << ans / n / n / 2 << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
pre();
read();
build(1, 0, n + 1);
kill();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
gn(first);
gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
gn(first);
gn(second);
gn(z);
}
template <class T>
inline void print(T first) {
if (first < 0) {
putchar('-');
return print(-first);
}
if (first < 10) {
putchar('0' + first);
return;
}
print(first / 10);
putchar(first % 10 + '0');
}
template <class T>
inline void printsp(T first) {
print(first);
putchar(' ');
}
template <class T>
inline void println(T first) {
print(first);
putchar('\n');
}
template <class T, class U>
inline void print(T first, U second) {
printsp(first);
println(second);
}
template <class T, class U, class V>
inline void print(T first, U second, V z) {
printsp(first);
printsp(second);
println(z);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
int b[300111], nxt[300111];
int pre[300111];
pair<int, int> arr[300111];
int nn;
set<int> s;
set<int>::iterator it;
double pw[300111];
int lft[300111], rgt[300111];
int c[2];
int main() {
pw[0] = 1;
for (int i = 1; i < 1111; i++) pw[i] = pw[i - 1] / 2;
int n;
cin >> n;
s.insert(0);
s.insert(n + 1);
nxt[0] = n + 1;
pre[n + 1] = 0;
for (int i = 1; i <= n; i++) gn(b[i]), arr[nn++] = pair<int, int>(b[i], i);
sort(arr, arr + nn);
double ans = 0;
for (int i = nn; i--;) {
int now = arr[i].second;
it = s.upper_bound(now);
rgt[0] = *it;
it--;
lft[0] = *it;
s.insert(now);
nxt[now] = rgt[0];
pre[now] = lft[0];
pre[rgt[0]] = now;
nxt[lft[0]] = now;
c[0] = c[1] = 1;
lft[0] = rgt[0] = now;
double A = 0, B = 0;
for (; c[0] < 50 and lft[c[0] - 1]; c[0]++) {
lft[c[0]] = pre[lft[c[0] - 1]];
A += (lft[c[0] - 1] - lft[c[0]]) * pw[c[0]];
}
for (; c[1] < 50 and rgt[c[1] - 1] <= n; c[1]++) {
rgt[c[1]] = nxt[rgt[c[1] - 1]];
B += (rgt[c[1]] - rgt[c[1] - 1]) * pw[c[1]];
}
ans += A * B * (2 * arr[i].first);
}
printf("%.9lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct SegmentTreeDivideRange {
int size;
int treeSize;
vector<double> SUM;
vector<int> LAZY;
SegmentTreeDivideRange(int sz) {
size = sz;
treeSize = 4 * sz;
SUM.resize(treeSize);
LAZY.resize(treeSize);
}
void push(int x, int l, int r) {
if (l < r) {
LAZY[x + x] += LAZY[x];
LAZY[x + x + 1] += LAZY[x];
}
apply(x);
}
inline void apply(int x) {
const int M = 60;
int k = LAZY[x];
if (k >= M + M)
SUM[x] = 0;
else {
while (k >= M) {
SUM[x] /= (1LL << M);
k -= M;
}
SUM[x] /= (1LL << k);
}
LAZY[x] = 0;
}
void UpdateOne(int pp, double val) { UpdateOne(1, 0, size - 1, pp, val); }
void UpdateOne(int x, int l, int r, int pp, double val) {
push(x, l, r);
if (l > pp || r < pp) return;
if (l == r) {
SUM[x] += val;
push(x, l, r);
return;
}
int y = (l + r) >> 1;
UpdateOne(x + x, l, y, pp, val);
UpdateOne(x + x + 1, y + 1, r, pp, val);
Correct(x);
}
void UpdateRange(int ll, int rr, int val) {
UpdateRange(1, 0, size - 1, ll, rr, val);
}
inline void Correct(int x) { SUM[x] = SUM[x + x] + SUM[x + x + 1]; }
void UpdateRange(int x, int l, int r, int ll, int rr, int val) {
push(x, l, r);
if (ll > r || l > rr) return;
if (ll <= l && r <= rr) {
LAZY[x] += val;
push(x, l, r);
return;
}
int y = (l + r) >> 1;
UpdateRange(x + x, l, y, ll, rr, val);
UpdateRange(x + x + 1, y + 1, r, ll, rr, val);
Correct(x);
}
double Query(int ll, int rr) { return Query(1, 0, size - 1, ll, rr); }
double Query(int x, int l, int r, int ll, int rr) {
push(x, l, r);
if (ll > r || l > rr) return 0;
if (ll <= l && r <= rr) {
return SUM[x];
}
int y = (l + r) >> 1;
return Query(x + x, l, y, ll, rr) + Query(x + x + 1, y + 1, r, ll, rr);
}
};
int main() {
int n;
cin >> n;
pair<double, int> bb[n];
int b[n];
int r[n];
double coefSum = 0;
double ret = 0;
SegmentTreeDivideRange sum(n), coef(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &b[i]);
bb[i] = {b[i], i};
}
sort(bb, bb + n);
for (int i = 0; i < n; ++i) r[bb[i].second] = i;
for (int i = 0; i < n; ++i) {
if (b[i] > 0) {
double oldCoefSum = coefSum;
coefSum = coefSum / 2 + (i + 1) / 2.0;
coef.UpdateRange(0, r[i] - 1, 1);
sum.UpdateRange(0, r[i] - 1, 1);
double newCoef = coefSum - oldCoefSum + coef.Query(0, r[i] - 1);
coef.UpdateOne(r[i], newCoef);
sum.UpdateOne(r[i], newCoef * b[i]);
}
ret += sum.Query(0, n - 1);
}
ret /= n;
ret /= n;
cout << setprecision(20) << ret << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, w[1000010], b[1000010], nxt[1000010], pre[1000010];
double ans;
bool cmp(int x, int y) {
if (w[x] == w[y]) {
return x < y;
} else {
return w[x] < w[y];
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &w[i]);
b[i] = i;
}
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= n; i++) {
nxt[i] = i + 1;
pre[i] = i - 1;
}
for (int i = 1; i <= n; i++) {
int x = b[i];
int l = x, r = x;
double lw = 0.0, rw = 0.0, now = 1.0;
for (int j = 0; j < 100; j++) {
if (l) {
lw += (double)(l - pre[l]) * now;
l = pre[l];
}
if (r <= n) {
rw += (double)(nxt[r] - r) * now;
r = nxt[r];
}
now *= 0.5;
}
ans += (double)w[x] * lw * rw * 0.5;
nxt[pre[x]] = nxt[x];
pre[nxt[x]] = pre[x];
}
printf("%.12lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace ::std;
const long long maxn = 3e5 + 500;
const long long inf = 2e9;
long long a[maxn];
pair<long long, long long> b[maxn];
set<long long> st;
long long nxt[maxn];
long long pre[maxn];
void update(long long p) {
set<long long>::iterator itnxt = st.lower_bound(p);
set<long long>::iterator itpre = itnxt;
itpre--;
long long rnxt = (*itnxt);
long long rpre = (*itpre);
nxt[rpre] = p;
pre[rnxt] = p;
nxt[p] = rnxt;
pre[p] = rpre;
st.insert(p);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
st.insert(0);
st.insert(n + 1);
for (long long i = 0; i <= n + 1; i++) {
nxt[i] = n + 1;
pre[i] = 0;
}
for (long long i = 1; i <= n; i++) {
cin >> a[i];
b[i] = make_pair(a[i], i);
}
sort(b + 1, b + n + 1);
reverse(b + 1, b + n + 1);
long double ans = 0;
for (long long i = 1; i <= n; i++) {
long long v = b[i].second;
update(v);
vector<long long> vecnxt;
vector<long long> vecpre;
vecnxt.reserve(25);
vecpre.reserve(25);
long long p = v;
while (p != n + 1 && vecnxt.size() < 25) {
vecnxt.push_back(abs(nxt[p] - p));
p = nxt[p];
}
p = v;
while (p != 0 && vecpre.size() < 25) {
vecpre.push_back(abs(p - pre[p]));
p = pre[p];
}
long double z = 0.5;
for (char j = 0; j < 25; j++) {
long long r = 0;
char tah = min((long long)j, (long long)vecpre.size() - 1);
char sar = max((long long)0, j - (long long)vecnxt.size() + 1);
for (char k = sar; k <= tah; k++) r += vecpre[k] * vecnxt[j - k];
ans += a[v] * z * r;
z /= 2;
}
}
ans /= (n * n);
cout << fixed << setprecision(10) << ans;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
int n;
int b[333333];
vector<pair<int, int> > t;
int rv[333333];
struct Node {
Node *lf;
Node *rg;
int l, r;
int mx;
Node() {
lf = rg = 0;
mx = 0;
}
};
Node *root;
Node *buildTree(int from, int to) {
Node *res = new Node();
res->l = from;
res->r = to;
if (from != to) {
res->lf = buildTree(from, (from + to) / 2);
res->rg = buildTree((from + to) / 2 + 1, to);
}
return res;
}
void setValue(Node *curr, int pos, int val) {
if (!curr) return;
if (curr->l > pos || curr->r < pos) return;
curr->mx = max(curr->mx, val);
setValue(curr->lf, pos, val);
setValue(curr->rg, pos, val);
}
int getBiggerL(Node *curr, int to, int val) {
if (!curr) return -1;
if (curr->mx <= val) return -1;
if (curr->l > to) return -1;
if (curr->l == curr->r) return curr->l;
int p1 = getBiggerL(curr->rg, to, val);
if (p1 != -1) return p1;
return getBiggerL(curr->lf, to, val);
}
int getBiggerR(Node *curr, int from, int val) {
if (!curr) return -1;
if (curr->mx <= val) return -1;
if (curr->r < from) return -1;
if (curr->l == curr->r) return curr->l;
int p1 = getBiggerR(curr->lf, from, val);
if (p1 != -1) return p1;
return getBiggerR(curr->rg, from, val);
}
double pw[111111];
int main() {
pw[0] = 1;
for (int i = (1); i < (111111); i++) pw[i] = pw[i - 1] / 2;
cin >> n;
for (int i = (0); i < (n); i++)
scanf("%d", b + i), t.push_back(pair<int, int>(b[i], i));
sort((t).begin(), (t).end());
root = buildTree(0, n - 1);
for (int i = (0); i < (n); i++) {
rv[t[i].second] = i + 1;
setValue(root, t[i].second, i + 1);
}
const int lim = 20;
double sum = 0;
vector<int> l, r;
double den = 1. / n / n;
l.reserve(lim), r.reserve(lim);
for (int i = (0); i < (n); i++) {
l.clear(), r.clear();
int curr = i;
while (1) {
int pos = getBiggerL(root, curr - 1, rv[i]);
if (pos == -1) {
l.push_back(curr + 1);
break;
} else {
l.push_back(curr - pos);
curr = pos;
}
if (l.size() == lim) break;
}
curr = i;
while (1) {
int pos = getBiggerR(root, curr + 1, rv[i]);
if (pos == -1) {
r.push_back(n - curr);
break;
} else {
r.push_back(pos - curr);
curr = pos;
}
if (r.size() == lim) break;
}
for (int i1 = (0); i1 < (l.size()); i1++)
for (int i2 = (0); i2 < (r.size()); i2++) {
sum += b[i] * den * l[i1] * r[i2] * pw[i1 + i2 + 1];
}
}
printf("%.10lf\n", sum);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double x, y, z, s;
int n, a, b, c, v[300010], p[300010], l[300010], r[300010];
bool cmp(int x, int y) { return v[x] < v[y]; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &v[i]), l[i] = i - 1, r[i] = i + 1, p[i] = i;
sort(p + 1, p + n + 1, cmp);
for (int i = 1; i <= n; i++) {
x = y = 0, z = 1;
a = b = c = p[i];
for (int j = 1; j <= 45; j++) {
if (a) x += (a - l[a]) * z, a = l[a];
if (b <= n) y += (r[b] - b) * z, b = r[b];
z /= 2;
}
l[r[c]] = l[c], r[l[c]] = r[c];
s += x * y * v[c] / 2;
}
printf("%.6f\n", s / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N;
int a[300010];
vector<pair<int, int> > v;
set<int> s;
double get_coef(int x) {
int i, prev, cnt;
double two, c1 = 0.0, c2 = 0.0;
s.insert(x);
set<int>::iterator itr = s.find(x);
two = 1.0;
cnt = 0;
prev = x;
while (cnt < 50) {
itr--;
int y = (*itr);
c1 += (prev - y) * two;
if (y == -1) break;
cnt++;
two /= 2.0;
prev = y;
}
itr = s.find(x);
two = 1.0;
cnt = 0;
prev = x;
while (cnt < 50) {
itr++;
int y = (*itr);
c2 += (y - prev) * two;
if (y == N) break;
cnt++;
two /= 2.0;
prev = y;
}
return c1 * c2;
}
int main(void) {
int i, j;
cin >> N;
for ((i) = 0; (i) < (int)(N); (i)++) scanf("%d", &a[i]);
for ((i) = 0; (i) < (int)(N); (i)++) v.push_back(make_pair(-a[i], i));
sort(v.begin(), v.end());
s.insert(-1);
s.insert(N);
double ans = 0.0;
for ((i) = 0; (i) < (int)(N); (i)++) {
double x = get_coef(v[i].second);
ans += x * a[v[i].second];
}
ans = ans / 2.0 / N / N;
printf("%.9f\n", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 300010;
const int TINY = 1e-13;
int N;
double b[MAXN];
pair<double, int> first[MAXN];
const int MAXS = 30;
int Ls, Rs;
int L[MAXS], R[MAXS];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(12) << fixed;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> b[i];
b[i] += i * TINY;
}
for (int i = 0; i < N; i++) first[i] = make_pair(b[i], i);
sort(first, first + N);
multiset<int> greater;
double ans = 0;
for (int i = N - 1; i >= 0; i--) {
Ls = Rs = 0;
multiset<int>::iterator it = greater.lower_bound(first[i].second);
while (it != greater.begin() && Ls < MAXS) {
it--;
L[Ls++] = *it;
}
if (Ls < MAXS) L[Ls++] = -1;
for (it = greater.upper_bound(first[i].second);
it != greater.end() && Rs < MAXS; it++)
R[Rs++] = *it;
if (Rs < MAXS) R[Rs++] = N;
int prevl = first[i].second;
double coef = 0.5;
double sa = 0;
for (int j = 0; j < Ls; j++) {
sa += coef * double(prevl - L[j]);
prevl = L[j];
coef *= 0.5;
}
int prevr = first[i].second;
coef = 1;
for (int j = 0; j < Rs; j++) {
ans += first[i].first * coef * double(R[j] - prevr) * sa;
prevr = R[j];
coef *= 0.5;
}
greater.insert(first[i].second);
}
cout << ans / (N * double(N)) << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void umax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
inline void umin(T &a, T b) {
if (a > b) a = b;
}
template <class T>
inline T abs(T a) {
return a > 0 ? a : -a;
}
template <class T>
inline T gcd(T a, T b) {
return __gcd(a, b);
}
template <class T>
inline T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
const int inf = 1e9 + 143;
const long long longinf = 1e18 + 143;
inline int read() {
int x;
scanf(" %d", &x);
return x;
}
const int N = 343434;
int n;
int a[N];
set<int> big;
double res = 0;
double pw2[100];
void calc(int x) {
vector<int> imp_left, imp_right;
auto mid = big.lower_bound(x);
auto it = mid;
for (int tt = 0; it != big.end() && tt < 40; tt++) {
imp_right.push_back(*it);
it++;
}
it = mid;
for (int tt = 0; tt < 40; tt++) {
if (it == big.begin()) break;
--it;
imp_left.push_back(*it);
}
imp_left.push_back(0);
imp_right.push_back(n + 1);
double val = (double)a[x] / n / n;
for (int i = 0; i < imp_left.size(); i++) {
for (int j = 0; j < imp_right.size(); j++) {
if (i + j > 40) break;
int cl = ((i == 0) ? x : imp_left[i - 1]) - imp_left[i];
int cr = imp_right[j] - ((j == 0) ? x : imp_right[j - 1]);
res += val * pw2[i + j + 1] * cl * cr;
}
}
}
int main() {
pw2[0] = 1;
for (int i = 1; i < 100; i++) pw2[i] = pw2[i - 1] / 2.0;
n = read();
vector<pair<int, int> > evs;
for (int i = 1; i <= n; i++) {
a[i] = read();
evs.push_back(pair<int, int>(a[i], i));
}
sort((evs).begin(), (evs).end());
reverse((evs).begin(), (evs).end());
for (int i = 0; i < n; i++) {
calc(evs[i].second);
big.insert(evs[i].second);
}
printf("%.10lf\n", res);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #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;
}
int N, a[310000];
pair<int, int> p[310000];
set<int> st;
double calc(int u) {
double c1, c2, wei;
int cnt, pre;
set<int>::iterator it;
st.insert(u);
c1 = 0;
cnt = 0;
wei = 1;
for (it = st.find(pre = u); cnt < 50; pre = *it) {
if (*it == -1) break;
it--;
cnt++;
c1 += wei * (pre - *it);
wei /= 2;
}
c2 = 0;
cnt = 0;
wei = 1;
for (it = st.find(pre = u); cnt < 50; pre = *it) {
if (*it == N) break;
it++;
cnt++;
c2 += wei * (*it - pre);
wei /= 2;
}
return c1 * c2;
}
int main() {
int i;
double ans;
scanf("%d", &N);
for (i = 0; i < N; i++) {
scanf("%d", &a[i]);
p[i] = pair<int, int>(-a[i], i);
}
st.insert(-1);
st.insert(N);
sort(p, p + N);
for (i = 0; i < N; i++) ans += calc(p[i].second) * a[p[i].second];
printf("%.10lf\n", ans / 2 / N / N);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N;
const int NMax = 300005;
const int MaxS = 30;
double Array[NMax];
double ans;
int id[NMax], L[NMax], R[NMax];
inline bool cmp(int a, int b) { return Array[a] < Array[b]; }
void Read() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%lf", &Array[i]), id[i] = i;
sort(id + 1, id + N + 1, cmp);
}
void Solve() {
for (int i = 1; i <= N; i++) L[i] = i - 1, R[i] = i + 1;
for (int i = 1; i <= N; i++) {
double cL = 0, cR = 0, cnt = 0;
double coef = 1, s = 1;
for (int j = id[i]; j > 0 && cnt <= MaxS; cnt++, j = L[j]) {
cL += coef * (j - L[j]);
coef *= 0.5;
s += j - L[j];
}
cnt = 0;
coef = 1, s = 1;
for (int j = id[i]; j <= N && cnt <= MaxS; cnt++, j = R[j]) {
cR += coef * (R[j] - j);
coef *= 0.5;
s += R[j] - j;
}
ans += cL * cR * Array[id[i]];
L[R[id[i]]] = L[id[i]];
R[L[id[i]]] = R[id[i]];
}
cout << fixed << setprecision(15) << ans / 2 / N / N;
}
int main() {
Read();
Solve();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 300010;
int n;
pair<int, int> a[MAXN];
int l[MAXN], r[MAXN];
int main() {
scanf("%d", &n);
int x;
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
a[i] = make_pair(x, i);
l[i] = i - 1;
r[i] = i + 1;
}
sort(a + 1, a + n + 1);
double ans = 0;
for (int i = 1; i <= n; ++i) {
x = a[i].second;
int lx = l[x], rx = r[x];
r[lx] = rx, l[rx] = lx;
long double lans = 0, rans = 0, t = 1;
int cur = x;
for (int j = 1; j < 40; ++j)
if (cur) {
lans += (cur - l[cur]) * t;
t /= 2;
cur = l[cur];
} else
break;
cur = x, t = 1;
for (int j = 1; j < 40; ++j)
if (cur <= n) {
rans += (r[cur] - cur) * t;
t /= 2;
cur = r[cur];
} else
break;
ans += lans * rans * a[i].first / 2;
}
printf("%.8lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
T abs(T x) {
return x < 0 ? -x : x;
}
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
const int MAXK = 50;
int main(int argc, char **argv) {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<pair<int, int> > a;
a.resize(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i + 1;
}
sort(a.rbegin(), a.rend());
set<int> s;
s.insert(0);
s.insert(n + 1);
long double ans = 0;
for (int I = 0; I < n; ++I) {
int i = a[I].second;
s.insert(i);
set<int>::iterator it;
int p;
long double l, r;
l = r = 0;
it = s.find(i);
p = i;
for (int j = 1; j <= MAXK && it-- != s.begin(); ++j) {
l += (p - *it) / (long double)(1LL << j);
p = *it;
}
it = s.find(i);
p = i;
for (int j = 1; j <= MAXK && ++it != s.end(); ++j) {
r += (*it - p) / (long double)(1LL << j);
p = *it;
}
ans += l * r * a[I].first * 2;
}
cout.precision(10);
cout << fixed << ans / n / n << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int k = 60;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(20) << fixed;
int n;
cin >> n;
vector<pair<int, int>> b;
for (int i = 0; i < n; i++) {
int val;
cin >> val;
b.push_back({val, i});
}
sort(b.begin(), b.end());
set<int> s;
for (int i = 0; i < n; i++) s.insert(i);
s.insert(-1);
s.insert(n);
vector<long double> zlomok(k + 1, 1);
for (int i = 1; i <= k; i++) {
zlomok[i] = zlomok[i - 1] / 2.0;
}
long double vys = 0;
for (int it = 0; it < n; it++) {
vector<int> l, r;
int i = b[it].second;
for (auto p = s.find(i);; p--) {
l.push_back(*p);
if (l.size() > k || p == s.begin()) {
break;
}
}
for (auto p = s.find(i);; p++) {
r.push_back(*p);
if (r.size() > k || next(p) == s.end()) {
break;
}
}
double sucl = 0;
double sucr = 0;
for (int li = 0; li < l.size() - 1; li++) {
sucl += (l[li] - l[li + 1]) * zlomok[li];
}
for (int ri = 0; ri < r.size() - 1; ri++) {
sucr += (r[ri + 1] - r[ri]) * zlomok[ri];
}
vys += sucl * sucr * 0.5 * b[it].first;
s.erase(i);
}
vys /= n * 1.0 * n;
cout << vys << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long INF = 1e18;
const int N = 500010;
int n, b[N], l[N], r[N], id[N];
long double ans;
int Write[20];
int read() {
int d = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') d = (d << 3) + (d << 1) + c - 48, c = getchar();
return d * f;
}
void write(int x) {
int t = 0;
if (x < 0) putchar('-'), x = -x;
for (; x; x /= 10) Write[++t] = x % 10;
if (!t) putchar('0');
for (int i = t; i >= 1; i--) putchar((char)(Write[i] + 48));
}
void judge() {
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
}
bool cmp(int x, int y) { return b[x] < b[y]; }
int main() {
n = read();
for (int i = 1; i <= n; i++)
b[i] = read(), l[i] = i - 1, r[i] = i + 1, id[i] = i;
sort(id + 1, id + 1 + n, cmp);
for (int pjy = 1; pjy <= n; pjy++) {
int i = id[pjy], x = i, y = i;
long double L = 0, R = 0, z = 1;
for (int j = 1; j <= 100; j++) {
if (x) {
L += (x - l[x]) * z;
x = l[x];
}
if (y <= n) {
R += (r[y] - y) * z;
y = r[y];
}
z /= 2;
}
l[r[i]] = l[i];
r[l[i]] = r[i];
ans += L * R * b[i] / 2;
}
ans = ans / n / n;
double x = ans;
printf("%.10lf", x);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
pair<int, int> pin;
const int maxn = 300005;
inline void read(int &x) {
x = 0;
int 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();
}
x *= f;
}
int a[maxn], n, p[maxn], l[maxn], r[maxn];
inline void judge() {
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
inline bool cmp(int x, int y) { return a[x] < a[y]; }
inline void baoli() {
double ans = 0;
int b[205];
for (int i = (1); i <= (n); i++)
for (int j = (i); j <= (n); j++) {
int counter = 0;
for (int k = (i); k <= (j); k++) b[++counter] = a[k];
sort(b + 1, b + 1 + counter);
reverse(b + 1, b + 1 + counter);
for (int k = (1); k <= (counter); k++)
ans = ans + (double)b[k] / (double)(1 << k);
}
printf("%.6lf\n", ans / n / n);
}
int main() {
read(n);
for (int i = (1); i <= (n); i++)
read(a[i]), p[i] = i, l[i] = i - 1, r[i] = i + 1;
if (n <= 200) {
baoli();
return 0;
}
sort(p + 1, p + 1 + n, cmp);
double ans = 0;
for (int i = (1); i <= (n); i++) {
int k = p[i];
double L = 0, R = 0, z = 1;
int x = k, y = k;
for (int p = (1); p <= (55); p++) {
if (x) {
L += (x - l[x]) * z;
x = l[x];
}
if (y <= n) {
R += (r[y] - y) * z;
y = r[y];
}
z /= 2;
}
ans += a[k] * L * R / 2 / n / n;
l[r[k]] = l[k];
r[l[k]] = r[k];
}
printf("%.6lf\n", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 300 * 1000;
const int MAXK = 50;
const int INF = 1000 * 1000 * 1000;
int a[MAXN + 2];
int l[MAXN + 2][MAXK];
int r[MAXN + 2][MAXK];
int cntl[MAXN + 2], cntr[MAXN + 2];
set<pair<int, int> > is;
long long two[MAXK + 10];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
two[0] = 1;
for (int i = 1; i < MAXK + 10; i++) two[i] = two[i - 1] * 2;
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
cntl[i] = cntr[i] = 1;
l[i][0] = r[i][0] = i;
}
a[0] = a[n + 1] = INF;
is.insert(make_pair(-a[0], 0));
for (int i = 1; i <= n + 1; i++) {
auto it = is.insert(make_pair(-a[i], i)).first;
it++;
while (it != is.end()) {
r[it->second][cntr[it->second]++] = i;
if (cntr[it->second] == MAXK) {
auto buf = it;
it++;
is.erase(buf);
} else {
it++;
}
}
}
is.clear();
is.insert(make_pair(-a[n + 1], n + 1));
for (int i = n; i >= 0; i--) {
auto it = is.insert(make_pair(-a[i], i)).first;
it++;
while (it != is.end()) {
l[it->second][cntl[it->second]++] = i;
if (cntl[it->second] == MAXK) {
auto buf = it;
it++;
is.erase(buf);
} else {
it++;
}
}
}
double ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < MAXK; j++) {
for (int L = 0; L <= min(j, cntl[i] - 1); L++) {
int R = j - L;
if (cntr[i] - 1 > R) {
int cntl = l[i][L] - l[i][L + 1], cntr = r[i][R + 1] - r[i][R];
ans += (double)cntl * cntr * a[i] / two[j + 1];
}
}
}
}
cout.setf(ios::fixed);
cout.precision(20);
cout << ans / n / n;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, a[300010], ord[300010], b[300010];
const int st = 55;
vector<int> lb[300010], rb[300010];
set<int> A;
void read(int &x) {
char ch = getchar();
int mark = 1;
for (; ch != '-' && (ch < '0' || ch > '9'); ch = getchar())
;
if (ch == '-') mark = -1, ch = getchar();
for (x = 0; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48;
x *= mark;
}
inline bool cmp(int x, int y) {
if (a[x] == a[y]) return x > y;
return a[x] > a[y];
}
int main() {
long double ans = 0;
read(n);
for (int i = 1; i <= n; i++) read(a[i]), ord[i] = i;
sort(ord + 1, ord + n + 1, cmp);
for (int i = 1; i <= n; i++) b[ord[i]] = i;
A.clear();
for (int i = 1; i <= n; i++) {
A.insert(b[i]);
set<int>::iterator it = A.lower_bound(b[i]), tmp;
for (; it != A.end();) {
rb[ord[*it]].push_back(i);
tmp = it;
it++;
if (rb[ord[*tmp]].size() == st) A.erase(tmp);
}
}
A.clear();
for (int i = n; i; i--) {
A.insert(b[i]);
set<int>::iterator it = A.lower_bound(b[i]), tmp;
for (; it != A.end();) {
lb[ord[*it]].push_back(i);
tmp = it;
it++;
if (lb[ord[*tmp]].size() == st) A.erase(tmp);
}
}
for (int i = 1; i <= n; i++) {
if (lb[i].size() < st) lb[i].push_back(0);
if (rb[i].size() < st) rb[i].push_back(n + 1);
long double tmp = 0, di = 1;
for (int j = 0; j < rb[i].size() - 1; j++, di *= 2)
tmp += (long long)a[i] * (rb[i][j + 1] - rb[i][j]) / di;
di = 1;
for (int j = 0; j < lb[i].size() - 1; j++)
di *= 2, ans += tmp * (lb[i][j] - lb[i][j + 1]) / di;
}
ans /= (long long)n * n;
printf("%.9lf\n", (double)ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #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 maxn = (int)3e5;
int a[maxn];
pair<int, int> ps[maxn];
double res[maxn];
bool solve() {
int n;
if (scanf("%d", &n) < 1) return 0;
for (int i = 0; i < n; ++i) scanf("%d", &a[i]), ps[i] = make_pair(a[i], i);
sort(ps, ps + n);
for (int i = 0; i < n; ++i) res[i] = (double)a[i] / 2.0 / n / n;
for (int rev = 0; rev < 2; ++rev) {
set<int> poss;
poss.insert(-1);
poss.insert(n);
for (int i = n - 1; i >= 0; --i) {
int pos = ps[i].second;
if (rev) pos = n - 1 - pos;
set<int>::iterator iter = poss.lower_bound(pos);
double cur = 0, two = 1.0;
int last = pos;
for (int coef = 0; coef < 30 && iter != poss.end();
++coef, ++iter, two /= 2) {
cur += two * (*iter - last);
last = *iter;
}
res[ps[i].second] *= cur;
poss.insert(pos);
}
}
double ans = 0;
for (int i = 0; i < n; ++i) ans += res[i];
printf("%.18lf\n", ans);
return 1;
}
int main() {
srand(rdtsc());
while (1) {
if (!solve()) break;
}
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 400000;
const int NUM = 40;
int n;
int a[MAX], b[MAX], pre[MAX], suf[MAX];
long double Pow[NUM];
int cmp(int l, int r) { return a[l] < a[r]; }
int main() {
int i;
scanf("%d", &n);
for ((i) = (1); (i) <= (n); ++(i)) {
scanf("%d", &a[i]);
b[i] = i;
}
sort(b + 1, b + n + 1, cmp);
for ((i) = (1); (i) <= (n); ++(i)) {
suf[i] = i + 1;
pre[i] = i - 1;
}
Pow[0] = 1;
for ((i) = (1); (i) != (NUM); ++(i)) Pow[i] = Pow[i - 1] * 0.5;
long double ans = 0;
for (int k = 1; k <= n; ++k) {
int u = b[k];
long double now = 0, sum = 0;
for (int i = u, j = 0; i && j < NUM; ++j, i = pre[i])
now += (i - pre[i]) * Pow[j];
for (int i = u, j = 0; i != n + 1 && j < NUM; ++j, i = suf[i])
sum += now * (suf[i] - i) * Pow[j];
ans += sum * a[u];
pre[suf[u]] = pre[u];
suf[pre[u]] = suf[u];
}
printf("%.6lf\n", (double)ans / n / n / 2);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #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;
}
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 _;
}
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 = mul(_tmp, _tmp, _mod);
if (_x & 1) _tmp = mul(_tmp, _a, _mod);
return _tmp;
}
long long mymul(long long _a, long long _x, long long _mod) {
if (_x == 0) return 0ll;
long long _tmp = mymul(_a, _x / 2, _mod);
_tmp = add(_tmp, _tmp, _mod);
if (_x & 1) _tmp = add(_tmp, _a, _mod);
return _tmp;
}
inline bool equal(double _x, double _y) {
return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
double p2[45];
void build() {
p2[0] = 1;
for (int i = 1; i < 45; i++) p2[i] = p2[i - 1] * 2.0;
}
int n;
int a[303030];
vector<pair<int, int> > v;
void init() {
n = getint();
for (int i = 1; i <= n; i++) {
a[i] = getint();
v.push_back({a[i], i});
}
sort((v).begin(), (v).end());
}
int tlft[303030], trgt[303030];
void solve() {
for (int i = 1; i <= n; i++) {
tlft[i] = i - 1;
trgt[i] = i + 1;
}
double ans = 0.0;
for (size_t _ = 0; _ < v.size(); _++) {
int i = v[_].second;
double tl = 0.0, tr = 0.0;
int pl = i, pr = i;
for (int j = 0; j < 45; j++) {
if (pl != 0) tl += (pl - tlft[pl]) / p2[j], pl = tlft[pl];
if (pr <= n) tr += (trgt[pr] - pr) / p2[j], pr = trgt[pr];
}
tlft[trgt[i]] = tlft[i];
trgt[tlft[i]] = trgt[i];
ans += tl * tr * a[i] * 0.5;
}
printf("%.12f\n", ans / ((double)n * (double)n));
}
int main() {
build();
while (__--) {
init();
solve();
}
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, a[300000 + 1], seq[300000 + 1], l[300000 + 1], r[300000 + 1], al[60 + 1],
ar[60 + 1];
double d[60 + 1];
inline bool cmp(int x, int y) { return a[x] < a[y]; }
int main(int argc, char *argv[]) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
for (int i = 1; i <= n; ++i) seq[i] = i, l[i] = i - 1, r[i] = i + 1;
sort(seq + 1, seq + n + 1, cmp);
d[0] = 1.0;
for (int i = 1; i <= 60; ++i) d[i] = d[i - 1] / 2.0;
double ans = 0.0;
for (int i = 1; i <= n; ++i) {
int lc = 0, rc = 0;
for (int c = 60, p = seq[i]; c > 0 && p > 0; p = l[p], --c) al[lc++] = p;
for (int c = 60, p = seq[i]; c > 0 && p <= n; p = r[p], --c) ar[rc++] = p;
al[lc] = 0, ar[rc] = n + 1;
double lv = 0.0, rv = 0.0;
for (int j = 0; j < lc; ++j) lv += d[j + 1] * (al[j] - al[j + 1]);
for (int j = 0; j < rc; ++j) rv += d[j] * (ar[j + 1] - ar[j]);
ans += a[seq[i]] * lv * rv;
l[r[seq[i]]] = l[seq[i]], r[l[seq[i]]] = r[seq[i]];
}
printf("%.10lf\n", ans / n / n);
fclose(stdin);
fclose(stdout);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
int n;
int b[333333];
vector<pair<int, int> > t;
int rv[333333];
struct Node {
Node *lf;
Node *rg;
int l, r;
int mx;
Node() {
lf = rg = 0;
mx = 0;
}
};
Node *root;
Node *buildTree(int from, int to) {
Node *res = new Node();
res->l = from;
res->r = to;
if (from != to) {
res->lf = buildTree(from, (from + to) / 2);
res->rg = buildTree((from + to) / 2 + 1, to);
}
return res;
}
void setValue(Node *curr, int pos, int val) {
if (!curr) return;
if (curr->l > pos || curr->r < pos) return;
curr->mx = max(curr->mx, val);
setValue(curr->lf, pos, val);
setValue(curr->rg, pos, val);
}
int getBiggerL(Node *curr, int to, int val) {
if (!curr) return -1;
if (curr->mx <= val) return -1;
if (curr->l > to) return -1;
if (curr->l == curr->r) return curr->l;
int p1 = getBiggerL(curr->rg, to, val);
if (p1 != -1) return p1;
return getBiggerL(curr->lf, to, val);
}
int getBiggerR(Node *curr, int from, int val) {
if (!curr) return -1;
if (curr->mx <= val) return -1;
if (curr->r < from) return -1;
if (curr->l == curr->r) return curr->l;
int p1 = getBiggerR(curr->lf, from, val);
if (p1 != -1) return p1;
return getBiggerR(curr->rg, from, val);
}
double pw[111111];
int main() {
pw[0] = 1;
for (int i = (1); i < (111111); i++) pw[i] = pw[i - 1] / 2;
cin >> n;
for (int i = (0); i < (n); i++)
scanf("%d", b + i), t.push_back(pair<int, int>(b[i], i));
sort((t).begin(), (t).end());
root = buildTree(0, n - 1);
for (int i = (0); i < (n); i++) {
rv[t[i].second] = i + 1;
setValue(root, t[i].second, i + 1);
}
const int lim = 22;
double sum = 0;
vector<int> l, r;
double den = 1. / n / n;
l.reserve(lim), r.reserve(lim);
for (int i = (0); i < (n); i++) {
l.clear(), r.clear();
int curr = i;
while (1) {
int pos = getBiggerL(root, curr - 1, rv[i]);
if (pos == -1) {
l.push_back(curr + 1);
break;
} else {
l.push_back(curr - pos);
curr = pos;
}
if (l.size() == lim) break;
}
curr = i;
while (1) {
int pos = getBiggerR(root, curr + 1, rv[i]);
if (pos == -1) {
r.push_back(n - curr);
break;
} else {
r.push_back(pos - curr);
curr = pos;
}
if (r.size() == lim) break;
}
for (int i1 = (0); i1 < (l.size()); i1++)
for (int i2 = (0); i2 < (r.size()); i2++) {
sum += b[i] * den * l[i1] * r[i2] * pw[i1 + i2 + 1];
}
}
printf("%.10lf\n", sum);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1001000;
const int lim = 50;
int a[N], kk[N], nxt[N], pre[N];
long double ans;
int n;
long double p[lim + 10];
bool cmp(int x, int y) { return a[x] < a[y]; }
void work(int id) {
long double ret1 = 0., ret2 = 0.;
int i, x;
for (i = 0, x = id; i <= lim && x; i++, x = pre[x])
ret1 += (x - pre[x]) * p[i];
for (i = 0, x = id; i <= lim && x <= n; i++, x = nxt[x])
ret2 += (nxt[x] - x) * p[i];
ans += a[id] * ret1 * ret2 * 0.5;
nxt[pre[id]] = nxt[id];
pre[nxt[id]] = pre[id];
nxt[id] = pre[id] = 0;
}
int main() {
int i;
p[0] = 1.;
for (i = 1; i <= lim; i++) p[i] = p[i - 1] * 0.5;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= n; i++) kk[i] = i;
sort(kk + 1, kk + 1 + n, cmp);
for (i = 1; i <= n; i++) nxt[i] = i + 1, pre[i] = i - 1;
for (i = 1; i <= n; i++) work(kk[i]);
ans = ans / (long double)n / (long double)n;
printf("%.8f\n", (double)ans);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int key[300005], pos[300005], pre[300005], nxt[300005], n;
const int maxj = 60;
bool cmp(int x, int y) { return key[x] < key[y]; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &key[i]);
pos[i] = i;
pre[i] = i - 1;
nxt[i] = i + 1;
}
sort(pos + 1, pos + n + 1, cmp);
double ans = 0;
for (int i = 1; i <= n; i++) {
int pnt1 = pos[i], pnt2 = pos[i];
double lsum = 0, rsum = 0, haha = 1;
for (int j = 1; j <= maxj; j++) {
if (pnt1) lsum += (pnt1 - pre[pnt1]) * haha, pnt1 = pre[pnt1];
if (pnt2 <= n) rsum += (nxt[pnt2] - pnt2) * haha, pnt2 = nxt[pnt2];
haha /= 2;
}
ans += lsum * rsum * key[pos[i]] / 2 / n / n;
nxt[pre[pos[i]]] = nxt[pos[i]];
pre[nxt[pos[i]]] = pre[pos[i]];
}
printf("%.15lf\n", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
struct node {
int w, p;
node() {}
node(int w, int p) : w(w), p(p) {}
} p[N], now;
int n, x, ls, nx, ny, rs, y;
bool tag;
double ans, s1, s2;
int a[N], nxt[N], pre[N], p1[N], p2[N];
bool cmp(node a, node b) { return a.w < b.w; }
void del(int k) {
nxt[pre[k]] = nxt[k];
pre[nxt[k]] = pre[k];
}
int read() {
int num = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
num = num * 10 + c - '0';
c = getchar();
}
return num;
}
int main() {
int i, j, k;
scanf("%d", &n);
for (i = 1; i <= n; i++) a[i] = read();
for (i = 1; i <= n; i++) p[i] = node(a[i], i);
sort(p + 1, p + 1 + n, cmp);
for (i = 1; i <= n; i++) nxt[i] = i + 1, pre[i] = i - 1;
for (i = 1; i <= n; i++) {
now = p[i];
ls = rs = 0;
x = y = now.p;
while (x && ls <= 30) p1[++ls] = x - pre[x], x = pre[x];
while (y <= n && rs <= 30) p2[++rs] = nxt[y] - y, y = nxt[y];
s1 = s2 = 0;
for (j = 1; j <= ls; j++) s1 += (double)p1[j] / (1ll << j);
for (j = 1; j <= rs; j++) s2 += (double)p2[j] / (1ll << j);
ans += (double)now.w * s1 * s2 * 2.0;
del(now.p);
}
printf("%.6f\n", ans / ((double)n * n));
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline void readi(int &x);
const int maxn = 300005;
int n, b[maxn], id[maxn], rk[maxn];
double fl[maxn], fr[maxn];
bool cmpb(const int &i, const int &j) { return b[i] < b[j]; }
struct node {
double div, dta;
node *lc, *rc;
inline void downdate() {
if (div != 1)
lc->div *= div, lc->dta *= div, rc->div *= div, rc->dta *= div, div = 1;
if (dta != 0) lc->dta += dta, rc->dta += dta, dta = 0;
}
inline void modify(int l, int r, const int &a, const int &b, const double &c,
const double &d) {
if (l >= a && r <= b)
div *= c, dta *= c, dta += d;
else {
int mid = l + r >> 1;
downdate();
if (a <= mid) lc->modify(l, mid, a, b, c, d);
if (b > mid) rc->modify(mid + 1, r, a, b, c, d);
}
}
inline double get(int l, int r, const int &p) {
if (l == r) return dta;
int mid = l + r >> 1;
downdate();
if (p <= mid)
return lc->get(l, mid, p);
else
return rc->get(mid + 1, r, p);
}
} ndl[maxn * 3], *root;
int ns;
node *build(int l, int r) {
node *c = ndl + ns++;
c->dta = 0;
c->div = 1;
if (l == r)
c->lc = c->rc = NULL;
else {
int mid = l + r >> 1;
c->lc = build(l, mid);
c->rc = build(mid + 1, r);
}
return c;
}
void build() { ns = 1, root = build(1, n); }
void init() {
readi(n);
for (int i = 1; i <= n; i++) readi(b[i]), id[i] = i;
sort(id + 1, id + n + 1, cmpb);
for (int i = 1; i <= n; i++) rk[id[i]] = i;
}
void work() {
build();
for (int i = 1; i <= n; i++) {
root->modify(1, n, 1, n, 1, 1);
if (rk[i] > 1) root->modify(1, n, 1, rk[i] - 1, .5, 0);
fl[i] = root->get(1, n, rk[i]) / 2;
}
build();
for (int i = n; i >= 1; i--) {
root->modify(1, n, 1, n, 1, 1);
if (rk[i] > 1) root->modify(1, n, 1, rk[i] - 1, .5, 0);
fr[i] = root->get(1, n, rk[i]) / 2;
}
double ans = 0;
for (int i = 1; i <= n; i++) ans += fl[i] * fr[i] * b[i];
ans /= (double)n * n;
printf("%lf", (double)ans * 2);
}
int main() {
init();
work();
return 0;
}
inline void readi(int &x) {
char c;
for (c = getchar(); c > '9' || c < '0'; c = getchar())
;
x = c ^ '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar())
x = x * 10 + (c ^ '0');
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300010, T = 60;
int i, j, k, l, m, n, o, p, c[N], pre[N], nex[N], L[200], R[200], tr, tl;
set<int> F;
set<int>::iterator pos;
double a[N], sum, tk[N], mi[N], ans;
inline bool cmp(int i, int j) { return a[i] > a[j]; }
void Init() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
c[i] = i;
scanf("%lf", &a[i]);
}
sort(c + 1, c + n + 1, cmp);
mi[0] = 1;
for (i = 1; i <= T; i++) mi[i] = mi[i - 1] * 2.0;
}
void getr(int x) {
pos = F.lower_bound(x);
while (tr < T && pos != F.end()) {
tr++;
R[tr] = (*pos);
pos++;
}
if (pos != F.end()) {
pos++;
R[tr + 1] = (*pos);
} else
R[tr + 1] = n + 1;
}
void getl(int x) {
pos = F.lower_bound(x);
if (pos != F.begin()) {
pos--;
while (tl < T) {
tl++;
L[tl] = (*pos);
if (pos == F.begin()) break;
pos--;
}
}
sum = 0;
int i;
if (pos != F.begin()) {
pos--;
L[tl + 1] = (*pos);
} else
L[tl + 1] = 0;
for (i = 0; i <= tl; i++) sum += (double)(L[i] - L[i + 1]) / mi[i];
}
inline double cul() {
int i, j;
double ans = 0;
for (i = 0; i <= tr; i++) {
ans += sum * (R[i + 1] - R[i]) / mi[i];
}
return ans;
}
void Prepare() {
for (i = 1; i <= n; i++) pre[i] = nex[i] = -1;
F.clear();
F.insert(c[1]);
ans = a[c[1]] * 0.5 * (double)(c[1]) * (double)(n - c[1] + 1);
for (i = 2; i <= n; i++) {
tl = tr = 0;
L[0] = R[0] = c[i];
getl(c[i]);
getr(c[i]);
ans += a[c[i]] * 0.5 * cul();
F.insert(c[i]);
}
ans /= (double)(n) * (double)(n);
}
int main() {
Init();
Prepare();
printf("%.8lf\n", ans);
fclose(stdin);
fclose(stdout);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n;
struct Node {
int pos, v;
} a[N];
int pre[N], nxt[N];
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].v, a[i].pos = i;
sort(a + 1, a + n + 1, [](Node a, Node b) { return a.v < b.v; });
for (int i = 1; i <= n; i++) pre[i] = i - 1, nxt[i] = i + 1;
double ans = 0;
for (int i = 1; i <= n; i++) {
int pos = a[i].pos, l = pos, r = pos;
double cur = 0.5, lv = 0, rv = 0;
for (int _ = 1; _ <= 60; _++, cur /= 2) {
if (l) lv += cur * (l - pre[l]), l = pre[l];
if (r <= n) rv += cur * (nxt[r] - r), r = nxt[r];
}
ans += lv * rv * a[i].v * 2;
nxt[pre[pos]] = nxt[pos];
pre[nxt[pos]] = pre[pos];
}
printf("%.8lf", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, l[N], r[N], p[N], a[N];
double ans, L, R, z;
bool cmp(const int i, const int j) { return a[i] < a[j]; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i), p[i] = i, l[i] = i - 1, r[i] = i + 1;
sort(p + 1, p + n + 1, cmp);
for (int k = 1; k <= n; ++k) {
int i = p[k], x = i, y = i;
z = 1, L = R = 0;
for (int c = 1; c <= 45; ++c) {
if (x) L += (x - l[x]) * z, x = l[x];
if (y <= n) R += (r[y] - y) * z, y = r[y];
z /= 2;
}
l[r[i]] = l[i], r[l[i]] = r[i], ans += L * R * a[i] / 2;
}
printf("%.9lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
} a[300005];
int l[300005], r[300005], n, i, L, R, j;
double p, q, x, ans;
int cmp(node i, node j) { return i.x < j.x; }
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i].x);
a[i].y = i;
}
for (i = 1; i <= n; i++) {
l[i] = i - 1;
r[i] = i + 1;
}
r[n + 1] = n + 1;
sort(a + 1, a + n + 1, cmp);
for (i = 1; i <= n; i++) {
L = R = a[i].y;
x = 1;
p = q = 0;
for (j = 1; j <= 45; j++) {
p += (L - l[L]) * x;
q += (r[R] - R) * x;
x /= 2.0;
L = l[L];
R = r[R];
}
ans += p * q * a[i].x / 2.0;
r[l[a[i].y]] = r[a[i].y];
l[r[a[i].y]] = l[a[i].y];
}
printf("%.10f\n", ans / (n + 0.0) / (n + 0.0));
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native")
using namespace std;
long long gcd(long long i, long long j) {
if (j == 0)
return i;
else
return gcd(j, i % j);
}
template <typename T>
inline T getint() {
T val = 0;
char c;
bool neg = false;
while ((c = getchar()) && !(c >= '0' && c <= '9')) {
neg |= c == '-';
}
do {
val = (val * 10) + c - '0';
} while ((c = getchar()) && (c >= '0' && c <= '9'));
return val * (neg ? -1 : 1);
}
const long long INF = 1e9 + 100;
const int mod = 1000000007;
const long double eps = 1e-10, pi = acosl(-1);
const long long maxN = 300010, maxT = 10010, A = 179, mid = 150;
mt19937 mt_rand(time(0));
long long bp(long long et, long long b) {
b %= mod - 1;
long long res = 1;
for (int i = 30; i >= 0; --i) {
res = (res * res) % mod;
if ((b & (1 << i)) != 0) res = (res * et) % mod;
}
return res;
}
void panic() {
cout << "-1\n";
exit(0);
}
const int byben_size = 23;
vector<int> merge(const vector<int>& a, const vector<int>& b,
bool want_mx = true) {
int s1 = a.size(), s2 = b.size();
int sz = min(s1 + s2, byben_size);
vector<int> r(sz);
if (!want_mx) {
int y1 = 0, y2 = 0;
int y = 0;
while (y < sz && y1 < s1 && y2 < s2) {
if (a[y1] < b[y2])
r[y++] = a[y1++];
else
r[y++] = b[y2++];
}
while (y < sz && y1 < s1) r[y++] = a[y1++];
while (y < sz && y2 < s2) r[y++] = b[y2++];
} else {
int y1 = s1 - 1, y2 = s2 - 1;
int y = sz - 1;
while (y >= 0 && y1 >= 0 && y2 >= 0) {
if (a[y1] > b[y2])
r[y--] = a[y1--];
else
r[y--] = b[y2--];
}
while (y >= 0 && y1 >= 0) r[y--] = a[y1--];
while (y >= 0 && y2 >= 0) r[y--] = b[y2--];
}
return r;
}
vector<int> t[1 << 18];
int sz = 1 << 17;
int a[maxN];
vector<int> l1[maxN], r1[maxN];
inline void add(int l, int r, int id, bool want_mx) {
for (l += sz, r += sz; l <= r; l >>= 1, r >>= 1) {
if (l & 1) {
int v = l++;
t[v].push_back(id);
int szz = t[v].size();
int rr = szz;
--szz;
while (szz > 0 && t[v][szz] < t[v][szz - 1]) {
swap(t[v][szz], t[v][szz - 1]);
--szz;
}
if (rr > byben_size) {
if (want_mx)
t[v].erase(t[v].begin());
else
t[v].pop_back();
}
}
if (~r & 1) {
int v = r--;
t[v].push_back(id);
int szz = t[v].size();
int rr = szz;
--szz;
while (szz > 0 && t[v][szz] < t[v][szz - 1]) {
swap(t[v][szz], t[v][szz - 1]);
--szz;
}
if (rr > byben_size) {
if (want_mx)
t[v].erase(t[v].begin());
else
t[v].pop_back();
}
}
}
}
vector<int> get(int v, bool want_mx) {
vector<int> r;
v |= sz;
for (v = v | sz; v; v >>= 1) {
r = merge(t[v], r, want_mx);
}
return r;
}
long double pw[maxT];
void solve() {
pw[0] = 1;
for (int i = 1; i < maxT; ++i) pw[i] = pw[i - 1] * 2;
int n = getint<int>();
for (int i = 1; i <= n; ++i) {
a[i] = getint<int>();
}
add(0, sz - 1, 0, 1);
for (int i = 1; i <= n; ++i) {
add(0, a[i], i, 1);
l1[i] = get(a[i], 1);
}
for (int i = 0; i < 2 * sz; ++i) t[i].clear();
add(0, sz - 1, n + 1, 0);
for (int i = n; i > 0; --i) {
add(0, a[i], i, 0);
r1[i] = merge({i}, get(a[i] + 1, 0), 0);
}
long double ans = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < l1[i].size(); ++j) {
for (int k = 0; k + 1 < r1[i].size(); ++k) {
long long cnt =
(long long)(l1[i][j] - l1[i][j - 1]) * (r1[i][k + 1] - r1[i][k]);
int pww = k + l1[i].size() - j;
if (pww > byben_size) break;
ans += (long double)cnt / pw[pww] * a[i];
}
}
}
cout << fixed << ans / n / n << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(20);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
void debug(T a, T b) {
;
}
template <class T>
void chmin(T& a, const T& b) {
if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
if (a < b) a = b;
}
namespace std {
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& a) {
out << '(' << a.first << ',' << a.second << ')';
return out;
}
} // namespace std
long long int readL() {
long long int res;
scanf("%I64d", &res);
return res;
}
void printL(long long int res) { printf("%I64d", res); }
int n;
int ar[300005];
set<int> exi;
pair<int, int> ar2[300005];
const int C = 60;
double get(const vector<int>& a) {
double res = 0, div = 1;
;
for (int i = 0; i < (a.size() - 1); ++i) {
res += (a[i + 1] - a[i]) / div;
div *= 2;
}
return res;
}
vector<int> refl(vector<int>& a) {
for (int i = 0; i < (a.size()); ++i) a[i] *= -1;
return a;
}
double getval(int p) {
auto it = exi.find(p);
vector<int> R, L;
R.push_back(*it);
++it;
while (it != exi.end()) {
if (R.size() > C) break;
R.push_back(*it);
++it;
}
R.push_back(n);
it = exi.find(p);
L.push_back(*it);
while (it != exi.begin()) {
if (L.size() > C) break;
--it;
L.push_back(*it);
}
L.push_back(-1);
double sum1 = get(R), sum2 = get(refl(L));
exi.erase(p);
return sum1 * sum2 / 2;
}
int main() {
cin >> n;
for (int i = 0; i < (n); ++i) {
scanf("%d", &ar[i]);
ar2[i] = make_pair(ar[i], i);
}
for (int i = 0; i < (n); ++i) exi.insert(i);
;
;
sort(ar2, ar2 + n);
double res = 0;
for (int i = 0; i < (n); ++i) {
int p = ar2[i].second;
double t = getval(p);
res += t * ar2[i].first;
}
res /= n * (long long int)n;
printf("%.10f\n", res);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int k = 30;
int a[300000];
bool cmp(int x, int y) { return (a[x] == a[y]) ? (x < y) : (a[x] > a[y]); }
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
static int seq[300000];
for (int i = 0; i < n; i++) seq[i] = i;
sort(seq, seq + n, cmp);
static int L[300000], R[300000];
set<int> all;
double ans = 0;
static double weight[k];
weight[0] = 0.5;
for (int i = 1; i < k; i++) weight[i] = weight[i - 1] / 2;
for (int i = 0; i < n; i++) {
int x = seq[i], l, r;
set<int>::iterator p = all.lower_bound(x);
if (p == all.end())
r = n;
else
r = *p;
if (p == all.begin())
l = -1;
else
l = *(--p);
L[x] = l;
R[x] = r;
if (l != -1) R[l] = x;
if (r != n) L[r] = x;
all.insert(x);
static int s0[k], s1[k];
memset(s0, 0, sizeof(s0));
memset(s1, 0, sizeof(s1));
for (int p = x, j = 0; j < k && p != -1; j++) {
s0[j] = p - L[p];
p = L[p];
}
for (int p = x, j = 0; j < k && p != n; j++) {
s1[j] = R[p] - p;
p = R[p];
}
for (int j0 = 0; j0 < k && s0[j0]; j0++)
for (int j1 = 0; j0 + j1 < k && s1[j1]; j1++)
ans += weight[j0 + j1] * s0[j0] * s1[j1] * a[x];
}
printf("%0.10lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")
#pragma GCC target("avx2")
using namespace std;
template <typename T1, typename T2>
inline void mine(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void maxe(T1 &x, T2 y) {
if (x < y) x = y;
}
ostream &operator<<(ostream &a, const vector<int> &b) {
for (auto k : b) cout << k << ' ';
return a;
}
const int MOD = 1000000007;
const int INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const int MX = 300010;
const double EPS = 1e-9;
const int D = 25;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<int> lm[MX];
vector<int> rm[MX];
int a[MX];
int rest[MX];
struct cmp {
bool operator()(int x, int y) const {
return a[x] < a[y] || (a[x] == a[y] && x < y);
}
};
long double pw[3 * D];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
pw[0] = 1;
for (int i = 1; i < 3 * D; i++) pw[i] = pw[i - 1] / (long double)2;
int n;
cin >> n;
set<int, cmp> st;
for (int i = 0; i < n; i++) {
cin >> a[i];
auto it = st.begin();
while (it != st.end() && a[*it] < a[i]) {
--rest[*it];
rm[*it].push_back(i);
if (rest[*it] == 0)
it = st.erase(it);
else
++it;
}
rest[i] = D - 1;
st.insert(i);
rm[i] = {i};
}
st.clear();
for (int i = n - 1; i >= 0; i--) {
auto it = st.begin();
while (it != st.end() && a[*it] <= a[i]) {
--rest[*it];
lm[*it].push_back(i);
if (rest[*it] == 0)
it = st.erase(it);
else
++it;
}
rest[i] = D - 1;
st.insert(i);
lm[i] = {i};
}
long double ans = 0;
for (int i = 0; i < n; i++) {
if ((int)lm[i].size() != D) lm[i].push_back(-1);
reverse(lm[i].begin(), lm[i].end());
if ((int)rm[i].size() != D) rm[i].push_back(n);
for (int l = 1; l < (int)lm[i].size(); ++l) {
for (int r = 0; r < (int)rm[i].size() - 1; ++r) {
int cnt = (int)lm[i].size() - l + r;
long long len =
1ll * (lm[i][l] - lm[i][l - 1]) * (rm[i][r + 1] - rm[i][r]);
ans += pw[cnt] * (long double)len * (long double)a[i];
}
}
}
cout << setprecision(15) << fixed;
cout << ans / (1ll * n * n) << '\n';
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300100;
struct dui {
int id, vl;
} a[N];
int ne[N], pre[N], n, st[100], s[100], top1, top2;
bool cmp(dui x, dui y) { return x.vl < y.vl; }
int main() {
int i, j, k, x;
long double ans, res, tmp1, tmp2;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i].vl);
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
for (i = 1; i <= n; i++) ne[i] = i + 1, pre[i] = i - 1;
ans = 0;
for (i = 1; i <= n; i++) {
x = a[i].id;
res = 0;
s[top1 = 1] = x;
st[top2 = 1] = x;
while ((top1 <= 60) && (pre[s[top1]])) s[++top1] = pre[s[top1 - 1]];
s[++top1] = 0;
while ((top2 <= 60) && (ne[st[top2]] != n + 1))
st[++top2] = ne[st[top2 - 1]];
st[++top2] = n + 1;
tmp1 = 0;
tmp2 = 0.5;
for (j = 1; j < top1; j++) {
tmp1 += tmp2 * (long double)(s[j] - s[j + 1]);
tmp2 /= 2.0;
}
for (j = 1; j < top2; j++) {
res += (long double)(st[j + 1] - st[j]) * tmp1;
tmp1 /= 2.0;
}
res = (res * (long double)a[i].vl / (long double)n / (long double)n);
ans += res;
ne[pre[x]] = ne[x];
pre[ne[x]] = pre[x];
}
double out = ans;
printf("%.9f\n", out);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int L = 1001000;
int l[L], r[L], p[L];
double a[L];
int n;
double ans, st, fi, z;
bool cmp(int i, int j) { return a[i] < a[j]; }
void init(void) {
cin >> n;
int i;
for (i = 1; i <= n; i++) {
scanf("%lf", &a[i]);
p[i] = i;
l[i] = i - 1;
r[i] = i + 1;
}
sort(p + 1, p + n + 1, cmp);
}
void work(void) {
int i, j, k, x, y;
for (k = 1; k <= n; k++) {
i = p[k], x = i, y = i;
z = 1;
st = fi = 0;
for (j = 1; j <= 60; j++) {
if (x) st += (x - l[x]) * z, x = l[x];
if (y <= n) fi += (r[y] - y) * z, y = r[y];
z /= 2;
}
l[r[i]] = l[i], r[l[i]] = r[i], ans += st * fi * a[i] / 2;
}
printf("%.9lf\n", ans / n / n);
}
int main(void) {
init();
work();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, A[300010], L[300010], R[300010];
double sum;
int q[300010];
int cmp(int a, int b) { return A[a] < A[b]; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &A[i]), q[i] = i, L[i] = i - 1, R[i] = i + 1;
sort(q + 1, q + n + 1, cmp);
for (int cas = 1; cas <= n; ++cas) {
int i = q[cas];
double l = 0, r = 0, z = 0.5;
int x = i, y = i;
for (int c = 0; c < 60; ++c, z /= 2) {
if (x) l += (x - L[x]) * z, x = L[x];
if (y <= n) r += (R[y] - y) * z, y = R[y];
}
sum += l * r * A[i] * 2;
L[R[i]] = L[i], R[L[i]] = R[i];
}
printf("%.12lf\n", sum / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1000010, lim = 22;
char buf[1 << 23], *p1 = buf, *p2 = buf;
int l[N], r[N], nxt[N], st[N], tp;
int h[N], n, id[N], rk[N];
double er[31], ans;
int a[31], b[31];
int read() {
int res = 0, fl = 0;
char a =
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
? EOF
: *p1++);
while (a < '0' || a > '9') {
if (a == '-') fl = 1;
a = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
? EOF
: *p1++);
}
while (a >= '0' && a <= '9')
res = res * 10 + a - '0',
a = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
? EOF
: *p1++);
return fl ? -res : res;
}
bool cmp(int a, int b) { return h[a] > h[b]; }
int main() {
int i, j, li, lj;
n = read();
for (i = 1; i <= n; i++) h[i] = read(), id[i] = i;
sort(id + 1, id + n + 1, cmp);
for (i = 1; i <= n; i++) rk[id[i]] = i;
for (er[0] = 1, i = 1; i <= 30; i++) er[i] = er[i - 1] * 2;
for (i = 1; i <= n + 1; i++) {
while (tp && rk[st[tp]] > rk[i]) nxt[st[tp]] = i, tp--;
st[++tp] = i;
}
r[0] = n + 1;
for (i = 1; i <= n; i++) {
memset(a, 0, sizeof(a)), memset(b, 0, sizeof(b));
r[id[i]] = nxt[id[i]], l[id[i]] = l[nxt[id[i]]],
r[l[id[i]]] = l[r[id[i]]] = id[i];
for (li = id[i], lj = 0; lj < lim && li != 0; lj++, li = l[li])
a[lj] = li - l[li];
for (li = id[i], lj = 0; lj < lim && li != n + 1; lj++, li = r[li])
b[lj] = r[li] - li;
double ti = 0;
for (j = 0; j < lim; j++) ti += (double)(b[j]) / er[j];
for (j = 0; j < lim; j++)
ans += (double)(h[id[i]]) * double(a[j]) / er[j + 1] * ti;
}
printf("%.10lf", ans / (double)n / (double)n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double ans;
long long N;
vector<pair<double, int> > ar;
set<int> myset;
set<int>::iterator it;
bool comp(const pair<double, int> &a, const pair<double, int> &b) {
return a.first > b.first;
}
double g(int x) {
double l = 0, r = 0, div;
int last;
myset.insert(ar[x].second);
last = ar[x].second;
it = myset.find(ar[x].second);
div = 1;
for (int i = 1; i <= 41; i++) {
it--;
l += div * (last - *it);
if (*it == -1) break;
div /= 2;
last = *it;
}
last = ar[x].second;
it = myset.find(ar[x].second);
div = 1;
for (int i = 1; i <= 41; i++) {
it++;
r += div * (*it - last);
if (*it == N) break;
div /= 2;
last = *it;
}
return l * r * ar[x].first / 2.0;
}
int main() {
scanf("%lld", &N);
ar.resize(N);
for (int i = 0; i < N; i++) {
scanf("%lf", &ar[i].first);
ar[i].second = i;
}
sort(ar.begin(), ar.end(), comp);
myset.insert(-1);
myset.insert(N);
for (int i = 0; i < N; i++) ans += g(i);
printf("%lf\n", ans / (N * N));
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
const int MAXN = 300010;
int n, a[MAXN];
std::vector<std::pair<int, int> > v;
std::set<int> s;
double solve(int x) {
double two, c1, c2;
int cnt, prev, y;
s.insert(x);
std::set<int>::iterator it = s.find(x);
two = 1.0;
cnt = 0;
prev = x;
c1 = 0.0;
while (cnt < 50) {
y = *(--it);
c1 += (prev - y) * two;
if (y == 0) break;
++cnt;
two /= 2.0;
prev = y;
}
it = s.find(x);
two = 1.0;
cnt = 0;
prev = x;
c2 = 0.0;
while (cnt < 50) {
y = *(++it);
c2 += (y - prev) * two;
if (y == n + 1) break;
++cnt;
two /= 2.0;
prev = y;
}
return c1 * c2;
}
int main() {
int i;
double ans;
scanf("%d", &n);
for (i = 1; i <= n; ++i) scanf("%d", a + i);
for (i = 1; i <= n; ++i) v.push_back(std::make_pair(-a[i], i));
std::sort(v.begin(), v.end());
s.insert(0);
s.insert(n + 1);
ans = 0.0;
for (i = 0; i < n; ++i) ans += solve(v[i].second) * a[v[i].second];
printf("%.9lf", ans / 2.0 / n / n);
fclose(stdin);
fclose(stdout);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
int n, arr[MAXN], all[MAXN];
double answer;
set<int> s;
bool cmp(int l, int r) {
if (arr[l] != arr[r]) return arr[l] > arr[r];
return l > r;
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) all[i] = i;
sort(all, all + n, cmp);
s.insert(-1);
s.insert(n);
for (int i = 0; i < n; i++) {
auto ind = s.lower_bound(all[i]);
double l = 0, r = 0, p = 1;
auto it = ind;
int pre = all[i];
for (int j = 0; j < 50 && ~pre; j++) {
it--;
p /= 2;
l += double(pre - (*it)) * p;
pre = *it;
}
it = ind;
it--;
pre = all[i];
p = 1;
for (int j = 0; j < 50 && pre != n; j++) {
it++;
p /= 2;
r += double((*it) - pre) * p;
pre = *it;
}
answer += arr[all[i]] * l * r * 2;
s.insert(all[i]);
}
answer /= (double)n * n;
cout << fixed << setprecision(12) << answer << endl;
cin >> n;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline 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;
}
double ans = 0;
int n, a[300000 + 5], rk[300000 + 5], ne[300000 + 5], la[300000 + 5];
bool cmp(int x, int y) { return a[x] < a[y]; }
int main() {
n = read();
for (int i = 1; i <= n; ++i) a[i] = read(), rk[i] = i;
sort(rk + 1, rk + n + 1, cmp);
for (int i = 1; i <= n; ++i) ne[i] = i + 1, la[i] = i - 1;
for (int i = 1; i <= n; ++i) {
int l = rk[i], r = rk[i];
double s = 1, tl = 0, tr = 0;
for (int tms = 1; tms <= 100; ++tms) {
if (l) tl += s * (l - la[l]), l = la[l];
if (r <= n) tr += s * (ne[r] - r), r = ne[r];
s *= 0.5;
}
ans += tl * tr * a[rk[i]];
la[ne[rk[i]]] = la[rk[i]];
ne[la[rk[i]]] = ne[rk[i]];
}
printf("%.10lf\n", ans / n / n / 2);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int res = 0;
int neg;
while (true) {
char ch = getchar();
if (ch >= '0' && ch <= '9' || ch == '-') {
if (ch == '-')
neg = -1;
else
neg = 1, res = ch - '0';
break;
}
}
while (true) {
char ch = getchar();
if (ch >= '0' && ch <= '9')
res *= 10, res += ch - '0';
else
break;
}
return res * neg;
}
const int maxn = 300020;
const int maxk = 100020;
const int maxx = 100000;
const int maxm = 1000020;
const int MOd = 1e9 + 7;
int a, dad[maxn], L[maxn], R[maxn];
pair<int, int> ar[maxn];
int findl(int n) {
if (L[n] == n) return n;
return L[n] = findl(L[n]);
}
int findr(int n) {
if (R[n] == n) return n;
return R[n] = findr(R[n]);
}
int main() {
scanf("%d", &a);
for (int i = 1; i <= a; i++) scanf("%d", &ar[i].first), ar[i].second = i;
sort(ar + 1, ar + 1 + a);
for (int i = 1; i <= a; i++) L[i] = R[i] = i;
double ans = 0.0;
for (int i = 1; i <= a; i++) {
double t1 = 0.0, t2 = 0.0, div = 1.0;
int cnt = 0;
R[ar[i].second - 1] = ar[i].second;
L[ar[i].second + 1] = ar[i].second;
for (int l = ar[i].second; l >= 1; l--) {
L[l] = findl(L[l]);
t1 += (double)(l - L[l] + 1) * div;
div /= 2;
++cnt;
if (cnt > 100) break;
l = L[l];
}
div = 1.0;
cnt = 0;
for (int r = ar[i].second; r <= a; r++) {
R[r] = findr(R[r]);
t2 += (double)(R[r] - r + 1) * div;
div /= 2;
++cnt;
if (cnt > 100) break;
r = R[r];
}
ans += (double)ar[i].first * ((double)t1 / a) * ((double)t2 / a) / 2;
}
printf("%.12lf\n", (double)ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 333333;
int a[N];
int pos[N];
set<int> s;
int n;
bool cmp(const int pos1, const int pos2) {
if (a[pos1] != a[pos2]) return a[pos1] > a[pos2];
return pos1 < pos2;
}
double calc(int pos) {
set<int>::iterator sit;
int prev;
double c1 = 0, c2 = 0, two;
two = 1;
sit = s.lower_bound(pos);
sit--;
for (int i = 0, prev = pos; i < 45; i++) {
c1 += two * (prev - *sit);
two /= 2.0;
prev = *sit;
if (*sit == 0) break;
sit--;
}
two = 1;
sit = s.lower_bound(pos);
for (int i = 0, prev = pos; i < 45; i++) {
c2 += two * (*sit - prev);
two /= 2.0;
prev = *sit;
if (*sit == n + 1) break;
sit++;
}
s.insert(pos);
return c1 * c2 / 2.0;
}
void solve() {
sort(pos + 1, pos + n + 1, cmp);
s.insert(0);
s.insert(n + 1);
double ans = 0;
for (int i = 1; i <= n; i++) {
ans += calc(pos[i]) * a[pos[i]];
}
ans = ans / n / n;
printf("%.10f\n", ans);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pos[i] = i;
}
solve();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n;
int t[300300];
void add(int k) {
while (k < 300300) t[k]++, k += (k & -k);
}
int sum(int k, int s = 0) {
while (k) s += t[k], k -= (k & -k);
return s;
}
int b[300300], id[300300];
bool cmp(int i, int j) { return b[i] > b[j]; }
int pre[300300], nxt[300300];
int a[300];
int main() {
int i, j, k, s;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", b + i), id[i] = i;
sort(id + 1, id + n + 1, cmp);
double ans = 0;
double tl, tr, p = 1;
for (i = 0; i <= n + 1; i++) pre[i] = 0, nxt[i] = 1 + n;
int mn, mx, mid;
for (int step = 1; step <= n; step++) {
i = id[step];
s = sum(i);
add(i);
if (!s) {
mn = 0, mx = nxt[0];
} else {
mn = 0, mx = i - 1;
while (mn < mx - 1) {
mid = (mn + mx) / 2;
if (sum(mid) == s)
mx = mid;
else
mn = mid;
}
mn = mx, mx = nxt[mn];
}
pre[i] = mn, nxt[mn] = i;
pre[mx] = i, nxt[i] = mx;
{
tr = 0, p = 1;
a[0] = i, a[1] = nxt[i];
for (k = 1; k <= 100 && a[k - 1] <= n; k++, p /= 2) {
tr += p * (a[k] - a[k - 1]);
a[k + 1] = nxt[a[k]];
}
}
{
tl = 0, p = 1;
a[0] = i, a[1] = pre[i];
for (k = 1; k <= 100 && a[k - 1] >= 0; k++, p /= 2) {
tl += p * (a[k - 1] - a[k]);
a[k + 1] = pre[a[k]];
}
}
ans += tr * tl * b[i];
}
printf("%.10lf\n", ans / n / n / 2);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, i, j, p, b[310000], tx[310000];
double ans, pia[310000], sum[410000], mu[410000];
struct aa {
double mul, sum;
aa operator+(aa b) {
aa c;
c.mul = mul * b.mul;
c.sum = sum + mul * b.sum;
return c;
}
} s[1210000];
bool cmp(int x, int y) {
if (b[x] != b[y]) return b[x] > b[y];
return x < y;
}
aa get(int k, int q, int h, int l, int r) {
if (l <= q && h <= r) return s[k];
if (r <= (q + h) / 2) return get(k * 2, q, (q + h) / 2, l, r);
if ((q + h) / 2 < l) return get(k * 2 + 1, (q + h) / 2 + 1, h, l, r);
return get(k * 2, q, (q + h) / 2, l, r) +
get(k * 2 + 1, (q + h) / 2 + 1, h, l, r);
}
void modify(int k, int q, int h, int x) {
if (q == h) {
s[k].mul /= 2;
s[k].sum /= 2;
} else {
if (x <= (q + h) / 2)
modify(k * 2, q, (q + h) / 2, x);
else
modify(k * 2 + 1, (q + h) / 2 + 1, h, x);
s[k] = s[k * 2] + s[k * 2 + 1];
}
}
void mkt(int k, int q, int h) {
if (q < h) {
mkt(k * 2, q, (q + h) / 2);
mkt(k * 2 + 1, (q + h) / 2 + 1, h);
s[k] = s[k * 2] + s[k * 2 + 1];
} else
s[k].mul = 1, s[k].sum = 0.5;
}
void downdate(int k, int l) {
sum[k] *= mu[k];
if (l > 1) {
mu[k * 2] *= mu[k];
mu[k * 2 + 1] *= mu[k];
}
mu[k] = 1;
}
void mul(int k, int q, int h, int l, int r) {
downdate(k, h - q + 1);
if (l <= q && h <= r)
mu[k] /= 2;
else {
if (r <= (q + h) / 2)
mul(k * 2, q, (q + h) / 2, l, r);
else if ((q + h) / 2 < l)
mul(k * 2 + 1, (q + h) / 2 + 1, h, l, r);
else
mul(k * 2, q, (q + h) / 2, l, r),
mul(k * 2 + 1, (q + h) / 2 + 1, h, l, r);
sum[k] = sum[k * 2] * mu[k * 2] + sum[k * 2 + 1] * mu[k * 2 + 1];
}
}
void inc(int k, int q, int h, int x, double y) {
downdate(k, h - q + 1);
if (q == h)
sum[k] += y;
else {
if (x <= (q + h) / 2)
inc(k * 2, q, (q + h) / 2, x, y);
else
inc(k * 2 + 1, (q + h) / 2 + 1, h, x, y);
sum[k] = sum[k * 2] * mu[k * 2] + sum[k * 2 + 1] * mu[k * 2 + 1];
}
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &b[i]);
for (i = 1; i <= n; i++) tx[i] = i;
sort(tx + 1, tx + n + 1, cmp);
mkt(1, 1, n);
for (i = 1; i <= n; i++) {
pia[tx[i]] = get(1, 1, n, tx[i], n).sum;
modify(1, 1, n, tx[i]);
}
for (i = n; i; i--) {
mul(1, 1, 100000, 1, b[i]);
inc(1, 1, 100000, b[i], b[i] * pia[i]);
ans += sum[1] * mu[1];
}
printf("%.10lf\n", ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXBUF = 1 << 23;
char B[MAXBUF], *Si = B, *Ti = B;
inline char getc() {
if (Si == Ti) Ti = (Si = B) + fread(B, 1, MAXBUF, stdin);
if (Si == Ti)
return 0;
else
return *Si++;
}
template <class T>
inline void read(T &a) {
static char c;
static int fh;
while (((c = getc()) < '0' || c > '9') && c != '-')
;
if (c == '-')
fh = -1, a = 0;
else
fh = 1, a = c - '0';
while ((c = getc()) <= '9' && c >= '0') a = (a << 3) + (a << 1) + c - '0';
if (fh == -1) a = -a;
}
char Buff[MAXBUF], *sti = Buff;
template <class T>
inline void write(T a) {
if (a == 0) {
*sti++ = '0';
return;
}
if (a < 0) *sti++ = '-', a = -a;
static char c[20];
static int c0;
c0 = 0;
while (a) c[c0++] = a % 10 + '0', a /= 10;
while (c0--) *sti++ = c[c0];
}
int a[1000005], n;
int pre[1000005], nxt[1000005];
int sl[85], sr[85], cl = 0, cr = 0;
int pl[85], pr[85];
double ans = 0.0;
struct node {
int num, id;
} p[1000005];
inline bool cmp(node a, node b) {
if (a.num != b.num) return a.num < b.num;
return a.id < b.id;
}
int main() {
int i, j;
read(n);
double orz = 0.5;
orz /= ((double)(n));
orz /= ((double)(n));
nxt[0] = 1;
pre[n + 1] = n;
for (i = 1; i <= n; i++) {
nxt[i] = i + 1;
pre[i] = i - 1;
}
for (i = 1; i <= n; i++) {
read(a[i]);
p[i].num = a[i];
p[i].id = i;
}
sort(p + 1, p + n + 1, cmp);
for (i = 1; i <= n; i++) {
cl = cr = 0;
for (j = p[i].id; j != n + 1 && cr <= 80; j = nxt[j]) pr[++cr] = j;
for (j = p[i].id; j && cl <= 80; j = pre[j]) pl[++cl] = j;
pl[++cl] = 0;
pr[++cr] = n + 1;
pre[nxt[p[i].id]] = pre[p[i].id];
nxt[pre[p[i].id]] = nxt[p[i].id];
double c1 = 0.0, c2 = 0.0, tp = 1.0;
for (j = 1; j < cl; j++) {
sl[j] = pl[j] - pl[j + 1];
c1 += 1.0 * sl[j] * tp;
tp *= 0.5;
}
tp = 1.0;
for (j = 1; j < cr; j++) {
sr[j] = pr[j + 1] - pr[j];
c2 += 1.0 * sr[j] * tp;
tp *= 0.5;
}
double ovo = c1 * c2 * orz;
ovo *= ((double)(p[i].num));
ans += ovo;
}
printf("%.12lf\n", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
bool debug = 1;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long ln, lk, lm;
pair<int, int> a[300105];
int pre[300105], nxt[300105];
double ans;
int main() {
scanf("%d", &n);
for (int(i) = 1; (i) <= (int)(n); (i)++) {
scanf("%d", &a[i].first);
a[i].second = i;
pre[i] = i - 1;
nxt[i] = i + 1;
}
sort(a + 1, a + 1 + n);
for (int(i) = 1; (i) <= (int)(n); (i)++) {
int pos = a[i].second, val = a[i].first;
int lp = pos, rp = pos;
double l = 0, r = 0, mi = 1;
for (int(j) = 0; (j) < (int)(50); (j)++) {
mi *= .5;
if (lp) {
l += mi * (lp - pre[lp]);
lp = pre[lp];
}
if (rp <= n) {
r += mi * (nxt[rp] - rp);
rp = nxt[rp];
}
}
ans += 2 * val * l * r;
pre[nxt[pos]] = pre[pos];
nxt[pre[pos]] = nxt[pos];
}
printf("%.10lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 3000 * 100 + 10, LOG = 65;
pair<int, int> a[N];
set<int> s;
int le[LOG], ri[LOG];
long double p[LOG];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i;
sort(a, a + n);
p[0] = 1;
for (int i = 1; i < LOG; i++) p[i] = p[i - 1] / (long double)2;
long double ans = 0;
for (int i = n - 1; i >= 0; i--) {
int vl = a[i].first, ind = a[i].second;
s.insert(ind);
auto itl = s.find(ind), itr = itl;
int cl = 0, cr = 0;
for (int j = 0; j < LOG; j++) {
le[cl++] = *itl;
if (itl == s.begin()) {
if (cl < LOG - 1) le[cl++] = -1;
break;
}
itl--;
}
for (int j = 0; j < LOG; j++) {
ri[cr++] = *itr;
itr++;
if (itr == s.end()) {
if (cr < LOG - 1) ri[cr++] = n;
break;
}
}
long double sum = 0;
for (int j = 1; j < cr; j++) sum += (long double)(ri[j] - ri[j - 1]) * p[j];
for (int j = 1; j < cl; j++)
ans += (long double)(p[j - 1]) * (le[j - 1] - le[j]) * sum * vl /
(long double)((long double)n * n);
}
cout << fixed << setprecision(10) << ans << endl;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int mo = 1000000007;
inline int IN() {
char ch;
(ch = getchar());
int f = 0, x = 0;
for (; ((ch) == '\n' || (ch) == '\r' || (ch) == '\t' || (ch) == ' ');
(ch = getchar()))
;
if (ch == '-') f = 1, (ch = getchar());
for (; !((ch) == '\n' || (ch) == '\r' || (ch) == '\t' || (ch) == ' ');
(ch = getchar()))
x = x * 10 + ch - '0';
return (f) ? (-x) : (x);
}
int Pow(int x, int y, int p) {
int A = 1;
for (; y; y >>= 1, x = (long long)x * x % p)
if (y & 1) A = (long long)A * x % p;
return A;
}
int n, b[N], l[N], r[N];
double a[N], ans;
inline int cmp(const int &A, const int &B) { return a[A] < a[B]; }
int main() {
scanf("%d", &n);
for (int i = (int)1; i <= (int)n; i++) scanf("%lf", a + i);
for (int i = (int)1; i <= (int)n; i++) b[i] = i;
sort(b + 1, b + n + 1, cmp);
for (int i = (int)1; i <= (int)n; i++) l[i] = i - 1, r[i] = i + 1;
for (int i = (int)1; i <= (int)n; i++) {
int x = b[i];
double resL = 0, resR = 0, z = 1;
int L = x, R = x;
for (int c = (int)1; c <= (int)60; c++) {
if (L) resL += (L - l[L]) * z, L = l[L];
if (R <= n) resR += (r[R] - R) * z, R = r[R];
z *= .5;
}
r[l[x]] = r[x];
l[r[x]] = l[x];
l[x] = r[x] = 0;
ans += resL * resR * a[x] * .5;
}
printf("%.10lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = int(3e5) + 50, K = 60, inf = ~0u >> 2;
int B[N], n, cLeft[N][K], cRight[N][K], buf[N];
set<int> S;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> B[i];
B[0] = B[n + 1] = inf;
for (int i = 0; i <= n + 1; ++i) buf[i] = i;
sort(buf, buf + n + 2,
[](int a, int b) { return B[a] == B[b] ? a < b : B[a] > B[b]; });
for (int _ = 0; _ <= n + 1; ++_) {
int i = buf[_], k;
auto ret = S.insert(i);
auto it = ret.first;
for (k = 0, it = ret.first; it != S.begin() && k < K; ++k, --it)
cLeft[i][k] = *it - *prev(it);
for (k = 0, it = ret.first; next(it) != S.end() && k < K; ++k, ++it)
cRight[i][k] = *next(it) - *it;
}
long double res = 0;
for (int i = 1; i <= n; ++i) {
long double sumLeft = 0, sumRight = 0, cpow = 1;
for (int j = 0; j < K; ++j) {
sumLeft += cLeft[i][j] * cpow;
sumRight += cRight[i][j] * cpow;
cpow *= 0.5;
}
res += B[i] * sumLeft * sumRight * 0.5;
}
res /= (long double)n * n;
cout.setf(ios::fixed);
cout << setprecision(10) << res << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 300010;
const int TINY = 1e-13;
int N;
double b[MAXN];
pair<double, int> first[MAXN];
const int MAXS = 24;
int Ls, Rs;
int L[MAXS], R[MAXS];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(12) << fixed;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> b[i];
b[i] += i * TINY;
}
for (int i = 0; i < N; i++) first[i] = make_pair(b[i], i);
sort(first, first + N);
multiset<int> greater;
double ans = 0;
for (int i = N - 1; i >= 0; i--) {
Ls = Rs = 0;
multiset<int>::iterator it = greater.lower_bound(first[i].second);
while (it != greater.begin() && Ls < MAXS) {
it--;
L[Ls++] = *it;
}
if (Ls < MAXS) L[Ls++] = -1;
for (it = greater.upper_bound(first[i].second);
it != greater.end() && Rs < MAXS; it++)
R[Rs++] = *it;
if (Rs < MAXS) R[Rs++] = N;
int prevl = first[i].second;
double coef = 0.5;
double sa = 0;
for (int j = 0; j < Ls; j++) {
sa += coef * double(prevl - L[j]);
prevl = L[j];
coef *= 0.5;
}
int prevr = first[i].second;
coef = 1;
for (int j = 0; j < Rs; j++) {
ans += first[i].first * coef * double(R[j] - prevr) * sa;
prevr = R[j];
coef *= 0.5;
}
greater.insert(first[i].second);
}
cout << ans / (N * double(N)) << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long linf = 1e18 + 5;
const int N = 3e5 + 5;
int n;
pair<int, int> a[N];
set<int> s;
int main() {
ios ::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a + 1, a + n + 1);
reverse(a + 1, a + n + 1);
set<int>::iterator l, r;
s.insert(0);
s.insert(n + 1);
double ans = 0;
for (int i = 1; i <= n; i++) {
l = r = s.upper_bound(a[i].second);
l--;
double left = 0, right = 0;
int bef = a[i].second;
for (int i = 0; i < 60; i++, l--) {
left += (double)(bef - *l) / (1LL << i);
bef = *l;
if (l == s.begin()) break;
}
bef = a[i].second;
for (int i = 0; i < 60 and r != s.end(); i++, r++) {
right += (double)(*r - bef) / (1LL << i);
bef = *r;
}
ans += left * right * a[i].first;
s.insert(a[i].second);
}
cout << fixed << setprecision(12) << ans / n / n / 2 << '\n';
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
return x > 0 ? x : -x;
}
long long n;
int m;
int c[10];
int i[10];
int d[18];
int main() {
scanf("%I64d %d", &n, &m);
for (int j = 0; j < 10; ++j) c[j] = 0;
for (; n > 0; n /= 10) ++c[n % 10];
long long f[c[0] + 1][c[1] + 1][c[2] + 1][c[3] + 1][c[4] + 1][c[5] + 1]
[c[6] + 1][c[7] + 1][c[8] + 1][c[9] + 1][m];
memset(f, 0, sizeof f);
bool all = true;
f[0][0][0][0][0][0][0][0][0][0][0] = 1;
for (i[0] = 0; i[0] <= c[0]; ++i[0])
for (i[1] = 0; i[1] <= c[1]; ++i[1])
for (i[2] = 0; i[2] <= c[2]; ++i[2])
for (i[3] = 0; i[3] <= c[3]; ++i[3])
for (i[4] = 0; i[4] <= c[4]; ++i[4])
for (i[5] = 0; i[5] <= c[5]; ++i[5])
for (i[6] = 0; i[6] <= c[6]; ++i[6])
for (i[7] = 0; i[7] <= c[7]; ++i[7])
for (i[8] = 0; i[8] <= c[8]; ++i[8])
for (i[9] = 0; i[9] <= c[9]; ++i[9]) {
for (int j = 0; j < m; ++j)
for (int k = 0; k < 10; ++k)
if (i[k] < c[k] and (!all or (all and k))) {
long long add = f[i[0]][i[1]][i[2]][i[3]][i[4]]
[i[5]][i[6]][i[7]][i[8]][i[9]][j];
++i[k];
f[i[0]][i[1]][i[2]][i[3]][i[4]][i[5]][i[6]][i[7]]
[i[8]][i[9]][(j * 10 + k) % m] += add;
--i[k];
}
all = false;
}
printf("%I64d\n",
f[c[0]][c[1]][c[2]][c[3]][c[4]][c[5]][c[6]][c[7]][c[8]][c[9]][0]);
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.util.*;
import java.io.*;
public class Main {
FastScanner in;
PrintWriter out;
final int DIGITS = 10;
int[] cnt = new int[DIGITS];
class Masks implements Comparable<Masks>{
int x, y;
Masks(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Masks o) {
return Integer.compare(o.y, y);
}
}
int decrMask(int x, int pos) {
int pw = 1;
for (int i = DIGITS - 1; i > pos; --i)
pw *= cnt[i] + 1;
int car = x % pw;
x /= pw;
--x;
return x * pw + car;
}
int getBit(int x, int pos) {
int pw = 1;
for (int i = DIGITS - 1; i > pos; --i)
pw *= cnt[i] + 1;
return x / pw % (cnt[pos] + 1);
}
int cntBits(int x) {
int ret = 0;
for (int i = 0; i < DIGITS; ++i)
ret += getBit(x, i);
return ret;
}
public void solve() throws IOException {
long n = in.nextLong();
int m = in.nextInt();
long nn = n;
int len = 0;
while (nn > 0) {
cnt[(int)(nn % DIGITS)]++;
nn /= 10;
++len;
}
int tot = 1;
for (int i = 0; i < DIGITS; ++i)
tot *= cnt[i] + 1;
Masks[] ms = new Masks[tot];
for (int i = 0; i < tot; ++i)
ms[i] = new Masks (i, cntBits(i));
Arrays.sort(ms);
//System.out.println(decrMask(7, 1));
long[][] dp = new long[tot][m];
dp[tot - 1][0] = 1;
for (int i = 0; i < tot; ++i) {
int curms = ms[i].x;
int beg = 0;
if (i == 0) beg = 1;
for (int dig = beg; dig < DIGITS; ++dig)
if (getBit(curms, dig) > 0)
for (int ost = 0; ost < m; ++ost) {
//System.out.println(curms + " " + decrMask(curms, dig));
dp[decrMask(curms, dig)][(ost * DIGITS + dig) % m] += dp[curms][ost];
}
}
out.println(dp[0][0]);
}
void run() {
try {
in = new FastScanner();
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] arg) {
new Main().run();
//run();
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) {
solve();
}
static int mod, state[] = new int[11];
static Long dp[][];
private static void solve() {
char num[] = IN.next().toCharArray();
int cnt[] = new int[10];
for (int i = 0; i < num.length; i++) {
cnt[num[i] - '0']++;
}
state[0] = 1;
for (int i = 1; i <= 10; i++) {
state[i] = state[i - 1] * (cnt[i - 1] + 1);
}
dp = new Long[mod = IN.nextInt()][state[10] + 1];
System.out.println(dp(num.length - 1, 0, state[10], num, true));
}
public static long dp(int pos, int cur, int mask, char num[], boolean leadZero) {
if (pos < 0) {
return cur == 0 ? 1 : 0;
}
if (dp[cur][mask] != null) {
return dp[cur][mask];
}
long res = 0;
int t, seen = 0;
for (int i = pos; i >= 0; i--) {
t = num[i] - '0';
if (leadZero && t == 0 || (seen & (1 << t)) != 0) {
continue;
}
seen |= (1 << t);
swap(num, pos, i);
res += dp(pos - 1, (cur * 10 + t) % mod, mask - state[t], num, false);
swap(num, pos, i);
}
return dp[cur][mask] = res;
}
public static void swap(char num[], int i, int j) {
char tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}
static class IN {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 65535);
private static StringTokenizer st = null;
public static String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
break;
}
}
return st.nextToken();
}
public static int nextInt() {
return Integer.valueOf(next());
}
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int M, cnt[10], base[10], mult[11];
long long dp[27000][100];
int main() {
long long n;
cin >> n >> M;
while (n) {
cnt[n % 10]++;
n /= 10;
}
mult[0] = 1;
for (int i = 0; i < 10; i++) {
base[i] = cnt[i] + 1;
mult[i + 1] = mult[i] * base[i];
}
for (int i = 1; i < 10; i++)
if (cnt[i]) dp[mult[i]][i % M] = 1;
for (int s = 1; s < mult[10]; s++)
for (int i = 0; i < 10; i++)
if (s / mult[i] % base[i] < cnt[i])
for (int v = 0; v < M; v++)
dp[s + mult[i]][(10 * v + i) % M] += dp[s][v];
cout << dp[mult[10] - 1][0] << endl;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") {
for (Tp current = _begin; current != _end; ++current) {
std::cout << *current << _delim;
}
std::cout << '\n';
}
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);
ll dp[1 << 18][100];
int cnt[10];
bool HasBit(int x, int b) { return (x >> b) & 1; }
ll Fact(int x) {
ll res = 1;
for (int i = 2; i <= x; ++i) {
res *= i;
}
return res;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n;
int mod;
cin >> n >> mod;
const auto s = to_string(n);
for (char c : s) {
++cnt[c - '0'];
}
const int len = static_cast<int>((s).size());
const int full_mask = 1 << len;
for (int i = 0; i < (len); ++i) {
if (s[i] != '0') {
dp[1 << i][(s[i] - '0') % mod] = 1;
}
}
for (int mask = 1; mask < full_mask; ++mask) {
for (int r = 0; r < mod; ++r) {
for (int i = 0; i < (len); ++i) {
if (!HasBit(mask, i)) {
dp[mask | (1 << i)][(r * 10 + s[i] - '0') % mod] += dp[mask][r];
}
}
}
}
ll d = 1;
for (int i = 0; i < (10); ++i) {
d *= Fact(cnt[i]);
}
cout << dp[full_mask - 1][0] / d << "\n";
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
template <typename T, size_t N>
int SIZE(const T (&t)[N]) {
return N;
}
template <typename T>
int SIZE(const T &t) {
return t.size();
}
string to_string(const string s, int x1 = 0, int x2 = 1e9) {
return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char c) { return string({c}); }
template <size_t N>
string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) {
string t = "";
for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
__iii__ <= __jjj__; ++__iii__) {
t += b[__iii__] + '0';
}
return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B>
string to_string(const pair<A, B> &p) {
l_v_l_v_l++;
string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
l_v_l_v_l--;
return res;
}
template <typename A, typename... C>
string to_string(const A(&v), int x1, int x2, C... coords) {
int rnk = rank<A>::value;
string tab(t_a_b_s, ' ');
string res = "";
bool first = true;
if (l_v_l_v_l == 0) res += '\n';
res += tab + "[";
x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
auto l = begin(v);
advance(l, x1);
auto r = l;
advance(r, (x2 - x1) + (x2 < SIZE(v)));
for (auto e = l; e != r; e = next(e)) {
if (!first) {
res += ", ";
}
first = false;
l_v_l_v_l++;
if (e != l) {
if (rnk > 1) {
res += '\n';
t_a_b_s = l_v_l_v_l;
};
} else {
t_a_b_s = 0;
}
res += to_string(*e, coords...);
l_v_l_v_l--;
}
res += "]";
if (l_v_l_v_l == 0) res += '\n';
return res;
}
void dbgm() { ; }
template <typename Heads, typename... Tails>
void dbgm(Heads H, Tails... T) {
cerr << to_string(H) << " | ";
dbgm(T...);
}
const long long mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr << "...............Console is yours! :)................."
<< "\n";
long long n;
int m;
cin >> n >> m;
string num = to_string(n);
int l = num.length();
long long dp[1 << l][m];
memset(dp, 0, sizeof(dp));
long long fac = 1;
int count[10];
memset(count, 0, sizeof(count));
for (int i = 0; i < l; i++) fac *= ++count[num[i] -= '0'];
dp[0][0] = 1;
for (int i = 0; i < 1 << l; i++) {
for (int j = 0; j < l; j++) {
if (!(i & (1 << j)) && (i || num[j])) {
for (int k = 0; k < m; k++)
dp[i | (1 << j)][(k * 10 + num[j]) % m] += dp[i][k];
}
}
}
cout << dp[(1 << l) - 1][0] / fac << "\n";
cerr << "......^_^....."
<< "\n";
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
string n;
int nsz;
int m;
long long dp[1 << 18][100];
long long rec(int a, int b, int mask) {
if (b == nsz) return (long long)(a == 0);
if (dp[mask][a] >= 0) return dp[mask][a];
dp[mask][a] = 0LL;
for (int i = 0; i < nsz; i++) {
if (((mask >> i) & 1) ||
(i > 0 && n[i - 1] == n[i] && ((mask >> (i - 1)) & 1) == 0))
continue;
dp[mask][a] += rec((10 * a + (n[i] - '0')) % m, b + 1, mask | (1 << i));
}
return dp[mask][a];
}
int main() {
cin >> n >> m;
sort(n.begin(), n.end());
nsz = n.size();
for (long long i = 0; i < 1 << 18; i++) {
for (long long j = 0; j < 100; j++) {
dp[i][j] = -1LL;
}
}
for (int i = 0; i < nsz; i++) {
if (n[i] == '0') dp[1 << i][0] = 0;
}
cout << rec(0, 0, 0) << endl;
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
struct point {
int a;
unsigned long long b;
bool operator<(const point A) const {
if (a != A.a) return a < A.a;
return b < A.b;
}
};
int l;
unsigned long long d[30], r[30], two[30], n, m;
int wh[(1 << 18) + 10];
queue<point> Q;
unsigned long long dp[110][(1 << 18) + 10];
int lowbit(int t) { return t & (-t); };
int main() {
int i, j, k, last;
bool f, fq;
point A, B;
while (~scanf("%I64u%I64u", &n, &m)) {
unsigned long long t = n;
l = 0;
while (t > 0) {
d[++l] = t % 10;
t /= 10;
}
sort(d + 1, d + l + 1);
r[0] = 0;
r[l] = 1;
for (i = l - 1; i > 0; i--) r[i] = (r[i + 1] * 10) % m;
two[0] = 1;
for (i = 1; i <= l; i++) two[i] = (two[i - 1] << 1);
wh[0] = 0;
for (i = 1; i < (1 << l); i++) wh[i] = wh[i - lowbit(i)] + 1;
for (i = 0; i < m; i++)
for (j = 0; j < (1 << l); j++) dp[i][j] = 0;
while (!Q.empty()) Q.pop();
A.a = 0;
A.b = (1uLL << l) - 1;
dp[A.a][A.b] = 1;
Q.push(A);
last = wh[A.b];
i = -1;
last = -1;
while (!Q.empty()) {
A = Q.front();
Q.pop();
if (wh[A.b] != last) i++;
last = wh[A.b];
f = false;
for (k = 1; k <= l; k++) {
if (f && d[k] == d[k - 1]) continue;
if (k == 1 || d[k] != d[k - 1]) f = 0;
if (i == 0 && d[k] == 0) continue;
if (two[k - 1] & A.b) {
f = 1;
B.a = (d[k] * r[i + 1] + A.a) % m;
B.b = A.b ^ two[k - 1];
if (dp[B.a][B.b] == 0) {
dp[B.a][B.b] = dp[A.a][A.b];
Q.push(B);
} else
dp[B.a][B.b] += dp[A.a][A.b];
}
}
}
printf("%I64u\n", dp[0][0]);
}
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long v;
int n, m;
long long dp[1 << 18][100];
int num[100], cnt[100];
int N;
void solve() {
N = 1 << n;
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
if (num[i] > 0) dp[1 << i][num[i] % m] += 1;
}
for (int i = 1; i < N; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
if ((i & (1 << k)) > 0) continue;
dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j];
}
}
}
long long ans = dp[N - 1][0];
for (int i = 0; i <= 9; i++) {
for (int j = 1; j <= cnt[i]; j++) {
ans /= j;
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin >> v >> m;
n = 0;
while (v) {
num[n++] = v % 10;
cnt[v % 10]++;
v /= 10;
}
solve();
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 18;
const int maxm = 1e6 + 10;
const int INF = 0x3f3f3f3f;
long long casn, n, m, k;
long long dp[maxn][100];
int num[123];
int tms[123];
int len;
long long pw[123];
int main() {
int a, b, c;
cin >> n >> m;
long long x = n;
while (x) {
num[len++] = x % 10;
tms[x % 10]++;
x /= 10;
}
sort(num, num + len);
pw[0] = 1;
for (int i = 1; i < 20; i++) pw[i] = pw[i - 1] * i;
for (int i = 0; i < len; i++)
if (num[i]) dp[1 << i][num[i] % m] = 1;
for (int i = 1; i < (1 << len); i++)
for (int j = 0; j < len; j++)
if (i & (1 << j))
for (int k = 0; k < m; k++)
dp[i][(k * 10 + num[j]) % m] += dp[i ^ (1 << j)][k];
for (int i = 0; i < 10; i++) dp[(1 << len) - 1][0] /= pw[tms[i]];
cout << dp[(1 << len) - 1][0] << endl;
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRomanAndNumbers solver = new DRomanAndNumbers();
solver.solve(1, in, out);
out.close();
}
static class DRomanAndNumbers {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
char[] s = in.scanString().toCharArray();
int m = in.scanInt();
int n = s.length;
int[] si = new int[n];
for (int i = 0; i < n; i++) si[i] = s[i] - '0';
Arrays.sort(si);
long[][] dp = new long[1 << n][m];
dp[0][0] = 1;
for (int i = 0; i < 1 << n; i++) {
for (int j = 0; j < m; j++) {
if (dp[i][j] == 0) continue;
for (int k = 0; k < n; k++) {
if ((i & 1 << k) == 0) {
if (i == 0 && si[k] == 0) continue;
if (k > 0 && si[k] == si[k - 1] && (i & 1 << k - 1) == 0) continue;
dp[i | 1 << k][(j * 10 + si[k]) % m] += dp[i][j];
}
}
}
}
out.println(dp[(1 << n) - 1][0]);
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public String scanString() {
int c = scan();
if (c == -1) return null;
while (isWhiteSpace(c)) c = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.util.*;
public class d {
static int m;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
long x = input.nextLong();
int n = (x+"").length();
m = input.nextInt();
int[]counts = new int[10];
for(int i = 0; i<n; i++)
{
counts[(int)(x%10)]++;
x/=10;
}
long res = 0;
memo = new HashMap<Long, Long>();
for(int i= 1; i<10; i++)
{
if(counts[i] == 0) continue;
counts[i]--;
res += go(counts, i%m);
counts[i]++;
}
System.out.println(res);
}
static HashMap<Long, Long> memo;
static long go(int[] counts, int mod)
{
boolean done = true;
for(int i = 0; i<10; i++)
{
if(counts[i] != 0)
{
done = false;
break;
}
}
if(done)
{
return mod == 0 ? 1 : 0;
}
long key = encode(counts)*101+mod;
if(memo.containsKey(key)) return memo.get(key);
long res = 0;
for(int i = 0; i<10; i++)
{
if(counts[i] == 0) continue;
counts[i]--;
res += go(counts, ((mod*10) + i)%m);
counts[i]++;
}
memo.put(key, res);
return res;
}
static long encode(int[] counts)
{
long res = 0;
for(int i = 0; i<10; i++)
{
res += counts[i];
res *= 20;
}
return res;
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Abood2C {
static int N;
static long memo[][];
static int MOD;
static String s;
static long solve(int i, int m) {
if(i == 0 && m == 0)
return 1;
if(i == 0)
return 0;
if(memo[i][m] != -1)
return memo[i][m];
long ans = 0;
int p = 1;
int c = 0;
for (int j = 0; j < N; j++) {
if((i & (1 << j)) == 0)
c++;
}
for (int j = 0; j < c; j++) {
p *= 10;
p %= MOD;
}
for (int j = 0; j < N; j++) {
if((i & (1 << j)) == 0 || s.charAt(j) == '0' && (i ^ (1 << j)) == 0)
continue;
boolean v = false;
for (int j2 = 0; j2 < j; j2++)
if((i & (1 << j2)) != 0 && s.charAt(j) == s.charAt(j2)) {
v = true;
continue;
}
if(v)
continue;
ans += solve((i ^ (1 << j)), ((s.charAt(j) - '0') * p + m) % MOD);
}
return memo[i][m] = ans;
}
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
s = sc.next();
N = s.length();
MOD = sc.nextInt();
memo = new long [(1 << N)][MOD + 1];
for (int i = 0; i < memo.length; i++)
Arrays.fill(memo[i], -1);
out.println(solve(memo.length - 1, 0));
out.flush();
out.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream System){ br = new BufferedReader(new InputStreamReader(System));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine()throws IOException{return br.readLine();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public char nextChar()throws IOException{return next().charAt(0);}
public Long nextLong()throws IOException{return Long.parseLong(next());}
public boolean ready() throws IOException{return br.ready();}
public void waitForInput(){for(long i = 0; i < 3e9; i++);}
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long n, fact[19], dp[1 << 18][100];
int m, td, dig[18], f[10];
int main() {
scanf("%lld %d", &n, &m);
while (n) {
dig[td++] = n % 10;
f[n % 10]++;
n /= 10;
}
for (int i = 0; i < td; i++) {
if (dig[i] == 0) continue;
dp[(1 << i)][dig[i] % m]++;
}
for (int mask = 0; mask < (1 << td); mask++) {
for (int rem = 0; rem < m; rem++) {
for (int j = 0; j < td; j++) {
if (mask & (1 << j)) continue;
dp[mask | (1 << j)][(rem * 10 + dig[j]) % m] += dp[mask][rem];
}
}
}
long long ans = dp[(1 << td) - 1][0];
fact[0] = 1;
for (int i = 1; i <= 18; i++) {
fact[i] = i * fact[i - 1];
}
for (int i = 0; i < 10; i++) {
ans /= fact[f[i]];
}
printf("%lld\n", ans);
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
const long long inf = 2e18;
using namespace std;
template <class T, class T2>
void smin(T& a, T2 val) {
if (a > val) a = val;
}
template <class T, class T2>
void smax(T& a, T2 val) {
if (a < val) a = val;
}
const int N = 5 * (int)1e5 + 10;
long long n, mod;
long long dp[1LL << 18][100];
vector<int> digits;
void solve() {
cin >> n >> mod;
vector<int> f(10);
while (n) {
digits.push_back(n % 10);
f[digits.back()]++;
n /= 10;
}
long long count = 1;
for (int i = 0; i < (int)(digits.size()); ++i) {
count *= 1LL * f[digits[i]];
f[digits[i]]--;
}
n = (int)(digits.size());
int mx = (1LL << n);
dp[mx - 1][0] = 1;
for (int msk = mx - 2; msk >= 0; --msk) {
for (int rem = 0; rem < mod; ++rem) {
for (int i = 0; i < n; ++i) {
if (msk & (1LL << i)) {
continue;
}
if (msk == 0 and digits[i] == 0) {
continue;
}
dp[msk][rem] += dp[(msk | (1LL << i))][(rem * 10 + digits[i]) % mod];
}
}
}
long long ans = dp[0][0];
ans /= count;
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long w[1 << 18][128], d = 1;
char n[88];
int main() {
int l, m, t, c[16] = {0};
cin >> n >> m;
l = strlen(n), t = 1 << l;
w[0][0] = 1;
for (int i = 0; i < l; ++i) d *= ++c[n[i] -= '0'];
for (int i = 0; i < t; ++i)
for (int j = 0; j < l; ++j)
if (!(i & 1 << j))
if (i || n[j])
for (int k = 0; k < m; ++k)
w[i | 1 << j][(k * 10 + n[j]) % m] += w[i][k];
cout << w[t - 1][0] / d << endl;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRomanAndNumbers solver = new DRomanAndNumbers();
solver.solve(1, in, out);
out.close();
}
static class DRomanAndNumbers {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
char[] s = in.scanString().toCharArray();
int m = in.scanInt();
long[][] dp = new long[1 << s.length][m];
for (int i = 0; i < s.length; i++) {
if (s[i] != '0') dp[1 << i][(s[i] - '0') % m] = 1;
}
int[] si = new int[s.length];
for (int i = 0; i < s.length; i++) si[i] = s[i] - '0';
long[] fact = new long[20];
fact[0] = 1;
for (int i = 1; i < 20; i++) fact[i] = fact[i - 1] * i;
for (int i = 1; i < 1 << s.length; i++) {
for (int j = 0; j < m; j++) {
if (dp[i][j] == 0) continue;
for (int k = 0; k < s.length; k++) {
if ((i & (1 << k)) == 0) {
dp[i ^ (1 << k)][(j * 10 + (si[k])) % m] += dp[i][j];
}
}
}
}
long ans = dp[(1 << s.length) - 1][0];
int[] count = new int[10];
for (char c : s) count[c - '0']++;
for (int i : count) ans /= fact[i];
out.println(ans);
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public String scanString() {
int c = scan();
if (c == -1) return null;
while (isWhiteSpace(c)) c = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.util.*;
import java.io.*;
public class Main{
BufferedReader in;
StringTokenizer str = null;
private String next() throws Exception{
if (str == null || !str.hasMoreElements())
str = new StringTokenizer(in.readLine());
return str.nextToken();
}
private int nextInt() throws Exception{
return Integer.parseInt(next());
}
private long nextLong() throws Exception{
return Long.parseLong(next());
}
long n;
int m, l;
long dp[][];
char []a;
public void run() throws Exception{
in = new BufferedReader(new InputStreamReader(System.in));
n = nextLong();
m = nextInt();
a = Long.toString(n).toCharArray();
l = a.length;
Arrays.sort(a);
//System.out.println(Arrays.toString(a));
dp = new long[1<<l][m];
for(long[]i : dp)
Arrays.fill(i, -1);
long r = go((1<<l)-1, 0, 0);
System.out.println(r);
}
private long go(int mask, int mod, int len){
//System.out.println(mask + " " + mod + " " + len);
if (len == l){
return mod == 0 ? 1L : 0;
}
if (dp[mask][mod] != -1){
return dp[mask][mod];
}
long ans = 0;
for(int i=0;i<l;i++){
if ((mask & (1<<i)) > 0){
if (len == 0 && a[i] == '0') continue;
if (i > 0 && a[i] == a[i-1] && (mask & (1<<(i-1))) > 0) continue;
ans+=go(mask ^ (1 << i), (mod * 10 + a[i] - '0')%m, len + 1);
}
}
return dp[mask][mod] = ans;
}
public static void main(String args[]) throws Exception{
new Main().run();
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 2005;
long long f[1 << 18][101];
string s;
int n, mod, Pow[20];
long long lui(int st, int remain) {
if (st == 0) return remain == 0;
if (f[st][remain] != -1) return f[st][remain];
long long res = 0, cnt = 0;
for (int i = 0; i < n; i++) cnt += (st >> i) & 1;
int dd[12] = {};
for (int i = 0; i < n; i++) {
if ((st >> i) & 1 && dd[s[i] - '0'] == 0) {
if (s[i] != '0' || st != (1 << n) - 1) {
int n_st = st - (1 << i),
n_remain = (remain + (s[i] - '0') * Pow[cnt - 1]) % mod;
res = res + lui(n_st, n_remain);
dd[s[i] - '0'] = 1;
}
}
}
return f[st][remain] = res;
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> s >> mod;
n = s.length();
Pow[0] = 1;
for (int i = 1; i <= n; i++) Pow[i] = Pow[i - 1] * 10 % mod;
memset(f, -1, sizeof f);
cout << lui((1 << n) - 1, 0);
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long MAXN = (1 << 18);
const long long MAXM = 100;
string n;
long long lgn, m;
void read() { cin >> n >> m; }
long long dp[MAXN][MAXM];
long long rec(long long mask, long long ost) {
long long i = __builtin_popcount(mask);
if (i == lgn) return (ost == 0);
long long &memo = dp[mask][ost];
if (memo != -1) return memo;
memo = 0;
bool used[10];
for (long long dig = 0; dig < 10; dig++) used[dig] = false;
for (long long idx = 0; idx < lgn; idx++)
if (!(mask & (1ll << idx)))
if (!(i == 0 && n[idx] == '0') && !used[n[idx] - '0'])
memo += rec(mask ^ (1ll << idx), (ost * 10 + (n[idx] - '0')) % m),
used[n[idx] - '0'] = true;
return memo;
}
void solve() {
memset(dp, -1, sizeof(dp));
lgn = n.size();
cout << rec(0, 0) << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long int dp[300000][105];
int used[15];
int a[20];
int n, m;
string s;
int main() {
cin >> s >> m;
n = s.size();
for (int i = 0; i < n; i++) {
a[i] = s[i] - '0';
}
dp[0][0] = 1;
for (int mask = 0; mask < (1 << n); mask++) {
for (int mod = 0; mod < m; mod++) {
for (int k = 0; k < n; k++) {
if (used[a[k]]) continue;
if (a[k] == 0 && mask == 0) continue;
if (mask & (1 << (k))) continue;
dp[mask | (1 << (k))][(mod * 10 + a[k]) % m] += dp[mask][mod];
used[a[k]] = 1;
}
memset(used, 0, sizeof(used));
}
}
cout << dp[(1 << n) - 1][0];
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long n;
int m, a[20], tot;
long long f[(1 << 18) + 10][110];
long long dfs(int x, int S, int mod, int zero) {
if (!x) return mod == 0;
if (f[S][mod] != -1) return f[S][mod];
long long res = 0;
bool vis[10];
memset(vis, 0, sizeof(vis));
for (int i = 0; i < tot; i++) {
if (!zero && a[i] == 0) continue;
if (((1 << i) & S) == 0 && !vis[a[i]])
vis[a[i]] = 1, res += dfs(x - 1, (S | (1 << i)), (mod * 10 + a[i]) % m,
(zero | (a[i] != 0)));
}
return f[S][mod] = res;
}
long long solve(long long x) {
tot = 0;
while (x) a[tot++] = x % 10, x /= 10;
return dfs(tot, 0, 0, 0);
}
int main() {
memset(f, -1, sizeof(f));
scanf("%lld%d", &n, &m);
printf("%lld", solve(n));
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
string s;
long long m, dig[29] = {}, l, memo[(1 << 18) + 2][103];
long long dp(int mask, int mod, int bno) {
if (memo[mask][mod] != -1) return memo[mask][mod];
if (bno == l) {
return memo[mask][mod] = 1;
}
memo[mask][mod] = 0;
for (int i = 0; i < l; i++) {
if (mask & (1 << i)) continue;
if (bno == 0 && dig[i] == 0) continue;
memo[mask][mod] += dp(mask | (1 << i), (mod * 10 + dig[i]) % m, bno + 1);
}
return memo[mask][mod];
}
int main() {
memset(memo, 0, sizeof(memo));
cin >> s >> m;
l = s.length();
long long freq[20] = {};
for (int i = 0; i < l; i++) dig[i] = s[i] - '0', freq[dig[i]]++;
memo[0][0] = 1;
for (int i = 0; i < (1 << l); i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < l; k++) {
if (i == 0 && dig[k] == 0) continue;
if (!(i & (1 << k)))
memo[i | (1 << k)][(j * 10 + dig[k]) % m] += memo[i][j];
}
}
}
long long ans = memo[(1 << l) - 1][0];
long long f[20];
f[0] = 1;
for (int i = 1; i < 19; i++) f[i] = f[i - 1] * i;
for (int i = 0; i < 10; i++) ans /= f[freq[i]];
cout << ans;
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf401d_2 {
public static void main(String[] args) throws IOException {
r();
char[] nch = input.nextToken().toCharArray();
int m = ni(), len = nch.length, n[] = new int[len];
for(int i = 0; i < len; ++i) {
n[i] = nch[i] - '0';
}
long fact[] = new long[len + 1], dp[][] = new long[1 << len][m];
fact[0] = 1;
for(int i = 1; i <= len; ++i) {
fact[i] = fact[i - 1] * i;
}
dp[0][0] = 1;
for(int i = 0; i < 1 << len; ++i) {
for(int j = 0; j < len; ++j) {
if((i & (1 << j)) == 0) {
for(int k = 0; k < m; ++k) {
if(i > 0 || n[j] > 0) {
dp[i | (1 << j)][(k * 10 + n[j]) % m] += dp[i][k];
}
}
}
}
}
long ans = dp[(1 << len) - 1][0];
int[] cnt = new int[10];
for(int i : n) {
++cnt[i];
}
for(int i = 0; i < 10; ++i) {
ans /= fact[cnt[i]];
}
prln(ans);
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random rand = new Random();
// references
// IBIG = 1e9 + 7
// IRAND ~= 3e8
// IMAX ~= 2e10
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IRAND = 327859546;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
static int minstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
static long minstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
static int maxstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
static long maxstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int floori(double d) {return (int)d;}
static int ceili(double d) {return (int)ceil(d);}
static long floorl(double d) {return (long)d;}
static long ceill(double d) {return (long)ceil(d);}
static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static <T> void shuffle(T[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); T swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void sort(int[] a) {shuffle(a); Arrays.sort(a);}
static void sort(long[] a) {shuffle(a); Arrays.sort(a);}
static void sort(double[] a) {shuffle(a); Arrays.sort(a);}
static void qsort(int[] a) {Arrays.sort(a);}
static void qsort(long[] a) {Arrays.sort(a);}
static void qsort(double[] a) {Arrays.sort(a);}
static int randInt(int min, int max) {return rand.nextInt(max - min + 1) + min;}
// input
static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
static long rl() throws IOException {return Long.parseLong(__in.readLine());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
static int ni() {return Integer.parseInt(input.nextToken());}
static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
static long nl() {return Long.parseLong(input.nextToken());}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {__out.println("yes");}
static void pry() {__out.println("Yes");}
static void prY() {__out.println("YES");}
static void prno() {__out.println("no");}
static void prn() {__out.println("No");}
static void prN() {__out.println("NO");}
static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next());}
static void h() {__out.println("hlfd");}
static void flush() {__out.flush();}
static void close() {__out.close();}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.util.*;
import java.io.*;
public class TaskD {
private FastScanner in;
private PrintWriter out;
private int MAX_SIZE;
public void solve() throws IOException {
long n = in.nextLong(), m = in.nextLong();
char[] data = Long.toString(n).toCharArray();
int[] digits = new int[data.length];
int[] count = new int[10];
for (int i = 0; i < data.length; i++) {
digits[i] = data[i] - '0';
count[digits[i]]++;
}
MAX_SIZE = digits.length;
long[][] dp = new long[1 << MAX_SIZE][(int) m];
dp[0][0] = 1;
for (int i = 0; i < 1 << MAX_SIZE; i++) {
for (int j = 0; j < MAX_SIZE; j++) {
if ((i & (1 << j)) == 0 && !(i == 0 && digits[j] == 0)) {
int to = (i | (1 << j));
for (int k = 0; k < m; k++) {
dp[to][(int) (((k * 10) + digits[j]) % m)] += dp[i][k];
}
}
}
}
long ans = dp[(1 << MAX_SIZE) - 1][0];
for (int i = 0; i < 10; i++) {
ans /= fact(count[i]);
}
out.print(ans);
}
long fact(int value) {
long res = 1;
for (int i = 1; i <= value; i++) {
res *= i;
}
return res;
}
public void run() {
try {
in = new FastScanner();
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class FastScanner {
private BufferedReader br;
private StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] arg) {
new TaskD().run();
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long n = in.nextLong();
int m = in.nextInt();
int[] number = new int[Long.toString(n).length()];
int[] freq = new int[10];
long[] fact = new long[18];
fact[0] = 1;
for (int i = 1; i < fact.length; i++)
fact[i] = fact[i - 1] * i;
int numberLength = 0;
for (char c : Long.toString(n).toCharArray()) {
freq[c - '0']++;
number[numberLength++] = c - '0';
}
long[][] dp = new long[1 << numberLength][m];
dp[0][0] = 1;
for (int mask = 0; mask < (1 << numberLength); mask++) {
for (int rem = 0; rem < m; rem++) {
if (dp[mask][rem] == 0)
continue;
for (int di = 0; di < numberLength; di++) {
if (mask == 0 && number[di] == 0)
continue;
if ((mask & (1 << di)) != 0)
continue;
dp[mask | (1 << di)][(rem * 10 + number[di]) % m] += dp[mask][rem];
}
}
}
long ans = dp[(1 << numberLength) - 1][0];
for (int i = 0; i < freq.length; i++) {
ans /= fact[freq[i]];
}
out.println(ans);
}
}
static class InputReader {
final InputStream is;
final byte[] buffer = new byte[1024];
int curCharIdx;
int nChars;
public InputReader(InputStream is) {
this.is = is;
}
public int read() {
if (curCharIdx >= nChars) {
try {
curCharIdx = 0;
nChars = is.read(buffer);
if (nChars == -1)
return -1;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return buffer[curCharIdx++];
}
public int nextInt() {
int sign = 1;
int c = skipDelims();
if (c == '-') {
sign = -1;
c = read();
if (isDelim(c))
throw new RuntimeException("Incorrect format");
}
int val = 0;
while (c != -1 && !isDelim(c)) {
if (!isDigit(c))
throw new RuntimeException("Incorrect format");
val = 10 * val + (c - '0');
c = read();
}
return val * sign;
}
public long nextLong() {
int sign = 1;
int c = skipDelims();
if (c == '-') {
sign = -1;
c = read();
if (isDelim(c))
throw new RuntimeException("Incorrect format");
}
long val = 0;
while (c != -1 && !isDelim(c)) {
if (!isDigit(c))
throw new RuntimeException("Incorrect format");
val = 10L * val + (c - '0');
c = read();
}
return val * sign;
}
private final int skipDelims() {
int c = read();
while (isDelim(c)) {
c = read();
}
return c;
}
private static boolean isDelim(final int c) {
return c == ' ' ||
c == '\n' ||
c == '\t' ||
c == '\r' ||
c == '\f';
}
private static boolean isDigit(final int c) {
return '0' <= c && c <= '9';
}
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.concurrent.PriorityBlockingQueue;
public class aa {
public static int mod;
public static long dp[][];
public static int a[];
public static int n;
public static long solve(int mask, int number, int count) {
if (mask == (1 << n) - 1) {
if (number == 0)
return 1;
return 0;
}
if (dp[mask][number] != -1)
return dp[mask][number];
long a1 = 0;
for (int i = 0; i < n; i++) {
if (count != 0 || a[i] != 0) {
if (i > 0 && (mask & (1 << (i - 1))) == 0 && a[i - 1] == a[i])
continue;
if ((mask & (1 << i)) == 0) {
a1 += solve(mask | (1 << i), (number * 10 + a[i]) % mod,
count + 1);
}
}
}
dp[mask][number] = a1;
return a1;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
StringBuilder q = new StringBuilder();
String y[] = in.readLine().trim().split(" ");
String num = y[0];
mod = Integer.parseInt(y[1]);
n = num.length();
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = num.charAt(i) - 48;
}
Arrays.sort(a);
dp = new long[1 << n + 1][mod];
for (int i = 0; i < (1 << n + 1); i++) {
for (int j = 0; j < mod; j++) {
dp[i][j] = -1;
}
}
out.println((long) solve(0, 0, 0));
out.print(q);
out.close();
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.awt.*;
import java.io.*;
import java.util.*;
public class Abc {
static long dp[][];
static int dig[];
static int n;
static int r;
static int m;
public static void main(String[] args) {
FastReader sc = new FastReader();
String s=sc.next();m=sc.nextInt();
n=s.length();
r=(1<<n)-1;
dig=new int[n];
// int repeat[]=new int[10];
for (int i=0;i<n;i++){
dig[i]=s.charAt(i)-'0';
// repeat[dig[i]]++;
}
Arrays.sort(dig);
dp=new long[r+1][m];
for (long arr[]:dp)Arrays.fill(arr,-1);
long ans=dp(0,0);
System.out.println(ans);
}
static long dp(int mask,int rem){
if (mask==r){
if (rem==0)return 1;
}
if (dp[mask][rem]!=-1)return dp[mask][rem];
long ans=0;
for (int i=0;i<n;i++){
if (i>0 && ((mask&(1<<(i-1))))==0 && dig[i-1]==dig[i])continue;
if ((mask&(1<<i))==0 && (mask!=0 || dig[i]!=0)){
ans+=dp((mask|(1<<i)),(10*rem+dig[i])%m);
}
}
return dp[mask][rem]=ans;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, neg = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') neg = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return x * neg;
}
long long n = read(), m = read();
long long a[20], cnt = 0, dp[262144][105], num[10], f[20];
signed main() {
while (n) {
a[++cnt] = n % 10;
n /= 10;
}
f[0] = 1;
for (long long i = 1; i <= 18; i++) f[i] = f[i - 1] * i;
for (long long i = 1; i <= cnt; i++)
if (a[i] != 0) dp[1 << (i - 1)][a[i] % m] = 1;
for (long long i = 0; i < (1 << cnt); i++) {
for (long long j = 0; j < m; j++) {
for (long long k = 1; k <= cnt; k++) {
if (!((i >> (k - 1)) & 1)) {
dp[i | (1 << (k - 1))][(j * 10 + a[k]) % m] += dp[i][j];
}
}
}
}
long long ans = dp[(1 << cnt) - 1][0];
for (long long i = 1; i <= cnt; i++) num[a[i]]++;
for (long long i = 0; i <= 9; i++) ans /= f[num[i]];
printf("%lld\n", ans);
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.io.*;
import java.util.*;
public class D
{
static StringBuilder st = new StringBuilder();
static long [][] memo ;
static char [] s ;
static int mult(int a , int b , int MOD) {return (int)((1l * a * b) % MOD) ; }
static int add(int a , int b , int MOD) {return (a+b) % MOD;}
static int n , m ;
static long dp(int mod , int msk )
{
if(mod == 0 && msk == (1 << n) - 1) return 1 ;
if(memo[mod][msk] != -1)return memo[mod][msk] ;
long ans = 0 ;
boolean [] used = new boolean [10] ;
for(int i = 0 ; i < n ; i++)
{
if((msk == 0 && s[i] - '0' == 0) || used[s[i] - '0']) continue ;
if((msk & (1 << i)) == 0)
{
ans += dp(add((int)(s[i] - '0'), mult(10 , mod , m), m), msk | 1 << i );
used[s[i] - '0'] = true;
}
}
return memo[mod][msk] = ans;
}
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in) ;
PrintWriter out = new PrintWriter(System.out) ;
s = sc.next().toCharArray() ;
n = s.length ;
m = sc.nextInt() ;
memo = new long [m+1][1 << n] ;
for(long [] x : memo) Arrays.fill(x, -1);
out.println(dp(m, 0 ));
out.flush();
out.close();
}
static class Scanner
{
BufferedReader br;
StringTokenizer st;
Scanner(InputStream in)
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws Exception
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws Exception { return Integer.parseInt(next()); }
long nextLong() throws Exception { return Long.parseLong(next()); }
double nextDouble() throws Exception { return Double.parseDouble(next());}
}
static void shuffle(int[] a)
{
int n = a.length;
for (int i = 0; i < n; i++)
{
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | //package codeforces;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class D implements Closeable {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
StringTokenizer stringTokenizer;
String next() throws IOException {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
private double nextDouble() throws IOException {
return Double.parseDouble(next());
}
int MOD = 1000 * 1000 * 1000 + 7;
int sum(int a, int b) {
a += b;
return a >= MOD ? a - MOD : a;
}
int product(int a, int b) {
return (int) (1l * a * b % MOD);
}
@SuppressWarnings("unchecked")
void solve() throws IOException {
long n = nextLong();
final int m = nextInt();
List<Integer> digits = new ArrayList<>();
for(long i = n; i > 0; i /= 10) {
digits.add((int)(i % 10));
}
Collections.sort(digits);
final int[] d = new int[digits.size()];
for(int i = 0; i < digits.size(); i++) {
d[i] = digits.get(i);
}
class Utils {
final boolean bitIsSet(int mask, int bit) {
return (mask & (1 << bit)) > 0;
}
long rec(int mask, int mod) {
if(mask == (1 << d.length) - 1) {
return mod == 0 ? 1 : 0;
}
long result = 0;
for(int i = 0; i < d.length; i++) {
if(mask == 0 && d[i] == 0) {
continue;
}
if(!bitIsSet(mask, i)) {
if(i > 0 && d[i] == d[i - 1] && !bitIsSet(mask, i - 1)) {
continue;
}
result += rec(mask | (1 << i), (mod * 10 + d[i]) % m);
}
}
return result;
}
}
class CachedUtils extends Utils {
boolean[][] hit = new boolean[1 << d.length][m];
long[][] cache = new long[1 << d.length][m];
@Override
long rec(int mask, int mod) {
if(!hit[mask][mod]) {
cache[mask][mod] = super.rec(mask, mod);
hit[mask][mod] = true;
}
return cache[mask][mod];
}
}
writer.println(new CachedUtils().rec(0, 0));
}
public static void main(String[] args) throws IOException, InterruptedException {
try (D d = new D()) {
d.solve();
}
}
@Override
public void close() throws IOException {
reader.close();
writer.close();
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 |
import java.io.*;
import java.util.*;
public class D {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int count(long x) {
int res = 0;
while (x > 0) {
if (x % 2 != 0)
res++;
x /= 2;
}
return res;
}
public void run() {
String s = in.next();
int mod = in.nextInt();
int len = s.length();
long[][] dp = new long[(1<<len)][mod];
int ten = (int)(Math.pow(10, len - 1) % mod);
for (int i = 0; i < len; i++) {
int c = s.charAt(i) - '0';
if (c != 0) dp[(1 << i)][(c * ten) % mod] = 1;
}
for (int i = 0; i < (1 << len); i++) {
int pos = count(i);
ten = (int)(Math.pow(10, len - pos - 1) % mod);
for (int j = 0; j < len; j++) {
if ((i & (1 << j)) != 0) continue;
int c = s.charAt(j) - '0';
int next = i | (1 << j);
for (int k = 0; k < mod; k++) {
dp[next][(k + c * ten) % mod] += dp[i][k];
}
}
}
long res = dp[(1<<len) - 1][0];
int[] cnt = new int[10];
for (int i = 0; i < len; i++) {
cnt[s.charAt(i) - '0']++;
}
for (int i = 0; i < 10; i++) {
for (int j = cnt[i]; j >= 2; j--) {
res /= j;
}
}
System.out.println(res);
out.close();
}
public static void main(String[] args) {
new D().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
System.out.println();
}
public void debug(Object... obj) {
System.out.println(Arrays.deepToString(obj));
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
//stream = new FileInputStream(new File("dec.in"));
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
int[][] nextIntMap(int n, int m) {
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextIntArray(m);
}
return map;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
long[][] nextLongMap(int n, int m) {
long[][] map = new long[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextLongArray(m);
}
return map;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
double[][] nextDoubleMap(int n, int m) {
double[][] map = new double[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextDoubleArray(m);
}
return map;
}
String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
struct st {
int tmp;
int cnt[10];
bool operator<(const st &i) const { return tmp < i.tmp; }
};
map<string, long long> d[20][101];
map<string, long long>::iterator it;
long long n, t;
int m, cnt[10], p;
long long re;
string tmp = "0000000000";
int main(void) {
scanf("%I64d %d", &n, &m);
t = n;
while (t) {
cnt[t % 10]++;
t /= 10;
p++;
}
for (int i = 0; i < 10; i++) {
if (cnt[i]) {
tmp[i]++;
d[0][i % m][tmp]++;
tmp[i]--;
}
}
for (int i = 1, tn = 10 % m; i < p; i++, tn = (tn * 10) % m) {
for (int j = 0; j < m; j++) {
for (it = d[i - 1][j].begin(); it != d[i - 1][j].end(); it++) {
for (int k = 0; k < 10; k++) tmp[k] = (*it).first[k];
for (int k = 0; k < 10; k++) {
if ((k || i + 1 != p) && tmp[k] - '0' < cnt[k]) {
tmp[k]++;
d[i][(j + k * tn) % m][tmp] += (*it).second;
tmp[k]--;
}
}
}
}
}
for (it = d[p - 1][0].begin(); it != d[p - 1][0].end(); it++)
re += (long long)(*it).second;
printf("%I64d\n", re);
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long n;
int f[20], num, m, maxn, g[10];
long long dp[1 << 18][100];
bool flag;
int main() {
memset(f, 0, sizeof(f));
memset(dp, 0, sizeof(dp));
cin >> n >> m;
num = 0;
if (n % m == 0)
flag = true;
else
flag = false;
while (n) {
f[num++] = n % 10;
n = n / 10;
}
maxn = 1 << num;
dp[0][0] = 1;
for (int k = 0; k < num; k++)
if (f[k]) dp[1 << k][f[k] % m] += dp[0][0];
for (int i = 1; i < maxn; i++)
for (int j = 0; j < m; j++) {
if (!dp[i][j]) continue;
for (int k = 0; k < num; k++)
if (!(i & (1 << k))) dp[i | (1 << k)][(j * 10 + f[k]) % m] += dp[i][j];
}
memset(g, 0, sizeof(g));
for (int i = 0; i < num; i++) g[f[i]]++;
long long ans = dp[maxn - 1][0];
for (int i = 0; i < 10; i++)
for (int j = 1; j <= g[i]; j++) ans = ans / j;
cout << ans;
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
/**
* Built using CHelper plug-in Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
TaskD t = new TaskD();
t.solve();
}
}
class TaskD {
long ways[][];
char no[];
int m;
int len;
LinkedList<Integer>[][] asdf;
public void solve() {
Scanner scan = new Scanner(System.in);
no = (scan.nextLong() + "").toCharArray();
len = no.length;
m = scan.nextInt();
ways = new long[1 << len][m];
for (long[] a : ways)
Arrays.fill(a, -1);
// this code is so ugly, but i'm so sleep deprived
asdf = new LinkedList[10][m];
for(int k=0; k < asdf.length; k++){
for(int rem=0; rem < asdf[k].length; rem++){
asdf[k][rem] = new LinkedList<Integer>();
for(int j=0; j < m; j++){
if((j * 10 + k) % m == rem){
asdf[k][rem].add(j);
}
}
}
}
System.out.println(waysToGetRemWithNums((1 << len) - 1, 0, 0));
// for (int i = ways.length - 1; i >= 0; i--) {
// System.out.println(Integer.toBinaryString(i) + ": " + Arrays.toString(ways[i]));
// }
}
long waysToGetRemWithNums(int mask, int rem, int depth) {
depth++;
String tabs = "";
// for (int i = 0; i < depth; i++, tabs = tabs + "\t") ;
// rem = rem*10%m;
if (mask == 0) {
if (rem == 0) {
ways[mask][rem] = 1;
return 1;
}
ways[mask][rem] = 0;
return 0;
}
if (ways[mask][rem] != -1)
return ways[mask][rem];
long ans = 0;
HashSet<Character> charsUsed = new HashSet<Character>();
// System.out.println(tabs + "how many ways with mask " + Integer.toBinaryString(mask) + " to make remainder " + rem +"?");
for (int i = 0; i < len; i++) {
if (((mask >> i) & 1) == 0)
continue;
if (depth == len && no[i] == '0')
continue;
if (charsUsed.contains(no[i]))
continue;
charsUsed.add(no[i]);
// find the remainder that solves (r1*10 + k) %M == r0
int k = no[i] - '0';
for (Integer j : asdf[k][rem]) {
int remainderNeeded = j;
// System.out.println(tabs + "need to make mask " + Integer.toBinaryString((mask & ~(1<<i))));
// System.out.println(tabs + " with remainder " + remainderNeeded);
ans += waysToGetRemWithNums(mask & ~(1 << i), remainderNeeded, depth);
// ans += go(mask | (1 << i), ((rem * 10) + no[i] - '0') % m);
}
}
// System.out.println(tabs + "ans: " + ans);
return ways[mask][rem] = ans;
}
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Himalay([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
Long dp[][];
int mod;
char arr[];
int pow[];
public void solve(int testNumber, Scanner in, PrintWriter out) {
dp = new Long[1<<18][101];
arr = in.next().toCharArray();
mod = in.nextInt();
pow = new int[20];
pow[0] = 1;
for (int i = 1; i < 20; i++) {
pow[i] = (pow[i-1] * 10) % mod;
}
out.println(go(0, 0, 0));
}
private long go(int mask, int m, int bit) {
if (bit == arr.length)
return (m == 0) ? 1 : 0;
if (dp[mask][m] != null)
return dp[mask][m];
int digits = 0;
long ret = 0;
for (int i = 0; i < arr.length; i++) {
if ((mask & (1 << i)) != 0)
continue;
int d = arr[i] - '0';
if (mask == 0 && d == 0)
continue;
if ((digits & (1<<d) ) == 0) {
ret += go(mask | (1 << i), (m + d * pow[arr.length-1-bit]) % mod, bit+1);
digits |= (1 << d);
}
}
return dp[mask][m] = ret;
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 |
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
static int[] count;
static int code(int[] a) {
int ret = 0;
for (int i = 0; i < 10; i++) {
ret = ret * (count[i] + 1) + a[i];
}
return ret;
}
static void decode(int x, int[] a) {
for (int i = 9; i >= 0; i--) {
a[i] = x % (count[i] + 1);
x /= count[i] + 1;
}
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
String s = in.next();
int m = in.nextInt();
count = new int[10];
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - '0']++;
}
int[] cur = new int[10];
int all = 1;
for (int i : count) {
all *= i + 1;
}
long[][] dp = new long[m][all];
dp[0][all - 1] = 1;
for (int state = all - 1; state > 0; state--) {
decode(state, cur);
for (int mod = 0; mod < m; mod++) {
long val = dp[mod][state];
if (val == 0) {
continue;
}
for (int d = 0; d < 10; d++) {
if (state + 1 == all && d == 0 || cur[d] == 0) {
continue;
}
--cur[d];
int nState = code(cur);
int nMod = (mod * 10 + d) % m;
dp[nMod][nState] += val;
++cur[d];
}
}
}
out.println(dp[0][0]);
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException { new Main().bw.close(); }
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = null;
String nxttok() {
try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); }
catch (Exception e) { return ""; }
return st.nextToken();
}
int nxtint() { return Integer.parseInt(nxttok()); }
long nxtlong() { return Long.parseLong(nxttok()); }
long n; int m;
int[] s = new int[10];
int getKey(int[] c) {
int ans = 0;
for (int i = -1; ++ i < 10; ) {
ans = (ans * (s[i] + 1) + c[i]);
}
return ans;
}
int maxn = 19;
int maxm = 101;
long[][] dp = new long[1 << maxn][maxm];
int ts[] = new int[10];
long cal(int mod) {
int k = getKey(ts);
if (k == 0) return mod == 0 ? 1 : 0;
if (dp[k][mod] != -1) return dp[k][mod];
long ans = 0;
for (int i = -1; ++ i < 10; ) {
if (ts[i] == 0) continue;
ts[i]--;
ans += cal((mod * 10 + i) % m);
ts[i]++;
}
return dp[k][mod] = ans;
}
Main() throws IOException {
n = nxtlong(); m = nxtint();
while (n > 0) {
s[(int)(n % 10)]++;
n /= 10;
}
for (int i = -1; ++i < 10;) {
ts[i] = s[i];
}
int k = getKey(s);
for (int i = -1; ++i < k; )
for (int f = -1; ++f < m; )
dp[i][f] = -1;
long ans = 0;
for (int i = 0; ++i < 10; ) {
if (ts[i] == 0) continue;
--ts[i];
ans += cal(i % m);
++ts[i];
}
bw.write("" + ans);
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | import java.util.*;
import java.io.*;
public class RomanandNumbers {
/************************ SOLUTION STARTS HERE ************************/
static int mod;
static int digits[];
static long memo[][];
static long rec(int mask , int sum) {
if(Integer.bitCount(mask) == digits.length)
return sum == 0 ? 1 : 0;
else if(memo[mask][sum] != -1)
return memo[mask][sum];
else {
long ways = 0;
int used = 0;
for(int i = 0; i < digits.length; i++)
if((mask & (1 << i)) == 0) {
if(mask == 0 && digits[i] == 0) // first digit is 0
continue;
if((used & (1 << digits[i])) != 0) // digit already used
continue;
used |= 1 << digits[i];
ways += rec(mask | (1 << i), (sum * 10 + digits[i]) % mod);
}
return memo[mask][sum] = ways;
}
}
private static void solve() {
long n = nextLong();
mod = nextInt();
digits = String.valueOf(n).chars().map(Character::getNumericValue).toArray();
memo = new long[1 << digits.length][mod];
for(long t[] : memo) Arrays.fill(t, -1);
println(rec(0, 0));
}
/************************ SOLUTION ENDS HERE ************************/
/************************ TEMPLATE STARTS HERE **********************/
public static void main(String[] args) throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
st = null;
solve();
reader.close();
writer.close();
}
static BufferedReader reader;
static PrintWriter writer;
static StringTokenizer st;
static String next()
{while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}
st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
static String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}
static int nextInt() {return Integer.parseInt(next());}
static long nextLong() {return Long.parseLong(next());}
static double nextDouble(){return Double.parseDouble(next());}
static char nextChar() {return next().charAt(0);}
static int[] nextIntArray(int n) {int[] a= new int[n]; int i=0;while(i<n){a[i++]=nextInt();} return a;}
static long[] nextLongArray(int n) {long[]a= new long[n]; int i=0;while(i<n){a[i++]=nextLong();} return a;}
static int[] nextIntArrayOneBased(int n) {int[] a= new int[n+1]; int i=1;while(i<=n){a[i++]=nextInt();} return a;}
static long[] nextLongArrayOneBased(int n){long[]a= new long[n+1];int i=1;while(i<=n){a[i++]=nextLong();}return a;}
static void print(Object o) { writer.print(o); }
static void println(Object o){ writer.println(o);}
/************************ TEMPLATE ENDS HERE ************************/
} | JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int m, num[10], a[10], pov[10], all, dig, dd;
long long n, f[1 << 18][110], ten[20];
void teg(int ip) {
dig = 0;
for (int i = 9; i >= 0; i--) a[i] = ip % num[i], ip /= num[i], dig += a[i];
}
int main() {
scanf("%lld%d", &n, &m);
while (n) num[n % 10]++, n /= 10, dd++;
ten[0] = 1;
for (int i = 1; i <= dd; i++) ten[i] = ten[i - 1] * 10;
for (int i = 0; i < 10; i++) num[i]++;
pov[9] = 1;
for (int i = 8; i >= 0; i--) pov[i] = pov[i + 1] * num[i + 1];
all = pov[0] * num[0];
for (int i = 1; i < 10; i++)
if (num[i] > 1) f[pov[i]][(ten[dd - 1] * i) % m] = 1;
for (int i = 1; i < all; i++) {
teg(i);
for (int j = 0; j < m; j++) {
if (!f[i][j]) continue;
for (int k = 0; k < 10; k++)
if (num[k] - a[k] > 1)
f[i + pov[k]][(ten[dd - dig - 1] * k + j) % m] += f[i][j];
}
}
printf("%lld\n", f[all - 1][0]);
return 0;
}
| CPP |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 |
import java.io.*;
import java.util.*;
public class D {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int count(long x) {
int res = 0;
while (x > 0) {
if (x % 2 != 0)
res++;
x /= 2;
}
return res;
}
public void run() {
String s = in.next();
int mod = in.nextInt();
int len = s.length();
long[][] dp = new long[(1<<len)][mod];
int ten = (int)(Math.pow(10, len - 1) % mod);
for (int i = 0; i < len; i++) {
int c = s.charAt(i) - '0';
if (c != 0) dp[(1 << i)][(c * ten) % mod] = 1;
}
for (int i = 0; i < (1 << len); i++) {
int pos = count(i);
ten = (int)(Math.pow(10, len - pos - 1) % mod);
for (int k = 0; k < mod; k++) {
if (dp[i][k] == 0) continue;
for (int j = 0; j < len; j++) {
if ((i & (1 << j)) != 0) continue;
int c = s.charAt(j) - '0';
int next = i | (1 << j);
dp[next][(k + c * ten) % mod] += dp[i][k];
}
}
}
long res = dp[(1<<len) - 1][0];
int[] cnt = new int[10];
for (int i = 0; i < len; i++) {
cnt[s.charAt(i) - '0']++;
}
for (int i = 0; i < 10; i++) {
for (int j = cnt[i]; j >= 2; j--) {
res /= j;
}
}
System.out.println(res);
out.close();
}
public static void main(String[] args) {
new D().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
System.out.println();
}
public void debug(Object... obj) {
System.out.println(Arrays.deepToString(obj));
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
//stream = new FileInputStream(new File("dec.in"));
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
int[][] nextIntMap(int n, int m) {
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextIntArray(m);
}
return map;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
long[][] nextLongMap(int n, int m) {
long[][] map = new long[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextLongArray(m);
}
return map;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
double[][] nextDoubleMap(int n, int m) {
double[][] map = new double[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextDoubleArray(m);
}
return map;
}
String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| JAVA |
401_D. Roman and Numbers | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long n;
int m, len;
long long dp[1 << 18][100];
int cnt[10];
int A[20];
int main() {
cin >> n >> m;
memset(cnt, 0, sizeof(cnt));
;
memset(dp, 0, sizeof(dp));
;
memset(A, 0, sizeof(A));
;
long long N = n;
len = 0;
while (N) {
A[len] = N % 10;
cnt[A[len]]++;
len++;
N /= 10;
}
dp[0][0] = 1;
for (int mask = 0; (mask) < ((1 << len)); mask++) {
for (int k = 0; (k) < (len); k++)
if ((mask & (1 << k)) == 0) {
if (A[k] == 0 && (mask == 0)) continue;
for (int c = 0; (c) < (m); c++) {
dp[mask | (1 << k)][(c * 10 + A[k]) % m] += dp[mask][c];
}
}
}
long long res = dp[(1 << len) - 1][0];
for (int i = 0; (i) < (10); i++)
for (int j = (cnt[i]); (j) >= (2); j--) res = res / j;
cout << res << endl;
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.