malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int mx = 110, Mod = 1e6 + 3;   int C, W, H;   struct Matrix {     int val[mx][mx];     int n;     Matrix(int _n = mx, int _base = 0) {       n = _n;       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) val[i][j] = (i == j ? _base : 0);     }     int* operator[](int x) { return val[x]; }   };   Matrix operator*(Matrix A, Matrix B) {     Matrix res(A.n);     for (int i = 0; i < res.n; i++)       for (int j = 0; j < res.n; j++)         for (int k = 0; k < res.n; k++)           res[i][k] = (0LL + res[i][k] + 1LL * A[i][j] * B[j][k]) % Mod;     return res;   }   Matrix operator^(Matrix A, long long int B) {     Matrix res(A.n, 1);     while (B) {       if (B & 1) res = res * A;       A = A * A;       B >>= 1;     }     return res;   }   int main() {     scanf( %d%d%d , &C, &W, &H);     Matrix res(W + 1);     for (int i = 0; i <= W; i++) res[0][i] = 1, res[i + 1][i] = H;     res = res ^ C;     int ans = 0;     for (int i = 0; i <= W; i++) ans = (0LL + ans + res[i][0]) % Mod;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 123;   const int K = (int)500;   const int maxn = (int)2e9;   const int mod = (int)1e9 + 7;   long long n;   vector<long long> v;   void solve() {     cin >> n;     long long cnt = 1;     for (int i = 0; i <= 62; ++i) {       if (i) {         cnt *= 2ll;       }       long long cnt2 = cnt - 1;       long long ok = 0;       long long l = 1, r = 1ll * 1e18;       while (l <= r) {         long long m = l + (r - l) / 2;         if (n % m) {           if ((m - 1) / 2 + cnt2 < n / m)             l = m + 1;           else             r = m - 1;           continue;         }         if ((m - 1) / 2 + cnt2 == n / m) {           if (m % 2) {             ok = m + m * cnt2;           }           break;         }         if ((m - 1) / 2 + cnt2 < n / m) {           l = m + 1;         } else {           r = m - 1;         }       }       if (ok) {         v.push_back(ok);       }     }     if (!(int)v.size()) {       cout << -1;       return;     }     sort(v.begin(), v.end());     v.resize(unique(v.begin(), v.end()) - v.begin());     for (long long i : v) {       cout << i <<   n ;     }   }   int main() {     ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int q = 1;     while (q--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void r1(T &x) {     x = 0;     char c(getchar());     int f(1);     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);     x *= f;   }   const int maxn = 100000 + 5;   const int maxm = maxn << 1;   typedef int room[maxn];   int n, m, d;   int dis[maxn], f[maxn][2];   int head[maxn], cnt;   struct Edge {     int to, next;   } edg[maxn << 1];   void add(int u, int v) { edg[++cnt] = (Edge){v, head[u]}, head[u] = cnt; }   int vis[maxn];   void dfs(int p, int fa) {     if (vis[p]) f[p][0] = 0;     for (int i = head[p]; i; i = edg[i].next) {       int to = edg[i].to;       if (to == fa) continue;       dfs(to, p);       if (f[to][0] + 1 > f[p][0]) {         f[p][1] = f[p][0], f[p][0] = f[to][0] + 1;       } else         f[p][1] = max(f[p][1], f[to][0] + 1);     }   }   void dfs1(int p, int fa) {     for (int i = head[p]; i; i = edg[i].next) {       int to = edg[i].to;       if (to == fa) continue;       if (f[p][0] == f[to][0] + 1) {         dis[to] = max(f[p][1] + 1, dis[p] + 1);       } else         dis[to] = max(dis[p] + 1, f[p][0] + 1);       dfs1(to, p);     }   }   signed main() {     int i, j;     memset(f, -0x3f, sizeof(f)), memset(dis, -0x3f, sizeof(dis));     r1(n), r1(m), r1(d);     for (i = 1; i <= m; ++i) {       int x;       r1(x);       vis[x] = 1;     }     for (i = 1; i < n; ++i) {       int u, v;       r1(u), r1(v);       add(u, v), add(v, u);     }     dfs(1, 0), dfs1(1, 0);     int ans((bool)(f[1][1] <= d && f[1][0] <= d));     for (i = 2; i <= n; ++i)       if (dis[i] <= d && f[i][0] <= d) {         ++ans;       }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 0xFFFFFFFFFFFFFFFL;   clock_t time_p = clock();   void time() {     time_p = clock() - time_p;     cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ;   }   long long mul_mod(long long a, long long b, long long m) {     long long y =         (long long)((long double)a * (long double)b / m + (long double)1 / 2);     y = y * m;     long long x = a * b;     long long r = x - y;     if (r < 0) {       r = r + m;       y = y - 1;     }     return r;   }   long long pot(long long a, long long b, long long c) {     if (b == 0) return 1;     if (b == 1) return a % c;     long long resp = pot(a, b >> 1, c);     resp = mul_mod(resp, resp, c);     if (b & 1) resp = mul_mod(resp, a, c);     return resp;   }   bool isPrime(long long n) {     long long d = n - 1;     long long s = 0;     if (n <= 3 || n == 5) return true;     if (!(n & 1)) return false;     while (!(d & 1)) {       s++;       d >>= 1;     }     for (long long i = 0; i < 32; i++) {       long long a = rand();       a <<= 32;       a += rand();       a %= (n - 3);       a += 2;       long long x = pot(a, d, n);       if (x == 1 || x == n - 1) continue;       for (long long j = 1; j <= s - 1; j++) {         x = mul_mod(x, x, n);         if (x == 1) return false;         if (x == n - 1) break;       }       if (x != n - 1) return false;     }     return true;   }   pair<long long, long long> breaker(long long n) {     if (isPrime(n)) return {n, 0};     for (long long i = 2; i < n; i++) {       if (isPrime(i) && isPrime(n - i)) return {i, n - i};     }   }   int main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     if (isPrime(n)) {       cout << 1 <<   n  << n <<   n ;       return 0;     }     if (isPrime(n - 2)) {       cout << 2 <<   n ;       cout << 2 <<     << n - 2 <<   n ;       return 0;     }     if (isPrime(n - 3)) {       cout << 2 <<   n ;       cout << 3 <<     << n - 3 <<   n ;       return 0;     }     if (isPrime(n - 4)) {       cout << 3 <<   n ;       cout << 2 <<     << 2 <<     << n - 4 <<   n ;       return 0;     }     long long part1 = 0, part2 = 0, part3 = 0;     for (long long i = n - 5; i >= 0; i--) {       if (isPrime(i)) {         part1 = i;         pair<long long, long long> p = breaker(n - i);         part2 = p.first;         part3 = p.second;         break;       }     }     long long ct = 0;     vector<long long> list;     if (part1 != 0) {       ct++;       list.push_back(part1);     }     if (part2 != 0) {       ct++;       list.push_back(part2);     }     if (part3 != 0) {       ct++;       list.push_back(part3);     }     cout << ct <<   n ;     for (long long i = 0; i < (long long)list.size(); i++) cout << list[i] <<    ;     cout <<   n ;     time();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char a[1000];     int i = 0, lower = 0, upper = 0;     cin >> a;     int len = strlen(a);     while (a[i] != 0) {       if (a[i] >=  a  && a[i] <=  z ) {         lower++;         i++;       } else if (a[i] >=  A  && a[i] <=  Z ) {         upper++;         i++;       } else         i++;     }     if (upper > lower) {       for (i = 0; i < len; i++) {         if (a[i] >= 97 && a[i] <= 122) {           a[i] = a[i] - 32;         }       }       cout << a;     } else {       for (i = 0; i < len; i++) {         if (a[i] >= 65 && a[i] <= 92) {           a[i] = a[i] + 32;         }       }       cout << a;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     vector<int> t(n);     for (int i = 0; i < n; i++) {       cin >> t[i];     }     a.push_back((int)1.01e9);     t.push_back(0);     ++n;     vector<int> order(n);     iota(order.begin(), order.end(), 0);     sort(order.begin(), order.end(), [&](int i, int j) { return a[i] < a[j]; });     unsigned long long ans = 0;     int last = 0;     multiset<int> s;     long long sum = 0;     for (int i : order) {       while (a[i] > last && !s.empty()) {         ++last;         auto it = prev(s.end());         sum -= *it;         s.erase(it);         ans += sum;       }       last = a[i];       s.insert(t[i]);       sum += t[i];     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 10;   const long long INF = 4e18 + 10;   const int maxn = 1 * 1e5 + 5;   const int maxe = maxn;   int a[maxn];   int p[maxn];   int n, m;   bool check(long long t) {     for (int i = 1; i <= n; ++i) p[i] = a[i];     int j = n;     for (int i = 0; i < m; ++i) {       while (j > 0 && p[j] == 0) --j;       if (j == 0) return true;       long long res = t - j;       if (res <= 0) return false;       while (j > 0 && res >= p[j]) res -= p[j--];       if (j > 0) p[j] -= res;     }     if (j == 0) return true;     return false;   }   int MAIN() {     scanf( %d%d , &n, &m);     long long lb = -1, rb = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       rb += i + a[i];     }     while (rb - lb > 1) {       long long mid = lb + (rb - lb) / 2;       if (check(mid))         rb = mid;       else         lb = mid;     }     printf( %I64d n , rb);     return 0;   }   int main() {     ios_base::sync_with_stdio(false);     cout << fixed << setprecision(16);     int ret = MAIN();     return ret;   }
#include <bits/stdc++.h>   using namespace std;   char a[200][200];   long long check(long long x, long long y, long long n) {     if (x >= 0 and x < n and y >= 0 and y < n and a[x][y] ==  . )       return 1;     else       return 0;   }   void solve() {     long long n;     cin >> n;     map<long long, long long> m;     for (long long i = 0; i < n; i++) {       long long t;       cin >> t;       m[t]++;     }     auto i = m.rbegin();     long long ans = 0;     long long hold = 0;     for (; i != m.rend(); i++) {       long long k = min(hold, i->second);       ans += k;       hold = hold - k + i->second;     }     cout << ans << endl;     return;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int v[361 * 2], acum[360 * 2];   int main() {     int n, tmp;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> v[i];       v[i + n] = v[i];     }     int ans = (1 << 20);     for (int i = 0; i < n; i++) {       for (int j = i; j < i + n; j++) {         int acum1 = 0, acum2 = 0;         for (int k = i; k < i + n; k++) {           if (k < j)             acum1 += v[k];           else             acum2 += v[k];         }         ans = min(ans, abs(acum1 - acum2));       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, pair<int, int> > > g[300043];   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     for (int i = 0; i < m; i++) {       int x, y, w;       scanf( %d %d %d , &x, &y, &w);       --x;       --y;       g[x].push_back(make_pair(y, make_pair(w, i)));       g[y].push_back(make_pair(x, make_pair(w, i)));     }     set<pair<long long, int> > q;     vector<long long> d(n, (long long)(1e18));     d[0] = 0;     q.insert(make_pair(0, 0));     vector<int> last(n, -1);     int cnt = 0;     vector<int> ans;     while (!q.empty() && cnt < k) {       set<pair<long long, int> >::iterator it = q.begin();       q.erase(q.begin());       pair<int, int> top = *it;       int k = top.second;       if (last[k] != -1) {         cnt++;         ans.push_back(last[k]);       }       vector<pair<int, pair<int, int> > >::iterator ii;       for (ii = g[k].begin(); ii != g[k].end(); ii++) {         pair<int, pair<int, int> > y = *ii;         int to = y.first;         int w = y.second.first;         int idx = y.second.second;         if (d[to] > d[k] + w) {           q.erase(make_pair(d[to], to));           d[to] = d[k] + w;           last[to] = idx;           q.insert(make_pair(d[to], to));         }       }     }     printf( %d n , ans.size());     vector<int>::iterator x;     for (x = ans.begin(); x != ans.end(); x++) printf( %d  , *x + 1);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 41;   const int mod = 1e9 + 7;   const long long INF = 1e18;   const int inf = 2e9;   vector<int> g[N];   long long res;   int cnt[2];   int a[N];   int sz[N];   bool used[N];   int cur_sz;   long long cur;   void pre(int v = 1, int p = -1) {     sz[v] = 1;     for (int to : g[v])       if (to != p && !used[to]) {         pre(to, v);         sz[v] += sz[to];       }   }   int f(int v, int p = -1) {     for (int to : g[v])       if (!used[to] && to != p && sz[to] * 2 > cur_sz) {         return f(to, v);       }     return v;   }   bool kth(int val, int bit) {     if (val & (1 << bit))       return true;     else       return false;   }   void addcur(int v, int p, int bit, bool pref = 0) {     pref ^= kth(a[v], bit);     cur += cnt[pref ^ 1];     for (int to : g[v])       if (!used[to] && to != p) {         addcur(to, v, bit, pref);       }   }   void addcnt(int v, int p, int bit, bool pref = 0) {     pref ^= kth(a[v], bit);     cnt[pref]++;     for (int to : g[v])       if (!used[to] && to != p) {         addcnt(to, v, bit, pref);       }   }   void dfs(int v = 1) {     pre(v);     cur_sz = sz[v];     v = f(v);     for (int bit = 0; bit < 20; ++bit) {       cur = 0;       cnt[1] = 0, cnt[0] = 1;       for (int to : g[v])         if (!used[to]) {           bool st = kth(a[v], bit);           addcur(to, v, bit, st);           addcnt(to, v, bit);         }       res += (1ll << bit) * 1ll * cur;     }     used[v] = 1;     for (int to : g[v])       if (!used[to]) {         dfs(to);       }   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     ;     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       res += a[i];     }     for (int i = 1; i < n; ++i) {       int a, b;       cin >> a >> b;       g[a].push_back(b);       g[b].push_back(a);     }     dfs();     cout << res;   }
#include <bits/stdc++.h>   using namespace std;   const int dx[] = {0, 0, -1, 1};   const int dy[] = {1, -1, 0, 0};   const int N = 505;   int n, m;   char adj[N][N];   bool vis[N][N];   struct node {     int type, x, y;     node() {}     node(int type, int x, int y) : type(type), x(x), y(y) {}     void print() {       if (type == 0)         putchar( B );       else if (type == 1)         putchar( R );       else         putchar( D );       printf(  %d %d n , x + 1, y + 1);     }   };   vector<node> ans;   void dfs(int x, int y) {     vis[x][y] = 1;     for (int i = 0; i < 4; i++) {       int xx = x + dx[i];       int yy = y + dy[i];       if (xx >= 0 && xx < n && yy >= 0 && yy < m && vis[xx][yy] == 0) {         ans.push_back(node(0, xx, yy));         dfs(xx, yy);         ans.push_back(node(2, xx, yy));         ans.push_back(node(1, xx, yy));       }     }   }   int main() {     while (scanf( %d%d , &n, &m) != EOF) {       ans.clear();       memset(vis, 0, sizeof(vis));       for (int i = 0; i < n; i++) scanf( %s , adj[i]);       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           if (adj[i][j] ==  # ) vis[i][j] = 1;         }       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++)           if (vis[i][j] == 0) {             ans.push_back(node(0, i, j));             dfs(i, j);           }       }       int len = (int)ans.size();       printf( %d n , len);       for (int i = 0; i < len; i++) ans[i].print();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int arr[N];   int main() {     int n;     memset(arr, 0, sizeof arr);     cin >> n;     int maxx = n;     int num;     for (int i = 0; i < n; i++) {       cin >> num;       arr[num] = -1;       while (arr[maxx] == -1) {         cout << maxx <<    ;         maxx -= 1;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int abs(int x) { return x > 0 ? x : -x; }   int min(int x, int y) { return x < y ? x : y; }   int max(int x, int y) { return x > y ? x : y; }   int main() {     int a1, a2, a3, a4;     scanf( %d%d%d%d , &a1, &a2, &a3, &a4);     if (abs(a3 - a4) > 1 || max(a3, a4) > min(a1, a2) || a3 + a4 + 1 > a1 + a2 ||         a3 + a4 == 0) {       printf( -1 n );       return 0;     }     int c = min(a3, a4);     a3 -= c;     a4 -= c;     a1 -= c, a2 -= c;     if (!a3 && !a4) {       if (!a1) {         for (int i = 1; i <= c; i++) printf( 74 );         for (int i = 1; i <= a2; i++) printf( 7 );       } else {         for (int i = 1; i < a1; i++) printf( 4 );         for (int i = 1; i <= c; i++) printf( 47 );         for (int i = 1; i <= a2; i++) printf( 7 );         printf( 4 n );       }     } else if (a3) {       for (int i = 1; i < a1; i++) printf( 4 );       for (int i = 1; i <= c + 1; i++) printf( 47 );       for (int i = 1; i < a2; i++) printf( 7 );     } else if (a4) {       printf( 74 );       for (int i = 1; i < a1; i++) printf( 4 );       for (int i = 1; i < c; i++) printf( 74 );       for (int i = 1; i <= a2; i++) printf( 7 );       printf( 4 );     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void c_p_c() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cout << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   long long int n, m;   char a[50][50];   long long int vis[50][50];   long long int cnt = 0;   long long int dx[] = {1, -1, 0, 0};   long long int dy[] = {0, 0, 1, -1};   bool is_valid(long long int i, long long int j) {     if (i >= 0 and j >= 0 and i < n and j < m and a[i][j] ==  B ) {       return true;     }     return false;   }   void dfs(long long int i, long long int j, long long int dn,            long long int dir) {     if (vis[i][j] == 0) {       vis[i][j] = 1;       cnt++;     }     for (long long int k = 0; k < 4; k++) {       long long int x = i + dx[k];       long long int y = j + dy[k];       if (is_valid(x, y)) {         long long int g;         long long int p;         if (dir == -1) {           g = 0;         }         if (dir != -1) {           if (k == dir) {             g = dn;           } else {             g = dn + 1;           }         }         if (g > 1) {           continue;         }         dfs(x, y, g, k);       }     }   }   int32_t main() {     c_p_c();     cin >> n >> m;     long long int k = 0;     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < m; j++) {         cin >> a[i][j];         if (a[i][j] ==  B ) {           k++;         }       }     }     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < m; j++) {         if (a[i][j] ==  B ) {           memset(vis, 0, sizeof(vis));           cnt = 0;           dfs(i, j, 0, -1);           if (cnt != k) {             cout <<  NO  <<   n ;             return 0;           }         }       }     }     cout <<  YES  <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     long long x, q = k - 1, flag, a[k], i, j;     for (i = 0; i < n; i++) {       cin >> x;       if (!i)         a[q--] = x;       else {         flag = 0;         for (j = k - 1; j > q; j--) {           if (a[j] == x) {             flag = 1;             break;           }         }         if (flag == 0) {           if (q < 0) {             for (j = k - 1; j > 0; j--) a[j] = a[j - 1];             a[0] = x;           } else             a[q--] = x;         }       }     }     cout << k - (q + 1) << endl;     for (i = q + 1; i < k; i++) cout << a[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1000100100;   int n, m;   string s[105];   int team[105];   int dp[21][(1 << 21)];   int a[104];   int solve(int cur, int mask) {     if (cur == m) return 0;     int &ret = dp[cur][mask];     if (ret == inf) {       if (s[cur] ==  p ) {         int j = 0;         while (j < m && ((mask >> j) & 1)) ++j;         if (team[cur] == 1)           ret = solve(cur + 1, mask | (1 << j)) + a[j];         else           ret = solve(cur + 1, mask | (1 << j)) - a[j];       } else {         ret = solve(cur + 1, mask);         for (int j = 0; j < m; ++j) {           if ((mask >> j) & 1) continue;           if (team[cur] == 1)             ret = max(ret, solve(cur + 1, mask | (1 << j)));           else             ret = min(ret, solve(cur + 1, mask | (1 << j)));         }       }     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     sort(a, a + n, greater<int>());     cin >> m;     for (int i = 0; i < m; ++i) {       cin >> s[i] >> team[i];     }     fill(&dp[0][0], &dp[0][0] + (21 * (1 << 21)), inf);     cout << solve(0, 0);   }
#include <bits/stdc++.h>   const double PI = 3.141592653589793238460;   using namespace std;   int main() {     long long int t, n, k;     cin >> t;     while (t--) {       cin >> n >> k;       if ((n < k * k))         cout <<  NO  <<   n ;       else if ((k % 2) != (n % 2))         cout <<  NO  <<   n ;       else         cout <<  YES  <<   n ;     }   }
////JAI SHREE RAM////   #include <iostream>   #include<bits/stdc++.h>   using namespace std;   const long long int INF = (long long)1e15;   const long long int mod = 1e9+7;   #define ll long long   #define f(i,a,b) for(int i=a;i<b;i++)   #define pb push_back   #define all(c) c.begin(),c.end()   #define rall(c) c.rbegin(),c.rend()   #define yes cout<< YES n    #define no cout<< NO n    #define sd second   #define ft first   //   bool cmp(const pair<int,int> &a, const pair<int,int> &b)   {       //return (a.sd>=b.sd);       return abs(a.ft)+abs(a.sd) < abs(b.ft)+abs(b.sd);   }      void solve()   {       int n;cin>>n;       int o=0;       n*=2;       f(i,0,n){           int x;cin>>x;           if(x%2) o++;       }       if(o==n/2) yes;       else no;   }   int main()   {       //ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0);       int t=1;       cin>>t;          while(t--)       {           solve();       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     double x, cx;     cin >> n >> x;     cx = x;     double a[n + 1], b[n + 1];     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (a[i] == 1) {         cout << -1;         return 0;       }     }     for (int i = 1; i <= n; i++) {       cin >> b[i];       if (b[i] == 1) {         cout << -1;         return 0;       }     }     x = x + x / (b[1] - 1);     for (int i = n; i > 1; i--) {       x = x * a[i] / (a[i] - 1);       x = x * b[i] / (b[i] - 1);     }     x = x + x / (a[1] - 1);     cout << setprecision(8) << x - cx;     return 0;   }